ese205lab3

17
 University of Pennsylvania Department of Electrical and Systems Engineering ESE 205  Electrical Circuits and Systems Laboratory I Lab 3    XBees and LCDs and Accelerometers, Oh My! Introduction:  In the first part of this lab, 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. First, you will progr am one Arduino board to transmi t one byte per second through an XBee module. The other Arduino will receive t he byte through an XBee module configured on the same channel as the transmitter, and it will turn a servo left or right based on the identity of the byte. Next, you will use the XBee modules to remotely control an LED on one Arduino wit h a photoresistor on another Ar duino. This is simila r to what you did in the last  part of Lab 1, except this time, it’s WIRELESS!  In the second part of this lab, you will learn how to use a LCD (liquid crystal display) screen for text display and other interest ing applications. You will first create a timer that s hows a countdown and sounds a buzzer after a certa in amount of time has elapsed. You will then change the pitch of a buzzer by tilting an accelerometer, while displaying the frequency of the  pitch on the LCD screen. Finally, you will use the accelerometer and LCD screen to simulate a spirit-level indicator. Pa r t 1: Wi r e le ss Com m uni ca tion Usi ng XB e e Mo d ule s a nd t he Ar d uino Figure 1 - Transmitter and Receiver Modules 

Upload: shereen-lina

Post on 14-Jul-2015

25 views

Category:

Documents


0 download

TRANSCRIPT

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 1/17

University of Pennsylvania

Department of Electrical and Systems Engineering

ESE 205 – Electrical Circuits and Systems Laboratory I

Lab 3 – XBees and LCDs and Accelerometers, Oh My!

Introduction: 

In the first part of this lab, you will learn how to use two XBee modules to transmit and receiveinformation wirelessly. XBee is a brand of low-power radio transmitter/receiver based on the

ZigBee digital communication protocol. First, 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 a servo left

or right 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. This is similar to what

you did in the last part of Lab 1, except this time, it’s WIRELESS! 

In the second part of this lab, you will learn how to use a LCD (liquid crystal display) screen for

text display and other interesting applications. You will first create a timer that shows acountdown and sounds a buzzer after a certain amount of time has elapsed. You will then

change the pitch of a buzzer by tilting an accelerometer, while displaying the frequency of the

pitch on the LCD screen. Finally, you will use the accelerometer and LCD screen to simulate a

spirit-level indicator.

 Part 1: Wireless Communication Using XBee Modules and the Arduino

Figure 1 - Transmitter and Receiver Modules 

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 2/17

Goal: 

-  To control the motion of a continuous rotation Parallax motor using Arduino

communication via XBee.

-  To control the lighting of an LED with a photoresistor using Arduino communication

via XBee.

Parts Required

1.  2 Arduino Boards

2.  2 USB Cables3.  2 XBee Shields4.  2 XBee Modules

5.  2 Extension Shields

6.  Wires

7.  Photoresistor

8. 

Two (2) 1 k 

resistors9.  LED

For this part of the lab, you will be paired up with another group since you must use two Arduino

boards. Your group will be assigned two XBee modules and two XBee Arduino shields. Each

pair of XBee modules is configured to communicate on a unique channel, so do not accidentally

switch one of your XBees with another group.

 Remote  control of a continuous rotation Parallax motor using XBee 

Procedure:

One group (2 people) should set up the receiver (parts a through e) while the other group sets up

the transmitter (parts f through g). You will switch roles for the next section.

Receiver Module

a. Connect the XBee Shield and set the Receiver Arduino module in USB Programming mode

Connect the XBee Shield to the Arduino Board as shown in Figure 1 (or Figure 3). Remove the jumpers

to set the XBee module in USB programming mode. Please do not lose the jumpers (shown below)!

You will need them again in the next step. 

Figure 2 - Jumpers (DO NOT LOSE!)  

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 3/17

 Figure 3 - Arduino XBee Shield in USB Programming Mode

b. Compile and download the working code to the Receiver Arduino module

#include <Servo.h>

