laboratory 5: buzzer and ldr - faculty websites · pdf filelight dependent resistor (ldr) ......

14
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 1 EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE LABORATORY 5: BUZZER AND LDR DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING UNIVERSITY OF NEVADA, LAS VEGAS GOAL: This section will familiarize the users with creating tones and sounds using the Arduino UNO and Danger Shield. OBJECTIVES: Learn the basics of Arduino Programming o Commands: setup() loop() pinMode() digitalWrite() delay() digitalRead() digitalWrite() analogRead() analogWrite() tone() Arrays Conditionals Create and modify melodies through the Arduino and Buzzer Create a tone generated from the LDR value OVERVIEW AND REQUIREMENTS: Arrays An array is a collection of variables that are accessed with an index number. Arrays in the C programming language, on which Arduino is based, can be complicated, but using simple arrays is relatively straightforward. There are several ways to create an array in Arduino. First you can create an array directly by declaring its size:

Upload: lamhanh

Post on 09-Mar-2018

241 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 1

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

LABORATORY 5: BUZZER AND LDR

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING UNIVERSITY OF NEVADA, LAS VEGAS

GOAL:

This section will familiarize the users with creating tones and sounds using the Arduino UNO

and Danger Shield.

OBJECTIVES:

Learn the basics of Arduino Programming

o Commands:

setup()

loop()

pinMode()

digitalWrite()

delay()

digitalRead()

digitalWrite()

analogRead()

analogWrite()

tone()

Arrays

Conditionals

Create and modify melodies through the Arduino and Buzzer

Create a tone generated from the LDR value

OVERVIEW AND REQUIREMENTS: Arrays

An array is a collection of variables that are accessed with an index number. Arrays in the C

programming language, on which Arduino is based, can be complicated, but using simple arrays

is relatively straightforward. There are several ways to create an array in Arduino. First you

can create an array directly by declaring its size:

Page 2: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 2

Datatype array_name [array_size]

Another method is to creating an array by entering its elements directly into the array:

Datatype array_name[] = {element_1, element_2, element_3…element_x}

You can also combine the two methods.

Datatype array_name[array_size] = {element_1, element_2,

element_3….element_arraysize}

Once you have defined your array you can access it to change or modify any of the elements.

Consider the following example:

int myArray[5] = {2, 4, 6, 8 ,10}

The above code creates an integer array named “myArray” that can contain 5 elements. The

code also declares what those elements are. To access the array you need to only list the index

value of the element you want.

x = myArray[0];

myArray[4] = 7;

From the above code the value of x will be 2 and myArray will change to {2, 4, 6, 8, 7}. As you

probably noticed the first element of the array is accessed by an index of 0 instead of 1 and the

last element is accessed by a 4 instead of a 5. This is because arrays in Arduino are zero

indexed. This means that the first index of the array starts at zero instead of 1.

Light Dependent Resistor (LDR)

A photoresistor or light dependent resistor (LDR) is a resistor whose resistance decreases with

increasing incident light intensity. A photoresistor is made of a high resistance semiconductor.

If light falling on the device is of high enough frequency, photons absorbed by the

semiconductor give bound electrons enough energy to jump into the conduction band. The

resulting free electron conduct electricity, thereby lowering resistance.

Page 3: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 3

COMPONENTS:

Arduino Uno

USB A-B Cable

Danger Shield

Host PC

Installed Arduino Uno drivers and IDE

OPERATION:

Buzzer with LDR

1. Attach the Danger Shield to the Arduino, making sure to properly align the pins. If you

are using an R3 revision of the Arduino UNO, there will be 2 pins on each side that will

have no corresponding pins on the shield.

Page 4: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 4

2. Attach the Arduino UNO to the host PC with the use of the USB cable.

3. Open the Arduino IDE and create a new sketch titled “Buzzer_with_LDR”. Make sure

that the correct COM port is in use.

4. Verify and upload the following sketch to your Arduino UNO (Code 1):

int photosensorPin = 3;

int Buzzer = 3;

int val = 0;

void setup() {

pinMode(Buzzer, OUTPUT);

Serial.begin(9600);

}

void loop() {

digitalWrite(Buzzer, LOW);

val = analogRead(photosensorPin);

val = val/2;

Serial.println(val);

for( int i=0; i<500; i++ ) { // play it for 50 cycles

digitalWrite(Buzzer, HIGH);

delayMicroseconds(val);

digitalWrite(Buzzer, LOW);

delayMicroseconds(val);

}

}

Page 5: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 5

5. The LDR creates an analog value that changes based on how much light it sense. By

moving your finger further and closer to the LDR, the tone should change.

Tone Generation

1. Attach the Danger Shield to the Arduino, making sure to properly align the pins. If you

are using an R3 revision of the Arduino UNO, there will be 2 pins on each side that will

have no corresponding pins on the shield.

2. Attach the Arduino UNO to the host PC with the use of the USB cable.

3. Open the Arduino IDE and create a new sketch titled “DoReMi”. Make sure that the

correct COM port is in use.

