7. Let’s get to coding!
Buzzer Alarm with Light
int ledPin = 13; // LED connected to pin 13
int tonePin = 8; // buzzer alarm connected
to pin 8
void setup()
{
pinMode(ledPin, OUTPUT); // sets the buzzer alarm as
output
}
8. Let’s get to coding!
void loop() // run over and over again forever
{
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the
voltage level)
tone (tonePin, 200, 250); // play a tone on tone pin with 200
Hz and 250 ms
delay(500); // wait for 0.5 s
digitalWrite(ledPin, LOW); // turn the LED off (LOW is the
voltage level)
tone (tonePin, 600, 250); // Play a tone on tone pin with 600
Hz and 250 ms
delay(500); // wait for 0.5 s
}
9. Uploading the Sketch
Step 1: Click the Verify/Compile button and check for errors in
the code.
Step 2: Click the Upload button.
10. Let’s think!
1. What happens in the circuit after the code was uploaded?
2. Change the value highlighted in yellow to a higher value, say
500. Compile/Verify the program before uploading the program
again. What happens?
3. Change the value highlighted in yellow to a lower value, say
100. Compile/Verify the program before uploading the program
again. What happens?
11. Let’s think!
1. Change the circled number to a lower value, say 100.
Compile/Verify the program before uploading the program
again. What happens?
The first beep is at lower pitch.
2. Try changing the underlined value to a higher value, say 600.
Compile/Verify the program before uploading the program
again. What happens?
The first beep remains on for a longer time.(600 ms)
12. Challenge
Listen to the alarm played by the educator. Can you create
something similar to that?
13. What have you learnt?
• The tone function in a sketch
• The function of the piezo buzzer
• Relationship between frequency and sound