• No results found

Lab 6 Introduction to Serial and Wireless Communication

N/A
N/A
Protected

Academic year: 2021

Share "Lab 6 Introduction to Serial and Wireless Communication"

Copied!
13
0
0

Loading.... (view fulltext now)

Full text

(1)

University of Pennsylvania

Department of Electrical and Systems Engineering ESE 111 – Intro to Elec/Comp/Sys Engineering

Lab 6 – Introduction to Serial and Wireless Communication Introduction:

Up to this point, you have only used sensors and pushbuttons to control the input to your Arduino. You will now learn how to receive communication signals or commands on the Arduino from the serial port. You have used Serial.print() to transmit communication signals to the serial monitor. Serial.read() will allow to you send characters from the input line on the serial monitor to the Arduino.

First, you will learn how to send commands to the Arduino via the serial port using the keyboard and how to interpret the input. You will use these commands to control the behavior of an LED. Then, you will learn how to use two XBee modules to transmit and receive information

wirelessly. XBee is a brand of low-power radio transmitter/receiver, based on the ZigBee digital communication protocol, which uses the serial port to communicate. You will program one Arduino board to transmit one byte per second through an XBee module. The other Arduino will receive the byte through an XBee module configured on the same channel as the transmitter, and it will turn an LED on or off based on the identity of the byte. Next, you will use the XBee modules to remotely control an LED on one Arduino with a photoresistor on another Arduino. Finally, you will transmit accelerometer data wirelessly to control the brightness of an LED using pulse width modulation (PWM).

Figure 1: Transmitter and Receiver Modules

(2)

Goals:

- To understand the basics of serial communication.

- To learn about ASCII encoding and the different formats of a variable. - To wirelessly control the lighting of an LED using Zigbee.

- To understand pulse width modulation (PWM).

1. Introduction to Serial Communication

(Adapted excerpt from BE 470, Lab 3; written by Prof. Dan Bogen)

Serial.read() is an Arduino function that allows to you send characters from the input line on the serial monitor to the Arduino. It is important to note on the Arduino Leonardo, there are two serial ports. The first one uses the class Serial, this class is used to print to the computers serial monitor. The second class is Serial1; this class is used for XBee communication and will not show up on the serial monitor. Serial and Serial1 contains all of the same function but are called in different ways. For example to read a character you could use Serial.read() or Serial1.read().

The Serial.read() function is used like this: int inputCharacter;

inputCharacter = Serial.read();

Serial.read() takes its input from the serial port, the same port that is used for Serial.print(). In this lab you will receive serial input and output from the serial monitor.To initialize the serial port and be able to use these functions, you need to use the Serial.begin(9600) instruction in the setup() function of your Arduino code.

Serial.read() is a bit of a pain to use because it only can take in one character at a time. It can’t just accept the number 123 and know that it means one-hundred-twenty-three. Instead, you have to take in the ‘1’ character, ‘2’ character, and ‘3’ character, and then let the Arduino know that you want the number 123. . Serial.read() will wait until a character is available before returning a value. So if there is nothing there, it will hang until a serial character is available to be read.. This means that it sometimes makes sense to check whether there are characters present (using Serial.available()) before using Serial.read().

Serial.available() returns the number of available serial characters. But if a whole string of characters is being sent to the Arduino, Serial.available() will be > 0 before all the characters “get to” the Arduino. For this reason, sometimes it makes sense to wait a few

(3)

that you know that they are all there.

Another function related to Serial.read() is Serial.flush(). The serial port has an input buffer, so there may be some characters “left over” in the buffer that you don’t want to read. Serial.flush() empties this buffer so that you can read new characters.

The characters are received as numerical codes – a number corresponding to each alphanumeric character on the keyboard. Part of the table of codes is shown below. For instance, the letter ‘a’ is coded by the number 97, and capital ‘A’ by 65. The ‘1’ character is coded by the number 49, the ‘2’ character by the number 50, and so on.

One useful feature of the Arduino language is that the expression ‘X’ , where X is a single

character (i.e., a single character enclosed by single quotes) is equal to the numerical code for that character. For instance ‘a’ = 95, ‘1’ = 49, etc. This will be useful in Arduino programming, as we will see a little later. These codes are called ASCII codes, ASCII standing for American Standard Code for Information Interchange. A table of ASCII representations can be found at the following link: http://www.asciitable.com/ (you only need to know the “Dec” (decimal) and “Char”