const int ledPin = 13; // the pin that the LED is attached to

const int servoPin = 12; // the pin that the +5V of servo is attached to

int incomingByte; // a variable to read incoming serial data into

Servo myservo; // create servo object to control a servo

void setup() {

 // initialize serial communication:

Serial.begin(9600);

 // initialize the LED pin as an output:

 pinMode(ledPin, OUTPUT);

 // initialize the SERVO pin as an output:

 pinMode(servoPin, OUTPUT);

 // set servoPin to +5V 

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 4/17

digitalWrite(servoPin, HIGH);

 // Pin 10 of the Arduino output is connected to the PWN input of the servo motor 

myservo.attach(10);

 }

void loop() {

 // see if there's incoming serial data:

if (Serial.available() > 0) {

 // read the oldest byte in the serial buffer:

incomingByte = Serial.read();

 // if it's a capital L (ASCII 72), turn on the LED and rotate the servo motor clockwise

if (incomingByte == 'L') {

digitalWrite(ledPin, HIGH);

myservo.write(0);

 }

 // if it's an R (ASCII 76) turn off the LED and rotate the servo motor anti-clockwise

if (incomingByte == 'R') {

digitalWrite(ledPin, LOW);

myservo.write(180);

 }

 }

 }

d. Set the Receiver Arduino module in XBee mode

Connect the jumpers to the XBee shield, as shown in Figure 4, to set it on XBee mode. This will be the

receiver Arduino module.

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 5/17

 

Figure 4 - Arduino XBee Shield in XBee Mode

e. Connect one of the Boe- Bot’s servos to the receiver Arduino module as shown in Figure 5.

Figure 5 - Continuous Rotation Parallax Motor

 Note: You are using one of the servos on your Boe-Bot platform, not the servo pictured above.

Transmitter Module

 f. Follow step ‘a’  to set the Transmitter Arduino module in USB Programming mode

g. Compile and download the working code to the Transmitter Arduino module

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 6/17

void setup()

{

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

 }

void loop()

{

Serial.print('L');

delay(1000);

Serial.print('R');

delay(1000);

 }

h. Set the Transmitter Arduino module in XBee mode as in step ‘d’, by connecting the jumper wires

i. Observe the rotation of the servo motor 

In the Arduino window containing the code for the transmitter, open the serial monitor by

  pressing the “Serial Monitor” button to the right of the “Upload” button. You should seealternating L’s and R’s appear every second. The servo motor attached to the receiver module

will turn clockwise and anticlockwise based on the serial data transmitted from the transmittermodule. The LED on the receiver module should blink on and off.

 Remote  control of an LED using a photoresistor circuit and XBee

Procedure:

The group (2 people) that set up the receiver in the previous part should now set up the

transmitter, and the group that set up the transmitter last time should set up the receiver.

This part requires you to construct a circuit on both the receiving and transmitting Arduinoboards. 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 breadboardshield, 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 beused 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 6.

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 7/17

 Figure 6 - Configuration of Arduino Shields on Extension Shield

Receiver Module

a.  Construct the circuit given by the schematic in Figure 7.

Figure 7 - Receiver LED circuit 

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

 your window.

const int ledPin = 12; // the pin that the LED is attached to

int incomingByte; // a variable to read incoming serial data into

void setup() {Serial.begin(9600); // initialize serial communication

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

 }

void loop() {

 // see if there's incoming serial data:

if (Serial.available() > 0){

Transmitter Receiver

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 8/17

 // read the oldest value in the serial buffer:

incomingByte = Serial.read();

if(incomingByte == 'D'){

digitalWrite(ledPin, HIGH);

 }

else if(incomingByte == 'L'){digitalWrite(ledPin, LOW);

 }

 }

 }

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

Transmitter Module

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

Figure 8 - Transmitter voltage divider circuit 

e.  Set the Arduino in USB Programming mode (remove jumpers) and 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

 }

void loop(){

value = analogRead(analogPin);

if(value >= 150){ // you may need to modify this argument 

Serial.print('L');

delay(500);

 }

if(value < 150){

Serial.print('D');

delay(500);

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 9/17

}

 }

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

