Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Rohan Barnwal
Published

Clap to Illuminate: Building a Sound-Activated Light Switch

Light up your home with a clap! Our DIY project shows you how to make a sound-activated light switch using Arduino. Quick, easy, and fun!

AdvancedFull instructions provided3,206
Clap to Illuminate: Building a Sound-Activated Light Switch

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
Using the Arduino Uno microcontroller board is a fantastic choice for this project, as it provides a versatile and reliable platform for creating DIY electronics. Its compact size and easy-to-use interface make it ideal for beginners and experienced makers alike. With the Uno, you can easily control and customize your sound-activated light switch, creating a truly unique and interactive home environment.
×1
Relay Module (Generic)
The relay module is a crucial component in this project, as it allows you to control high-power devices like lights with a low-power signal from the Arduino. Its compact size and simple interface make it easy to integrate into your project, while its robust design ensures reliable and safe operation. With the relay module, you can take your sound-activated light switch to the next level, creating a truly innovative and convenient home automation system.
×1
mic module
The mic module is the heart of this project, as it senses and amplifies sound signals from your claps, triggering the Arduino to turn your lights on and off. Its small size and easy-to-use interface make it ideal for integrating into your DIY electronics projects. With the mic module, you can bring a new level of interactivity and convenience to your home environment, creating a truly unique and innovative experience.
×1
Jumper wires (generic)
Jumper wires (generic)
Jumper wires are an essential component in any DIY electronics project, providing a reliable and easy-to-use method for connecting components together. With their flexible design and standardized connectors, jumper wires make it easy to prototype and test your projects quickly and efficiently. In this project, jumper wires help to connect the mic module and the relay module to the Arduino board, creating a seamless and reliable connection. With jumper wires, you can easily customize and iterate on your projects, taking your DIY electronics skills to the next level.
×1

Software apps and online services

Arduino IDE
Arduino IDE
The Arduino IDE is the go-to software for programming the Arduino boards, providing a user-friendly and powerful interface for creating and uploading code. With its simple and intuitive editor, you can easily write, compile, and upload your code to the Arduino board, allowing you to control your sound-activated light switch with ease. With the Arduino IDE, you can unleash your creativity and take your DIY electronics projects to the next level.

Story

Read more

Code

here's the code

Arduino
#define signalToRelayPin 13
#define sensorPin 2

int lastSoundValue;
int soundValue;
long lastNoiseTime = 0;
long currentNoiseTime = 0;
long lastLightChange = 0;
int relayStatus = HIGH;

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(signalToRelayPin, OUTPUT);
}

void loop() {
  soundValue = digitalRead(sensorPin);
  currentNoiseTime = millis();

  if (soundValue == 1) { // if there is currently a noise

    if (
      (currentNoiseTime > lastNoiseTime + 200) && // to debounce a sound occurring in more than a loop cycle as a single noise
      (lastSoundValue == 0) &&  // if it was silent before
      (currentNoiseTime < lastNoiseTime + 800) && // if current clap is less than 0.8 seconds after the first clap
      (currentNoiseTime > lastLightChange + 1000) // to avoid taking a third clap as part of a pattern
    ) {

      relayStatus = !relayStatus;
      digitalWrite(signalToRelayPin, relayStatus);
      lastLightChange = currentNoiseTime;
     }

     lastNoiseTime = currentNoiseTime;
  }

  lastSoundValue = soundValue;
}

Credits

Rohan Barnwal
21 projects • 30 followers
Rohan Barnwal - maker, hacker, tech enthusiast. I explore new tech & find innovative solutions. See my projects on hackster.io!

Comments