Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.
Created on: 6 April 2016
Clapping hands together switches an LED on or off in this Arduino breadboard project for beginners. The sound made by clapping is detected by an electret microphone connected to one of the Arduino analog input pins.
The first time that the clap is detected by the Arduino, an LED will be switched on. The second time that a clap is detected the LED will be switched off. This is an easy and fun project for beginners and kids.
The following video shows the Arduino clap switch project being operated.
Can't see the video? View on YouTube →
The following hardware is required to build the Arduino clap switch project:
Build the circuit as shown in the breadboard layout and circuit diagram below. The negative or GND pin of the electret microphone is the pin that connects to the body of the microphone.
A resistor with value between 220 ohm and 470 ohm is used in series with the LED (R3 in the circuit diagram).
After building the above circuit, load the following sketch to the Arduino. Open the Arduino serial monitor window as shown in the video to see what the value is that is read from the analog pin. This value is what is being checked in the sketch to trigger the clap switch and switch the LED. The check is done in the following line of code.
if (analog_val > 10) { // trigger threshold
The value used in this check can be changed if the switch is too sensitive or not sensitive enough.
//--------------------------------------------------------------------- // Program: clap switch // // Description: Switches on an LED when hands are clapped. // Electret microphone connected to A2, LED and series // resistor connected to digital pin 2 // // Date: 6 April 2016 Author: W.A. Smith // http://startingelectronics.org //--------------------------------------------------------------------- void setup() { Serial.begin(9600); // using serial port to check analog value pinMode(2, OUTPUT); // LED on digital pin 2 } void loop() { int analog_val; // analog value read from A2 static bool led_state = false; // current state of LED analog_val = analogRead(A2); if (analog_val > 10) { // trigger threshold // toggle LED if (led_state) { led_state = false; // LED was on, now off digitalWrite(2, LOW); Serial.println(analog_val); // print analog value for debug purposes } else { led_state = true; digitalWrite(2, HIGH); // LED was off, now on Serial.println(analog_val); } delay(50); // wait for clap noise to subside } }
The electret microphone needs resistor R1 to supply voltage to the transistor inside the microphone. C1 stops any d.c. voltage from reaching the Arduino analog pin and only allows the audio voltage from the microphone through.
R2 pulls the Arduino analog pin down to GND to ensure that an analog value of zero is read when the microphone is not picking up the sound of a clap. The analog input would otherwise be floating at some value and picking up noise from the microphone.
When someone claps, the microphone picks up the sound and a tiny voltage reaches the analog pin which is read by the sketch running on the Arduino. If the value read from the analog pin is big enough, the LED will be toggled from off to on or on to off.
This is a very easy project for beginners to build. Some improvements could be made by building a circuit to amplify the microphone and feeding the amplified signal to the Arduino. The sensitivity of the microphone could then be adjusted by adjusting the gain of the amplifier.
As an Amazon Associate I earn from qualifying purchases: