UNIVERSITY OF PENNSYLVANIA DEPARTMENT OF ELECTRICAL ENGINEERING ESE UNDERGRADUATE LABORATORY ESE 205: Electrical Circuits and Systems I Laboratory Self-timer and Musical Notes using the Arduino Board Goal: 1. To use the Arduino Board as a self-timer. a. The LCD display interfaced to the Arduino board should display the countdown time. b. The buzzer should go off when the timer countdown reaches zero. 2. To play musical notes using the buzzer interfaced with the Arduino board. 3. Extra Credit: Use the self timer mode to play musical notes. Parts Required 1. 2. 3. 4. 5. Arduino Board USB Cable Sound Buzzer (32S4120) 16X2 LCD Display Wires Procedure: a. Build the sound buzzer circuit as shown in Figure 1 Figure 1: Sound Buzzer Circuit Figure 2: Arduino Board Page 1 of 6 b. Compile and download the following working code to the Arduino Boardusing Arduino IDE #include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); int runTimer = 1; // true condition for timer int serialData = 0; // false condition for serial communication int speakerPin = 8; 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(); } runTimer = 0; lcd.noDisplay(); delay(250); // Sound Buzzer for(int duration = 0; duration < 100; duration ++){ digitalWrite(speakerPin, HIGH); Page 2 of 6 delayMicroseconds(2000); 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 Curosr 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!"); } Page 3 of 6 c. Timer Display In order to display the timer you will have to wire the 16x2 LCD display (Figure 3) to 6 Digital Output pins of the Arduino board. Wire the pins according to the following assignments: LCD 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Arduino GND +5V GND 12 GND 11 Board - - - 5 4 3 2 16 +5V GND Figure 3: 16x2 Liquid Crystal Display d. Self-Timer and Sound Buzzer Press the RESET Button of the Arduino board, the timer will countdown from 10 seconds, as programmed. 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 timer countdown begins again. e. Questions i. ii. How will you make the timer countdown from 100 seconds? How will you make the Buzzer buzz only for a fixed amount of time after the timer countdown reaches 0s? Answer the above questions to your TA and demo your circuit. Page 4 of 6 Figure 4: Timer Countdown Display of Selftimer using Arduino Board Figure 5: Buzzer Display of Selftimer using Arduino Board Page 5 of 6 f. Play Melody Use the sound buzzer (32S4120) to produce musical notes. Refer to the following links Musical Note Frequencies from TechLib.com http://www.techlib.com/reference/musical_note_frequencies.htm Note: there are 5 columns in the matrix – these refer to difference octaves. For the purposes of this assignment, choose your favorite octave (preferably one near the middle) and go with it. (ignore sharps (#) and just go with plain A B C, etc) Music Theory & Note Reading from ThinkQuest.org http://library.thinkquest.org/15413/theory/note-reading.htm Note: we will assume a whole note is 1 second long Program the Arduino board using the code available from the following link: http://www.arduino.cc/en/Tutorial/Melody Note: Arduino is open source. The Arduino website provides links to various other interesting projects. g. Extra Credit i. Program the Arduino to play three different musical notes. Each musical note gets 5 points. ii. Program the Arduino to count down from 20s and play a musical note of your choice for two cycles. (5 points) Page 6 of 6