4. Verify and upload the following sketch to your Arduino UNO (Code 2):

#define NOTE_B0 31

#define NOTE_C1 33

#define NOTE_CS1 35

#define NOTE_D1 37

#define NOTE_DS1 39

#define NOTE_E1 41

#define NOTE_F1 44

#define NOTE_FS1 46

#define NOTE_G1 49

#define NOTE_GS1 52

#define NOTE_A1 55

#define NOTE_AS1 58

#define NOTE_B1 62

#define NOTE_C2 65

#define NOTE_CS2 69

#define NOTE_D2 73

#define NOTE_DS2 78

#define NOTE_E2 82

#define NOTE_F2 87

#define NOTE_FS2 93

#define NOTE_G2 98

#define NOTE_GS2 104

#define NOTE_A2 110

#define NOTE_AS2 117

#define NOTE_B2 123

#define NOTE_C3 131

#define NOTE_CS3 139

#define NOTE_D3 147

#define NOTE_DS3 156

#define NOTE_E3 165

#define NOTE_F3 175

#define NOTE_FS3 185

#define NOTE_G3 196

#define NOTE_GS3 208

#define NOTE_A3 220

#define NOTE_AS3 233

Page 6: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 6

#define NOTE_B3 247

#define NOTE_C4 262

#define NOTE_CS4 277

#define NOTE_D4 294

#define NOTE_DS4 311

#define NOTE_E4 330

#define NOTE_F4 349

#define NOTE_FS4 370

#define NOTE_G4 392

#define NOTE_GS4 415

#define NOTE_A4 440

#define NOTE_AS4 466

#define NOTE_B4 494

#define NOTE_C5 523

#define NOTE_CS5 554

#define NOTE_D5 587

#define NOTE_DS5 622

#define NOTE_E5 659

#define NOTE_F5 698

#define NOTE_FS5 740

#define NOTE_G5 784

#define NOTE_GS5 831

#define NOTE_A5 880

#define NOTE_AS5 932

#define NOTE_B5 988

#define NOTE_C6 10476

#define NOTE_CS6 1109

#define NOTE_D6 1175

#define NOTE_DS6 1245

#define NOTE_E6 1319

#define NOTE_F6 1397

#define NOTE_FS6 1480

#define NOTE_G6 1568

#define NOTE_GS6 1661

#define NOTE_A6 1760

#define NOTE_AS6 1865

#define NOTE_B6 1976

#define NOTE_C7 2093

#define NOTE_CS7 2217

#define NOTE_D7 2349

#define NOTE_DS7 2489

#define NOTE_E7 2637

#define NOTE_F7 2794

#define NOTE_FS7 2960

#define NOTE_G7 3136

#define NOTE_GS7 3322

#define NOTE_A7 3520

#define NOTE_AS7 3729

#define NOTE_B7 3951

#define NOTE_C8 4186

#define NOTE_CS8 4435

#define NOTE_D8 4699

#define NOTE_DS8 4978

int melody[] = {

Page 7: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 7

NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5,

NOTE_C6};

// note durations: 4 = quarter note, 8 = eighth note, etc.:

int noteDurations[] = {

8, 8, 8, 8, 8, 8, 8, 8};

void setup() {

// iterate over the notes of the melody:

for (int thisNote = 0; thisNote < 8; thisNote++) {

// to calculate the note duration, take one second

// divided by the note type.

//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

int noteDuration = 1000/noteDurations[thisNote];

tone(3, melody[thisNote],noteDuration);

// to distinguish the notes, set a minimum time between them.

// the note's duration + 30% seems to work well:

int pauseBetweenNotes = noteDuration * 2.30;

delay(pauseBetweenNotes);

// stop the tone playing:

noTone(3);

}

}

void loop() {

// no need to repeat the melody.

}

5. The results should be the scale playing through the code.

6. Modify the code so that the pause between each note is longer. Demonstrate the results

to your instructor.

7. Modify the code so that the scale goes in reverse. Demonstrate the results to your

instructor.

Mario Theme

1. Attach the Danger Shield to the Arduino, making sure to properly align the pins. If you

are using an R3 revision of the Arduino UNO, there will be 2 pins on each side that will

have no corresponding pins on the shield.

2. Attach the Arduino UNO to the host PC with the use of the USB cable.

3. Open the Arduino IDE and create a new sketch titled “MarioTheme”. Make sure that the

correct COM port is in use.

4. Verify and upload the following sketch to your Arduino UNO (Code 3):

int melody[] =

Page 8: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 8

{

660,660,660,510,660,770,380,510,380,320,

440,480,450,430,380,660,760,860,700,760,

660,520,580,480,510,380,320,440,480,450,

430,380,660,760,860,700,760,660,520,580,

480,500,760,720,680,620,650,380,430,500,

430,500,570,500,760,720,680,620,650,1020,

1020,1020,380,500,760,720,680,620,650,380,

430,500,430,500,570,585,550,500,380,500,

500,500,500,760,720,680,620,650,380,430,

500,430,500,570,500,760,720,680,620,650,

1020,1020,1020,380,500,760,720,680,620,650,

380,430,500,430,500,570,585,550,500,380,

500,500,500,500,500,500,500,580,660,500,

430,380,500,500,500,500,580,660,870,760,

500,500,500,500,580,660,500,430,380,660,

660,660,510,660,770,380

};

