Arduino Clap Switch Lamp: Clap Twice to Turn It On (Code)

Arduino ile Alkışla Yanan Lamba Yapımı projesi kapak görseli

🇹🇷 Bu yazıyı Türkçe okuyun

What does this project do?

We build a lamp that turns on when you clap twice and turns off when you clap twice again. The sound sensor sends a signal to the Arduino when it hears a loud sound. The Arduino counts how many claps it hears within one second. It ignores a single clap or a slamming door; it only switches the lamp on or off when it hears two claps.

  • Age group: 9 and up
  • Level: Beginner
  • Time: About 35 minutes
  • What you will learn: using a sound sensor, measuring time with millis(), counting events, keeping an on/off state in a bool variable

New to Arduino? Have a look at the What is Arduino and how does it work? guide first. All projects, sorted from easy to hard, are on the Getting Started with Arduino page.

Parts list

PartQty
Arduino Uno (or compatible)1
KY-038 sound sensor module1
LED (yellow or white)1
220 Ω resistor1
Breadboard1
Male-to-male and male-to-female jumper wires~8
Small screwdriver (for adjusting the sensitivity)1
USB cable1

Wiring

Arduino clap switch lamp with KY-038 sound sensor wiring diagram
ComponentComponent pinArduino pin
KY-038+5V
KY-038GGND
KY-038DOD2
KY-038AONot connected
LEDLong leg (through 220 Ω)D9
LEDShort legGND

Code

// Arduino Clap Switch Lamp - ozandamci.com

const int soundPin = 2;
const int lampPin = 9;

const unsigned long CLAP_GAP = 150;     // don't count the echo of one clap again (ms)
const unsigned long COUNT_TIME = 1000;  // time window for counting claps (ms)

bool lampOn = false;
int clapCount = 0;
unsigned long firstClap = 0;
unsigned long lastSound = 0;

void setup() {
  pinMode(soundPin, INPUT);
  pinMode(lampPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  unsigned long now = millis();

  if (digitalRead(soundPin) == HIGH) {
    // A sound after some silence is a new clap
    if (now - lastSound > CLAP_GAP) {
      if (clapCount == 0) firstClap = now;
      clapCount++;
      Serial.print("Clap: ");
      Serial.println(clapCount);
    }
    lastSound = now;
  }

  // When the time is up, check how many claps were heard
  if (clapCount > 0 && now - firstClap > COUNT_TIME) {
    if (clapCount == 2) {
      lampOn = !lampOn;
      digitalWrite(lampPin, lampOn);
      Serial.println(lampOn ? "Lamp ON" : "Lamp OFF");
    }
    clapCount = 0;
  }
}

No Arduino? Try this circuit in your browser with the Wokwi simulation → (free, nothing to install). In the simulation, a button stands in for the sound sensor: each press is a clap.

Step-by-step instructions

  1. Prepare the power rails. Connect the Arduino’s 5V pin to the + rail of the breadboard and the GND pin to the – rail.
  2. Connect the sound sensor. Connect the sensor’s + pin to the + rail, the G pin to the – rail and the DO pin to D2.
  3. Place the LED. Connect a 220 Ω resistor to the long leg of the LED and wire it to D9. Connect the short leg to the – rail.
  4. Upload the code. In the Arduino IDE, choose Arduino Uno and the right port from Tools → Board, then click Upload.
  5. Adjust the sensitivity. The sensor has two lights. One is the power light; the other lights up when sound is detected. Slowly turn the screw on the blue box with a screwdriver. When the room is quiet, the second light should stay off, and it should flash briefly when you clap.
  6. Test it. Open Tools → Serial Monitor. Clap twice in a row. You should see “Clap: 1”, “Clap: 2” and the LED should turn on. Clap twice again to turn it off.

Troubleshooting

  • No claps are detected: Turn the sensitivity screw the other way. On some modules the screw turns 10–20 times; be patient.
  • It counts claps even when it’s quiet: The sensitivity is too high. Turn the screw back until the detection light goes off.
  • It works backwards and counts claps in silence: On some modules, the DO output goes LOW when it hears a sound. Change digitalRead(soundPin) == HIGH to == LOW in the code.

Try it yourself

  • Do something different with three claps: for example, make the LED blink.
  • Use a relay module instead of the LED to control a desk lamp. Always work with an adult when using devices that plug into the mains.
  • Connect the AO output to A0 and watch how loud the sound is as a graph in the Serial Plotter.

For teachers: a two-page activity sheet for using this project in class: lesson plan, discussion questions, answer key and a student worksheet.

Video

The narration is in Turkish. English subtitles are available: click CC on the video.

“Arduino Clap Switch Lamp: Clap Twice to Turn It On (Code)” için 2 yorum

Bir Cevap Yazın