(character) columns).

This part of the lab will attempt to illuminate the different formats a serial byte can be represented as. As you saw in the ASCII table the same data can be represented in many different ways. It is important to always know what format your data is in so you can use it later without needed to decode its format or guess at what the actual data is.

Please copy the code below or from the provide text file into an Arduino sketch. void setup(){

Serial.begin(9600); //initializes the serial port } void loop(){ int input_char; if( Serial.available() > 0 ){ input_char = Serial.read(); Serial.print(input_char); Prelab Question 1:

Please convert the following bytes or strings of bytes to their ASCII equivalent. a. 65 114 100 117 105 110 111

b. 69 83 69 49 49 49 32 82 111 99 107 115

(4)

Serial.print('\n'); //print a new line for readability }

}

This code prints out the serial data as it is received. Now add the following two lines between Serial.print(input_char) and Serial.print('\n').

Serial.print('\t');

Serial.print((char)input_char);

These lines print the input character in the char format as well as the original integer format. See the difference? Spell out your favorite candy and show a TA!

As we mentioned earlier, numbers must be built one digit at time. Copy the following code into a new Arduino sketch. Make sure to keep the old sketch open as well.

void setup(){ Serial.begin(9600); } void loop(){ int inputChar; int inputNumber; inputNumber = 0;

if( Serial.available() > 0 ){// build the number as long as there are characters

delay(5);

while (Serial.available() > 0) {

inputChar = Serial.read();// build number from left to right, shifting left by multiplying by 10

inputNumber = inputNumber*10 + (inputChar - '0'); // why inputChar – ‘0’ ? Make sure you understand this!

}

Serial.print(inputNumber); }

}

Try entering different numbers into the Serial Monitor. The program should “echo” the number back to you. Which numbers behave differently than the first program? Think about this difference to prepare for the post lab.

Prelab Question 2:

a. Why do we write input_char – ‘0’ when trying to convert the result of Serial.read() to a number? Hint: Think about the ASCII table and what data type you are reading and what data type you want.

(5)

2. Control of an LED using input from keyboard

1) Write a program that accepts a character input from the serial monitor. The on-board LED connected to pin 13 should turn on if the input is ‘H’, and the LED on pin 13 should turn off if the input is ‘L’. Otherwise, nothing should happen. Remember that case matters in ASCII encodings.

a. Remember to initialize serial communication by putting Serial.begin(9600) in setup().

b. Remember to set pin 13 to an output by adding pinMode(13, OUTPUT)in setup().

c. Copy this code into a text file. You will need it for the next section and the post-lab.

2) Write a program that reads in a number from the serial monitor. Use the input number to control the flashing rate of the LED on pin 13; that is, use the input number as the

argument in your delay functions.

a. Copy this code into a text file. You will need it for section 5 and the post-lab.

3. Remote Control of an LED using XBees

In this section, you will wirelessly transmit ‘H’ and ‘L’ characters from one Arduino to another Arduino (instead of from keyboard to Arduino), and the receiver will turn on or off an LED accordingly, as in the previous section. This wireless communication is possible using XBees. The XBee modules work by transmitting data in the form of bytes (e.g. ASCII characters) through the serial port. This means that any character that is printed to the serial monitor (from the transmitter) is received on the other module. You can fetch these characters on the receiving end the same way you would if you were just entering characters directly into the serial monitor. Only XBees that are on the same channel will be able to communicate with each other; this is what allows us to have multiple pairs of XBees communicating in one room without

interference. Make sure that your XBee modules have the same number written on them.

Receiver Module

(6)

Figure 5: Arduino with XBee Shield

b. Upload the receiver code to the Arduino

Upload the following code to the Transmitter Module. This code should accept a character input from the serial monitor. The on-board LED connected to pin 13 should turn on if the input is ‘H’, and the LED on pin 13 should turn off if the input is ‘L’. The only difference between this code and the code from the previous part is that the XBees use the Serial1 class to communicate, while the USB serial port used the Serial class.

Transmitter Module