int noteDurations[] =

{

100,100,100,100,100,100,100,100,100,100,

100,80,100,100,100,80,50,100,80,50,

80,80,80,80,100,100,100,100,80,100,

100,100,80,50,100,80,50,80,80,80,

80,100,100,100,100,150,150,100,100,100,

100,100,100,100,100,100,100,150,200,80,

80,80,100,100,100,100,100,150,150,100,

100,100,100,100,100,100,100,100,100,100,

100,100,100,100,100,100,150,150,100,100,

100,100,100,100,100,100,100,100,150,200,

80,80,80,100,100,100,100,100,150,150,

100,100,100,100,100,100,100,100,100,100,

100,100,100,60,80,60,80,80,80,80,

80,80,60,80,60,80,80,80,80,80,

60,80,60,80,80,80,80,80,80,100,

100,100,100,100,100,100

};

int notepause[] ={

150,300,300,100,300,550,575,450,400,500,

300,330,150,300,200,200,150,300,150,350,

300,150,150,500,450,400,500,300,330,150,

300,200,200,150,300,150,350,300,150,150,

500,300,100,150,150,300,300,150,150,300,

150,100,220,300,100,150,150,300,300,300,

150,300,300,300,100,150,150,300,300,150,

150,300,150,100,420,450,420,360,300,300,

150,300,300,100,150,150,300,300,150,150,

300,150,100,220,300,100,150,150,300,300,

300,150,300,300,300,100,150,150,300,300,

150,150,300,150,100,420,450,420,360,300,

300,150,300,150,300,350,150,350,150,300,

150,600,150,300,350,150,150,550,325,600,

150,300,350,150,350,150,300,150,600,150,

300,300,100,300,550,575

Page 9: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 9

};

void setup() {

for (int playnote = 0; playnote < 156; playnote++)

{

int noteduration = noteDurations[playnote];

tone(3, melody[playnote],noteduration);

delay(notepause[playnote]);

}

noTone(3);

}

void loop()

{

}

5. World 1-1.

Page 10: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 10

DEMO AND SCREENSHOTS:

Page 11: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 11

Page 12: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 12

PRELAB:

Explain: what is an Array? What does it mean that Arrays are zero indexed?

EXPERIMENTS:

Experiment 1

1. Use Code 1, test the operation.

2. Modify the code:

a. LED changes brightness instead of the Buzzer tone.

Page 13: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 13

b. Display the values of the LDR in the Serial Monitor

Experiment 2

1. Use Code 2, test the operation

2. Modify the code:

a. Increase the pause between notes twice

b. Play the scale in the reverse order

Experiment 3

1. Use Code 3, test the operation

2. Modify the code

a. Use the slider1 value to control the speed of playing the melody (both duration of

the pauses and tones). Write the value of the slider1 to the Serial Monitor.

b. Use the combined value LDR+slider1 (sum) to control the speed of playing the

melody (both duration of the pauses and tones). Write the value of the slider 1 to

the Serial Monitor.

Experiment 4 (homework)

1. To program this code, either:

a. connect Arduino to your PC (you can use the one that was given to you in the class),

b. use online C++ compiler, such as:

http://www.tutorialspoint.com/compile_cpp_online.php . In this case, use cout

command instead of Serial.println() to print the values. Example:

Place your code in the gray area, leave the rest as it is.

2. Write a short Arduino code that demonstrates your understanding of Arrays. Create a 10-

element array of integers with the values 17 through 26. Read the values from the array

and print them in the Serial Monitor.

3. Use a for loop to add 10 to each element of the array. Read the values from the array and

print them in the Serial Monitor.

#include <iostream>

using namespace std;

int main()

{

int myVal = 5;

cout << myVal << endl;

return 0;

}

Page 14: LABORATORY 5: BUZZER AND LDR - Faculty Websites · PDF fileLight Dependent Resistor (LDR) ... (LDR) is a resistor whose resistance decreases with increasing incident light intensity

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 14

POSTLAB REPORT DELIVERIES Include the following elements in your postlab report:

1. Theory of operation

a. Describe what the array in programming is. How to declare array of floating

numbers?

b. Describe what the LDR is. List 3 practical applications.

2. Results of the experiments

For each experiment, include:

a. The code that you developed for the experiment. Each line that was added must be

highlighted and commented with the explanation of what is its meaning.

b. Brief explanation how the goal of the experiment was reached

c. Screenshots of the serial monitor with the values, presenting the operation of your

code

3. Answer the questions

a. What is the array index?

b. Having the array of integers, representing tones, list two ways to play them in the

reverse order.

c. For the same array, provide a way to play every second tone.

4. Conclusions

a. Write down your conclusions, things learned, problems encountered during the lab

and how they were solved, etc.