Lab 6 Introduction to Serial and Wireless Communication

Similar documents
Microcontroller Programming Beginning with Arduino. Charlie Mooney

Arduino Lesson 5. The Serial Monitor

Theory and Practice of Tangible User Interfaces. Thursday Week 2: Digital Input and Output. week. Digital Input and Output. RGB LEDs fade with PWM

Basic Pulse Width Modulation

Arduino Lesson 13. DC Motors. Created by Simon Monk

Surveillance System Using Wireless Sensor Networks

Arduino project. Arduino board. Serial transmission

IR Communication a learn.sparkfun.com tutorial

Connecting Arduino to Processing a

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

Arduino Lesson 16. Stepper Motors

H-Bridge Motor Control

Arduino Lesson 1. Blink

Computer Architectures

Web Site: Forums: forums.parallax.com Sales: Technical:

Arduino Shield Manual

Arduino Wifi shield And reciever. 5V adapter. Connecting wifi module on shield: Make sure the wifi unit is connected the following way on the shield:

Arduino Shield Manual

MeshBee Open Source ZigBee RF Module CookBook

Eric Mitchell April 2, 2012 Application Note: Control of a 180 Servo Motor with Arduino UNO Development Board

Waspmote. Quickstart Guide

Ultrasonic Distance Measurement Module

Using Xbee in Serial Communication

Arduino DUE + DAC MCP4922 (SPI)

Single channel data transceiver module WIZ2-434

Introduction to Arduino

Your Multimeter. The Arduino Uno 10/1/2012. Using Your Arduino, Breadboard and Multimeter. EAS 199A Fall Work in teams of two!

DEPARTMENT OF ELECTRONICS ENGINEERING

Building a Basic Communication Network using XBee DigiMesh. Keywords: XBee, Networking, Zigbee, Digimesh, Mesh, Python, Smart Home

Arduino Lesson 14. Servo Motors

EARTH PEOPLE TECHNOLOGY SERIAL GRAPH TOOL FOR THE ARDUINO UNO USER MANUAL

BLUETOOTH SERIAL PORT PROFILE. iwrap APPLICATION NOTE

Microcontroller Based Low Cost Portable PC Mouse and Keyboard Tester

Lecture 7: Programming for the Arduino

Part Number Description Packages available

Remote control circuitry via mobile phones and SMS

Lab Experiment 1: The LPC 2148 Education Board

Home Security System for Automatic Doors

Arduino Motor Shield (L298) Manual

Programming the Arduino

Basic DC Motor Circuits. Living with the Lab Gerald Recktenwald Portland State University

Electronics 5: Arduino, PWM, Mosfetts and Motors

Basic DC Motor Circuits

1602 LCD adopts standard 14 pins(no backlight) or 16pins(with backlight) interface, Instruction of each pin interface is as follows:

Arduino Lab 1 - The Voltage Divider

Modbus and ION Technology

Wireless Security Camera with the Arduino Yun

RDF1. RF Receiver Decoder. Features. Applications. Description. Ordering Information. Part Number Description Packages available

ZX-NUNCHUK (# )

1. Learn about the 555 timer integrated circuit and applications 2. Apply the 555 timer to build an infrared (IR) transmitter and receiver

Yun Shield Quick Start Guide VERSION: 1.0 Version Description Date 1.0 Release 2014-Jul-08 Yun Shield Quick Start Guide 1 / 14

Experiments: Labview and RS232

Software Manual RS232 Laser Merge Module. Document # SU Rev A

Lab 3 Microcontroller programming Interfacing to Sensors and Actuators with irobot

The Answer to the 14 Most Frequently Asked Modbus Questions

LED light control using DMX512 control method

Arduino Internet Connectivity: Maintenance Manual Julian Ryan Draft No. 7 April 24, 2015

Intro to Intel Galileo - IoT Apps GERARDO CARMONA

Wireless Communication With Arduino

2011, The McGraw-Hill Companies, Inc. Chapter 3

Written examination in Computer Networks

CanSat Program. Stensat Group LLC

TUTORIAL FOR INITIALIZING BLUETOOTH COMMUNICATION BETWEEN ANDROID AND ARDUINO

Electronic Brick of Current Sensor

Massachusetts Institute of Technology

-Helping to make your life betterwww.person-to-person.net

Arduino Lesson 9. Sensing Light

Embedded Systems Design Course Applying the mbed microcontroller

cs281: Introduction to Computer Systems Lab08 Interrupt Handling and Stepper Motor Controller

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

C4DI Arduino tutorial 4 Things beginning with the letter i

ReSound Unite TV FREQUENTLY ASKED QUESTIONS. Setup & Configuration. Use & Operation. Troubleshooting

Arduino Microcontroller Guide W. Durfee, University of Minnesota ver. oct-2011 Available on-line at

[WIR-1186] 865MHz-869MHz Wireless Module(version 3.0) (3.3V)

PHYS 2P32 Project: MIDI for Arduino/ 8 Note Keyboard

If an occupancy of room is zero, i.e. room is empty then light source will be switched off automatically

The I2C Bus. NXP Semiconductors: UM10204 I2C-bus specification and user manual HAW - Arduino 1

PCM Encoding and Decoding:

Bluetooth for device discovery. Networking Guide

VirtualWire. Copyright (C) Mike McCauley. 1.0 Introduction. 2.0 Overview

Display Board Pulse Width Modulation (PWM) Power/Speed Controller Module

Conversion Between Analog and Digital Signals


The BSN Hardware and Software Platform: Enabling Easy Development of Body Sensor Network Applications

Bidirectional wireless communication using EmbedRF

Modbus and ION Technology

Work with Arduino Hardware

The Analyst RS422/RS232 Tester. With. VTR, Monitor, and Data Logging Option (LOG2) User Manual

RB-See-211. Seeedstudio Grove 6-15V, 2A Dual I2C Motor Driver. Grove - I2C Motor Driver. Introduction

Wireless Security Camera

A REST API for Arduino & the CC3000 WiFi Chip

Servo Motor API nxt_motor_get_count nxt_motor_set_count nxt_motor_set_speed

A+ Guide to Managing and Maintaining Your PC, 7e. Chapter 1 Introducing Hardware

RS-232 Communications Using BobCAD-CAM. RS-232 Introduction

CENTRONICS interface and Parallel Printer Port LPT

Introduction to Arduino

How To Control A Car With A Thermostat

Adding Heart to Your Technology

Transcription:

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). Created by Nick Howarth, EE 13 Last updated by Joe Trovato in Fall 2014 Figure 1: Transmitter and Receiver Modules

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 milliseconds (maybe 5) after Serial.available() > 0 before reading the characters in so 2

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). 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 c. 65 83 67 73 73 32 105 115 32 97 119 101 115 111 109 101 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); 3

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. 4

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 postlab. 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 a. Connect the XBee Shield to the Arduino Board as shown in Figure 2 (or Figure 5). 5

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. 6

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). Figure 8: Transmitter voltage divider circuit 7

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 d. Copy the following code into the Arduino IDE window. 8

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 9

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; const int powerpin = 19; const int ypin = 16; // analog input pin4 -- ground (GND) // analog input pin5 -- voltage (Vcc) // 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. 10

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 = ***; int ymax = ***; // Replace with measured minimum yvalue // 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! 11

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? 12

Figure 8 - Electrocardiograph (http://www.instrumentalanalysis.com/en/projects/fv_fum.php) 13