c. Upload the following code to the Transmitter Arduino module void setup(){

Serial1.begin(9600); // initialize serial communication } void loop(){ Serial1.print(‘L’) ; delay(1000); Serial1.print(‘H’) ; delay(1000); }

d. Observe the flashing LED

The LED on the receiver module should blink on and off based on the serial data transmitted from the transmitter module.

(7)

4. Remote control of an LED using a photoresistor circuit and XBees

This part requires you to construct a circuit on both the receiving and transmitting Arduino boards. However, there is no room to fit a breadboard on the Arduino since it is occupied by the XBee shield. For this reason, we have constructed extension shields that can hold a breadboard shield, XBee shield, and LCD screen all on one shield. The only limitation is that each

component occupies a number of digital I/O pins; consequently, only two components can be used at a time. In this part of the lab, you will use the extension shield to hold the XBee shield and the breadboard shield.

Connect your shields as shown in Figure 7.

Transmitter Receiver

Figure 7: Configuration of Arduino Shields on Extension Shield

Transmitter Module

a. Construct the circuit given by the schematic in Figure 8. Choose any analog pin (0-5).

(8)

b. Upload the following code:

int analogPin = ***; // change *** to the pin number you're reading from int value;

void setup(){

Serial.begin(9600); // initialize serial communication Serial1.begin(9600);

pinMode(analogPin, INPUT); }

void loop(){

value = analogRead(analogPin);

if(value >= 150){ // you may need to modify this argument Serial1.print(‘L’);

delay(500); }

if(value < 150){ // you may need to modify this argument Serial1.print(‘D’);

delay(500); }

}

Receiver Module

c. Construct the circuit given by the schematic in Figure 9.

Figure 9 - Receiver LED circuit

(9)

const int ledPin = 12; // the pin that the LED is attached to int incomingByte; // a variable to read incoming serial data into

void setup() {

Serial1.begin(9600); // initialize serial communication

pinMode(ledPin, OUTPUT); // initialize the LED pin as an output }

void loop() {

// your code here

}

e. Finish writing the code above such that the LED turns on when the photocell on the transmitter is covered and is off otherwise.

Demonstrate to a TA that your system works!

5. Remote control of an LED using an accelerometer and XBees

For many applications of wireless communication, it is desirable to transmit a continuous, analog signal, such as data from an accelerometer or electromyograph (EMG). However, the XBee can only transmit and receive data in packets, where each packet is one byte, and one byte contains 8 bits (0 or 1) of information. (Note that in the previous part we were transmitting characters [designated by single quotes ‘ ’], which only store one byte of information.) So how do we break up an analog signal (imagine a sine wave) into bytes? Well, we can sample the signal at regular time intervals to obtain the integer values that make up the signal. An integer stores 2 bytes of information, so it cannot be sent in this form through the serial port. Instead, integers are printed one byte at a time using ASCII characters corresponding to each digit. For example, if we were to transmit the number 4316 through the serial port, it would transmit the ASCII representations of 4, 3, 1, and 6 in series. These bytes can then be regrouped into an int on the receiving end, thus reconstructing the original integer.

In this section, you will transmit integer values from an accelerometer and reconstruct them on the receiver. You will then use this data to control the brightness of an LED on the receiver. This is done using a technique pulse width modulation (PWM).

For an good explanation of PWM, read the following reference page: - http://www.arduino.cc/en/Tutorial/PWM

(10)

An easy way to implement PWM is by using the Arduino’s analogWrite() function. - http://arduino.cc/en/Reference/AnalogWrite

Note that this function only works on digital pins 3, 5, 6, 9, 10, 11, and 13 (and has no connection to analogRead() or the analog pins). By using the map() function from Lab 4, we can map the accelerometer values to a range of 0 to 255 (the range accepted by analogWrite()).

Transmitter Module

a. Connect an accelerometer to your transmitter module.

b. Complete the following code so that the transmitter sends the y-axis value to the receiver every 250 seconds. Set the Arduino in USB mode and upload your code.

const int groundpin = 18; // analog input pin4 -- ground (GND)

const int powerpin = 19; // analog input pin5 -- voltage (Vcc)

const int ypin = 16; // analog input pin2 -- y-axis pin

int yvalue; void setup() { Serial1.begin(9600); pinMode(ypin, INPUT); pinMode(groundpin, OUTPUT); pinMode(powerpin, OUTPUT);

digitalWrite(groundpin, LOW); // make analog pin (4) equivalent to GND (ground) digitalWrite(powerpin, HIGH); // make analog pin (5) equivalent to Vcc (voltage source) }

void loop(){ // your code here }

c. Set the Transmitter Arduino in XBee mode by connecting the jumpers.

d. Using the output on the serial monitor, determine the minimum and maximum y-axis values.

(11)

Receiver Module

e. Construct the circuit given by the schematic in Figure 10.

Figure 10: Receiver LED circuit

f. Set the Arduino in USB Programming mode (remove jumpers) and copy the following code into the Arduino IDE window.

int ymin = ***; // Replace with measured minimum yvalue int ymax = ***; // Replace with measured maximum yvalue int yval; // incoming y-axis value

void setup() { Serial1.begin(9600) ;

}

void loop() {

// insert code to reconstruct yval pwm = map(yval, ymin, ymax, 0, 255); analogWrite(9, pwm);

}

g. Replace *** with the values that you found in part d. Finish writing the code to reconstruct the transmitted value from the accelerometer. Hint: look at Fragment #2 on page 4).

h. Upload the finished code to the Arduino.

i. Tilt the accelerometer on the transmitter and observe the LED brightness on the receiver. Demonstrate to a TA that your system works!

(12)

6. Post-Lab Questions:

1. What does casting an int received from Serial.read() to a char do? In other words explain why we print (char)input_char instead of just input_char.

2. Why do you think numbers cannot be built all at once? Why can letters be read in with only one byte but numbers often have problems.

3. In Part 3, you created a system to blink the Arduino’s LED on and off every second. Rewrite the transmitter function to toggle the LED when the user input any character. You do not have to compile and upload this code. We just want to see the logic.

4. If analogWrite() was not available how would implement a function to produce a Pulse Width Mudulation (PWM) signal. A step by step algorithm or pseudo code will be accepted. EXTRA

CREDIT: actually write it!

7. Thinking Further – Biomedical Application

With technology rapidly evolving, it is becoming possible for medical professionals to remotely observe and treat patients. This new form of medicine, called telemedicine, allows people in need of medical surveillance to move around comfortably at home while their physiological signals, such as heart rate, brain and muscle activity, etc., are wirelessly transmitted to a computer and sent to a medical center to be analyzed.

Imagine a digital communication system consisting of two XBee modules (one transmitter and one receiver), two Arduinos, and a biomedical sensor such as an electrocardiograph (ECG), which records electrical currents associated with heart muscle activity (shown in Figure 9). The signal from the ECG is directly connected to the transmitter, which then wirelessly transmits the signal to the receiver. The data can then be transferred from the receiver onto a computer and sent to a medical center via an internet connection.

With your group, briefly discuss the following:

- What are some factors that might interfere with the quality of the transmitted signal? - Do you think that this could be an effective system for remote patient monitoring? - Could a similar type of system be used by a doctor to remotely treat a patient?

(13)

Figure 8 - Electrocardiograph

References

Related documents

These routines let you eas- ily read characters from the serial port, write characters to the serial port, and check to see if there is data available at the input port or see if it

 For writing and storing customer USB VID/PID, Serial Number, Product String, and other device startup configurations...  Extensive Flow

Then again, if you need to turn around fatty liver without taking drugs, if you don’t have a considerable measure of cash for medicinal conferences and medications, or if

It means the virtue COM port is in ttyUSB0, so if you want to use this port, you need to use &#34;ln&#34; command to hard link with device, for example: we are use PL-2303X/H

Download the Arduino IDE from the following link, http://arduino.cc/en/Main/Software Complete installation as per the instructions given in the previous section. C) Make

– A sequence of operations in a specific programming language, which may act upon real data in the form of numbers, images, sound, etc.. •

If the debugging computer is equipped with a standard serial port (usually RS-232 level), you can use the serial cable to connect the computer to the AIY-A005M, if you are using

automatically and the Bluetooth LED (blue color) on the adapter will become illuminated. If configured in the Slave state, the Bluetooth to Serial Adapter will wait for