loading

An simple, fun and useful device to impress your friends, feel like a bon vivant, or simply turn the lights off in the laziest way possible.

I set it to recognise two claps. Sometimes it can take other repeating noises in the room (like coughing) by mistake, but can be improved to any kind of clapping pattern.

Please be careful while you're working with AC power. Don't touch the connectors in the relay when is connected. If you're young, ask an adult for help.


You will need:

  • 1 Arduino UNO (or any Arduino compatible)
  • 1 Relay module (any 5v relay will work but you'll need other components)
  • 1 noise sensor FC-04 or similar
  • 1 12v AC/DC adapter to power the Arduino
  • A couple of female/male cables to connect the components
  • 1 AC male plug
  • 1 AC female plug
  • AC power wire
  • 1 Box to contain it all

  • Software: Arduino IDE

Step 1: Set the Sound Sensor

The FC-04 sensor has three pins. Connect VCC to the 3.3v output pin and GND to GND on the Arduino board. For this tutorial we will connect the OUT data to pin 2 on the Arduino board, but you can use any digital pin.

I've found out that some sound sensors return an analog value with the level of noise detected. The one I'm using only returns a digital signal when it gets to the level set by the potentiometer in the sensor.

Upload this script to the Arduino board to get to see when the sensor is detecting noise to adjust the sensitivity.

#define LED 13
#define sensorPin 2

int soundValue;
int ledStatus = HIGH;

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

void loop() {
soundValue = digitalRead(sensorPin);
digitalWrite(LED, (soundValue == 1) ? HIGH : LOW);
}

You may notice that the led on the Arduino board will start to blick when there's noise. With a small screwdriver, gently, turn the potentiometer on the sensor counterclockwise all the way. While speaking, slowly turn the potentiometer clockwise till it doesn't detect your voice anymore. Clap to see if that sound is high enough to turn the led on the Arduino. Test it on different parts of the room until you are satisfied with the level of sensitivity.

Step 2: Create the Clap Pattern Detector

We will be using a variable to record the time (in milliseconds) when a noise is detected. Comparing this variable with the current time, Arduino will know if the second clap was close enough to the first clap to identify it as our clap pattern.

Upload this code to the Arduino board and check if the led behaves as you expected.

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

Step 3: Set the Relay

I'm using a relay module which is ready to connect directly to Arduino. If you are using a regular 5v relay you will need to add some resistors and a transistor.

We'll connect the relay VCC to 5v and GND to GND in the Arduino board. The IN signal pin will be connected to the pin 13 in the Arduino board.

We'll make an AC power cord with one wire going straight from the female plug to the male plug and the other will be connected from the female plug to the middle connector in the relay and from the "normally open" connectors in the relay to the male plug.

You can now plug a lamp to test it. Please, be careful when testing the project at this point. Do not touch any part of the relay or the ac wiring while plugged. If you need to fix the wiring be sure to unplug the power before.

Step 4: The Box

Create a case that can contain the board, the relay, the sensor and the AC/DC adapter. You can feed the adapter adding another female plug to the circuit before the relay so is always on. Isolate the relay and keep the components fixed to the case to avoid accidents.

You can add holes in the box near the sound sensor to improve the sensitivity.

About This Instructable

999views

27favorites

License:

Bio: Programmer and poet at Aerolab.co
More by pato_pitaluga:Turn on and off a lamp clapping twice, using Arduino Automate your grocery list using QR codes Arduino Programmable Button Panel As Keyboard 
Add instructable to: