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.

Arduino Clap Switch Project for Beginners

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 →

Building the Clap Switch Arduino Project for Beginners

Hardware Requirements

The following hardware is required to build the Arduino clap switch project:

  • Electret microphone
  • 100n capacitor
  • 10k resistor
  • 100k resistor
  • 220 ohm to 470 ohm resistor
  • LED
  • Arduino Uno or similar Arduino board
  • Electronic breadboard and jumper wires

Books that may interest you:

C Programming with Arduino Book Ultimate Arduino MEGA 2560 Hardware Manual Ultimage Arduino Uno Hardware Manual

Clap Switch Breadboard Layout and Circuit Diagram

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).

Arduino Clap Switch Project Breadboard Layout
Arduino Clap Switch Project Breadboard Layout
Arduino Clap Switch Project Circuit Diagram
Arduino Clap Switch Project Circuit Diagram

Arduino Clap Switch Sketch Code

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
  }
}

How the Circuit Works

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.

New Book: Explore ATtiny Microcontrollers using C and Assembly Language

Explore ATtiny Microcontrollers using C and Assembly Language book Ultimate Arduino Uno Hardware Manual

As an Amazon Associate I earn from qualifying purchases: