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.
The ESP32 is a 3.3V board: never connect a 5V signal straight to a GPIO pin.
Every part for this build comes in the Geekobot Titan Kit box, a Geekobot exclusive, together with the parts for all 120 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.
ESP32 DevKit V1 (38-pin): the brain of every Titan Kit project, with Wi-Fi and Bluetooth
Male-to-female and female-to-female leads for the modules
ESP32 expansion board from the kit: every GPIO has its own S, V, G header
Follow the colours: red is power, dark grey is ground, blue is a signal.
titan_kit_009_musical_buzzer.inoVerify & upload in Electrolarkint buzzer = 33;
int button = 13;
// 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 a micro-USB data cable (some cables only charge), install the CP210x or CH340 driver if no port appears, pick Tools > Board > ESP32 Dev Module and hold the BOOT button on the board until the upload starts.
The sketch uses INPUT_PULLUP, so one leg goes to the GPIO 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), 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 120 in the Geekobot Titan 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 Titan Kit project is wired on the ESP32 expansion board and the breadboard with jumper wires.
7 parts: ESP32-WROOM-32 38-Pin Development Board, USB Cable, Solderless Breadboard, Dupont Jumper Wires (40 pcs), Tactile Push Button Switch, Buzzer Module 3.3–5V and ESP32 38-Pin Expansion Board. 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 20 minutes. The build on this page has 5 steps.
No. This page has the complete, tested code (titan_kit_009_musical_buzzer.ino).
About ₹712 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.
The ESP32 is a 3.3V board: never connect a 5V signal straight to a GPIO pin.
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.