Double-clap light switch
Clap twice and the lights come on. Clap twice again to switch them off.
Shop by category
Shop all componentsPlay recognisable tunes on a passive buzzer by sending it specific frequencies. Pitch is frequency measured in hertz, how many times per second the buzzer's disc is pushed and pulled. Middle C is 262 Hz, and doubling any frequency raises it exactly one octave, which is why 523 Hz is the C above. tone() generates that signal in the background so the sketch can carry on, and noTone() stops it. A tune is then just two arrays walked in step: one of pitches and one of how long each note lasts.
Every part for this build comes in the Geekobot Explorer Kit box, a Geekobot exclusive, together with the parts for all 61 projects, from a first blinking LED to a smart fan and a reaction game. The wiring below is the kit's own wiring.
Tick anything you already have and the total updates.
The brain of every Explorer Kit project
Male-to-female and female-to-female leads for the modules
Nano I/O shield from the kit: every pin has its own S, V, G header, so modules plug in with jumpers
Follow the colours: red is power, dark grey is ground, blue is a signal.
explorer_kit_09_musical_buzzer.inoVerify & upload in Electrolarkint buzzer = 8;
int button = 2;
// Note frequencies in hertz
int NOTE_C4 = 262, NOTE_D4 = 294, NOTE_E4 = 330, NOTE_F4 = 349;
int NOTE_G4 = 392, NOTE_A4 = 440, NOTE_B4 = 494, NOTE_C5 = 523;
// Twinkle Twinkle Little Star
int melody[14] = {NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4};
int beats[14] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2};
int beatLength = 400;
void playTune()
{
for (int i = 0; i < 14; i++)
{
int duration = beats[i] * beatLength;
tone(buzzer, melody[i], duration * 0.9);
Serial.print("Note ");
Serial.print(melody[i]);
Serial.println(" Hz");
delay(duration); // Full beat, including the short gap
}
noTone(buzzer);
}
void playScale()
{
int scale[8] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5};
for (int i = 0; i < 8; i++)
{
tone(buzzer, scale[i], 250);
delay(300);
}
noTone(buzzer);
}
void setup()
{
pinMode(buzzer, OUTPUT);
pinMode(button, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Press the button to play");
playScale();
}
void loop()
{
if (digitalRead(button) == LOW)
{
Serial.println("Playing the tune");
playTune();
while (digitalRead(button) == LOW)
{
delay(10);
}
delay(300);
}
}
Use the kit's mini-USB cable, install the CH340 driver on Windows if no port appears, pick Tools > Board > Arduino Nano and try Tools > Processor > ATmega328P (Old Bootloader) if the upload times out.
The sketch uses INPUT_PULLUP, so one leg goes to the pin and the other to GND. Use two legs on opposite sides of the button.
This project needs the passive buzzer (the one that plays notes with tone()), not the active one.
Pitch is frequency measured in hertz, how many times per second the buzzer's disc is pushed and pulled. Middle C is 262 Hz, and doubling any frequency raises it exactly one octave, which is why 523 Hz is the C above. tone() generates that signal in the background so the sketch can carry on, and noTone() stops it. A tune is then just two arrays walked in step: one of pitches and one of how long each note lasts.
A rising eight-note scale plays when the board starts. Press the button and Twinkle Twinkle Little Star plays. Serial Monitor shows the frequency of each note.
Yes. It is project 9 of 61 in the Geekobot Explorer Kit, in level 1 (Foundations). Every part on the list comes in the kit box, and you can also buy the parts one by one from the list above.
No. Every Explorer Kit project is wired on the Nano I/O shield and the breadboard with jumper wires.
6 parts: Generic Nano CH340 Development Board (Soldered), USB Cable, Dupont Jumper Wires (40 pcs), Tactile Push Button Switch, Buzzer Module 3.3–5V and Arduino Nano IO Expansion Shield (328P Compatible). On this page you can tick the parts you already have and add only the rest to your cart.
It is a beginner project and takes about 30 minutes. The build on this page has 5 steps.
No. This page has the complete, tested code (explorer_kit_09_musical_buzzer.ino).
About ₹445 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Musical notes as frequencies; tone() and noTone() for pitch; Parallel arrays for pitch and rhythm; Why doubling a frequency gives the same note an octave higher.
Clap twice and the lights come on. Clap twice again to switch them off.
Plays a friendly ding-dong whenever someone walks into the shop or room.
Beeps and flashes any message in Morse, or key your own by hand.
Turn a membrane keypad into a two-octave piano.
Lessons are free to read. Maker (₹499/month) and Pro add quizzes, worked problems and certificates, paid with your Geekobot account. See plans
Learning project. Check every circuit and the datasheet of each part before you power it up.