g.   Demonstrate to a TA that your system works!

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?

Figure 9 - Electrocardiograph

(http://www.instrumentalanalysis.com/en/projects/fv_fum.php) 

 Part 2: Applications of LCD Screen and Accelerometer

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 10/17

Parts Required:

1.  Arduino Board

2.  USB Cable

3.  Sound Buzzer4.  16X2 LCD Display

5.  ADXL335, 3-axis Accelerometer

6.  Wires

Self-Timer using the Arduino

Procedure:

a.  Building the circuit 

Attach the LCD screen shield to the Arduino, and place the breadboard shield on top, as shown

in Figure 10. Build the sound buzzer circuit as described in Figure 11.

Figure 10 - Arduino Shield Configuration

Figure 11 - Sound Buzzer Circuit

b. Compile and download the following working code to the Arduino Board using Arduino IDE.

6

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 11/17

#include <LiquidCrystal.h>

 // initialize the library with the numbers of the interface pins

 LiquidCrystal lcd(12, 11, 13, 10, 9, 8);

int runTimer = 1; // true condition for timer 

int serialData = 0; // false condition for serial communication

int speakerPin = 6;

int data = 0; // default condition

void setup() {

 pinMode(speakerPin, OUTPUT);

 // set up the LCD's number of rows and columns:

lcd.begin(16, 2);

 }

void loop() {

 // To execute timer only once

if(runTimer == 1){

 // Print a message to the LCD.

lcd.clear();

lcd.print("Timer: ");

 //Start timer 

timer(); // runs the timer code below, under void timer()

 }

runTimer = 0;

lcd.noDisplay();

delay(250);

 // Sound Buzzer 

 for(int duration = 0; duration < 100; duration ++){

digitalWrite(speakerPin, HIGH);

delayMicroseconds(2000);

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 12/17

digitalWrite(speakerPin, LOW);

delayMicroseconds(2000);

 }

lcd.display();

delay(250);

 }

void timer(){

 // For loop to run the COUNT-DOWN in Seconds

 for(int timer = 10; timer > 0; --timer){

 // Set the Cursor to the space after the display "TIMER: "

if(timer >= 10)

lcd.setCursor(6,0);

else{

lcd.setCursor(6,0);

lcd.print("0");

lcd.setCursor(7,0);

 }

 // Display the COUNT-DOWN Seconds

lcd.print(timer);

lcd.print("s");

delay(1000);

 }

 // Bring the Cursor to the initial position

lcd.setCursor(0,0);

lcd.clear();

lcd.print("Buzzer!");

 }

c. Self-Timer and Sound Buzzer 

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 13/17

Press the RESET Button of the Arduino board, the timer will countdown from 10 seconds, asprogrammed. Once the timer countdown reaches 0s, the buzzer will go on and the LCD display

will blink “Buzzer!” 

The program is reset every time you press the RESET Button of the Arduino board and the timercountdown begins again.

d. Questions

i.  Can you make the timer countdown from 100 seconds?

ii.  Can you make the buzzer buzz only for a fixed amount of time after the timer countdownreaches 0s? Show a TA! 

Possibly helpful reference links:http://arduino.cc/en/Reference/For  

http://arduino.cc/en/Reference/While  

 Pitch-Control Using an Accelerometer and the Arduino

Procedure:

a. 3-Axis Accelerometer 

An accelerometer is used to measure the acceleration experienced by an object. The ADXL335

(Figure 12) is adopted to measure the acceleration experienced by the object in motion with

respect to the X or Y or Z axis. We will only be measuring movements to the Y axis in this lab.

Figure 12 - ADXL335, 3-axis Accelerometer 

b. Interface the 3-Axis Accelerometer 

Connect the 3-Axis accelerometer to the Arduino shield as shown in Figure 13. Keep your

buzzer circuit from the previous part connected.

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 14/17

 

Figure 13 – Interfacing ADXL335, 3-Axis Accelerometer, with the Arduino Board

c. Copy the following code into the Arduino IDE (the code will not compile correctly until you

change *** with numbers):

#include <LiquidCrystal.h>

 // initialize the library with the numbers of the interface pins

 LiquidCrystal lcd(12, 11, 13, 10, 9, 8);

const int groundpin = 14; // analog input pin0 -- ground 

const int powerpin = 18; // analog input pin4 -- voltage

const int buzzerPin = 6; // digital input pin6 -- buzzer 

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

int yvalue;

int freq;

int ymin = ***; // Replace with measured minimum yvalue

int ymax = ***; // Replace with measured maximum yvalue

void setup() {

 pinMode(buzzerPin, OUTPUT);

 pinMode(groundpin, OUTPUT);

 pinMode(powerpin, OUTPUT);

digitalWrite(groundpin, LOW);

digitalWrite(powerpin, HIGH);

 // set up the LCD's number of rows and columns:

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 15/17

lcd.begin(16, 2);

 }

void loop() {

 yvalue = analogRead(ypin);

 freq = map(yvalue, ymin, ymax, 100, 10000); // maps yvalue into frequency range

tone(buzzerPin, freq); // sounds buzzer at given frequency

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Freq: ");

lcd.print(freq);

lcd.print(" Hz");

lcd.setCursor(0,1);

lcd.print("yvalue: ");

lcd.print(yvalue);

delay(150);

 }

d. Find the range of the y-axis acceleration

Find the minimum and maximum values of the y-axis acceleration ( yvalue). Replace the valuesof  ymin and ymax with the values you found. Upload the code to the Arduino and tilt the board

along the y-axis to observe its effect.

The map function map(value, fromLow, fromHigh, toLow, toHigh)  maps a value of  fromLow to

toLow, a value of  fromHigh to toHigh, values in-between to values in-between, etc. In this

case,  yvalue is converted from an acceleration value to a frequency value. To get more

information about any function, right-click the function name and click “Find in Reference.”

e. Questions

i.  If  yvalue is 450, then what is the value of  freq?ii.  How would you change the range of frequencies that is swept through?

iii.  Can you modify the code so that change of pitch sounds more continuous?iv.  Change the code so that the change in frequency correlates with acceleration on the x-

axis instead of the y-axis? Show a TA! 

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 16/17

Spirit-Level Indicator using the Arduino

Procedure:

a. Compile and download the working code to the Arduino Board:

#include <LiquidCrystal.h>

 LiquidCrystal lcd(12, 11, 13, 10, 9, 8);

const int groundpin = 14; // analog input pin0 -- ground 

const int powerpin = 18; // analog input pin4 -- voltage

const int ypin =2; // y-axis of the accelerometer 

void setup() {

lcd.begin(16, 2);

Serial.begin(9600);

 pinMode(groundpin, OUTPUT);

 pinMode(powerpin, OUTPUT);

digitalWrite(groundpin, LOW);

digitalWrite(powerpin, HIGH);

 }

void loop() {

int avalue = 0;

int lcd_Cursor_Position = 0;

lcd.clear();

avalue = analogRead(ypin); // read value of the X-axis acceleration

lcd_Cursor_Position = 46 - avalue/13; // calculation to position the lcd cursor 

Serial.print(avalue); // prints x-axis acceleration in serial monitor 

lcd.setCursor((15 - lcd_Cursor_Position), 1);

lcd.print('.');

lcd.setCursor((15 - lcd_Cursor_Position), 0);

lcd.print('.');

delay(100);

 }

5/12/2018 ESE205Lab3 - slidepdf.com

http://slidepdf.com/reader/full/ese205lab3 17/17

b. Questions:

v.  Make the spirit level indicator ‘.’ move in the direction of the acceleration.

vi.  Make one spirit level indicator ‘.’ move in the opposite direction to the other.

Show a TA! vii.  Extra Credit: Using a sound buzzer, play any musical note, if the level indicator is

stationary in a particular position for more than 10 seconds. Show a TA!