$USD
  • EUR€
  • £GBP
  • $USD
PROJECTS Arduino

Clap-O-Switch

DFRobot Feb 13 2020 884

Project Maker: Rushabh Jain



Things used in this project

Hardware components

Microchip ATtiny85 ×1

DFRobot Gravity: Analog Sound Sensor For Arduino × 1

Relay (generic) ×1


Software apps and online services

Arduino IDE


Hand tools and fabrication machines

Soldering iron (generic)

Soldering Gun Kit, Instant Heat

Drill / Driver, Cordless


Story

Ever feel too lazy to get up to turn off THAT one lamp? That lamp which is essential but also irritates you the most. That lamp which after you turn off you race to bed like hell. Well, fear not people I got a perfect solution for you. Clap-O-Switch, a perfect switch which you can control by clapping twice. So no sprinting to bed with the ghost just clap and sleep.


Components Required

ATtiny85 microcontroller / Arduino can also be used

Sound sensor

5 volts relay

Three pin AC socket

Three pin C14 input socket with cable

Pcb board.

Any colour Leds x2

A project box


Schematics

For the project I have used a basic ATtiny85 microcontroller as the brain of the project. A sound sensor is used to sense the clap intensity. An algorithm runs on the microcontroller to sense the particular type of clap. It is then used to actuate a relay, which in turn activates the load (bulb).

Before soldering test the schematics and the code on the breadboard to avoid unnecessary stress.


Code

I have attached the code below with the project, but in this section I will describe about the code in detail.

#include <ResponsiveAnalogRead.h>
For this project have included "ResponsiveAnalogRead" library.

ResponsiveAnalogRead is an Arduino library for eliminating noise in analogRead inputs without decreasing responsiveness. You can read more about this library here: https://github.com/dxinteractive/ResponsiveAnalogRead

#define sound_pin   5    //use any ADC pin for sound sensor
#define relay_pin   3    //Can use any pin for digital logic
Here you can define the pin number where the sensor is attached. Use only ADC pin for sound sensor as we need analog value. Relay can be connected to any pin which will give use digital output.

ResponsiveAnalogRead sound(sound_pin, true);
An object 'sound' of class 'ResponsiveAnalogRead' is created here. This has been done so we could access various functions in the 'ResponsiveAnalogRead' library.

void loop() {
 sound.update();    //current sound value is updated
 soundValue = sound.getValue();
 currentNoiseTime = millis();
 Serial.println(soundValue);
  
 if (soundValue > threshold) {         // if there is currently a noise
   if (
     (currentNoiseTime > lastNoiseTime + 250) && // to debounce a sound occurring in more than a loop cycle as a single noise
     (lastSoundValue < 400) &&  // if it was silent before
     (currentNoiseTime < lastNoiseTime + 750) && // if current clap is less than 0.75 seconds after the first clap
     (currentNoiseTime > lastLightChange + 1000) // to avoid taking a third clap as part of a pattern
   ) {
     relayStatus = !relayStatus;
     digitalWrite(relay_pin, relayStatus);
     delay(300);
     lastLightChange = currentNoiseTime;
    }
    lastNoiseTime = currentNoiseTime;
 }
 lastSoundValue = sound.getValue();
}
This is the algorithm for sensing the only two claps and changing the logic level accordingly. The sound value is stored in the variable 'soundValue'. This is updated every loop.

If the 'soundValue' is more than the threshold then it will advance in the if loop. Then it will check four conditions for the clap. These conditions are explained in code itself. When all the given condition are satisfied the relay status will toggle. A delay of 300 milli second is given so that the relay won't make clicking noise.

The algorithm is taken from one of the Instructatbles project. I'll post the the link once I get it.


Assemble

I have used a normal project ABS box for my project. I have used 3 pin AC connector C14 as the input socket.

C14 AC socket
For the output where the load/lamp is connected I have used a normal 3 pin AC female socket where we can easily connect anything we want.
Three pin AC socket
I have also mounted two led in the box. The red led represents if the box(Clap-O-Switch) is ON or OFF. The green led represents the condition of the output.

The Inside of the box
I have assemble the circuit in ABS project box. Both the AC ground are connected for protection. The ATtiny is powered by a 5v supply which we get by using an adapter circuit to convert 230V AC to 5V DC.


Working



Schematics
Clap-O-Switch

Code
Clap-O-Switch Arduino

#include <ResponsiveAnalogRead.h>

#define sound_pin   5
#define relay_pin   3

ResponsiveAnalogRead sound(sound_pin, true);

int soundData = 0;
int threshold = 1000;
int lastSoundValue = 0 ;
int soundValue;
long lastNoiseTime = 0;
long currentNoiseTime = 0;
long lastLightChange = 0;
int relayStatus = HIGH;
int data = 0;


void setup() {
  pinMode(relay_pin,OUTPUT);
  digitalWrite(relay_pin,HIGH);
  //pinMode(sound_pin,INPUT);
  Serial.begin(9600);

}

void loop() {
  sound.update();
  soundValue = sound.getValue();
  currentNoiseTime = millis();
  Serial.println(soundValue);

  if (soundValue > threshold) { // if there is currently a noise

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

      relayStatus = !relayStatus;
      digitalWrite(relay_pin, relayStatus);
      delay(300);
      lastLightChange = currentNoiseTime;
     }

     lastNoiseTime = currentNoiseTime;
  }

  lastSoundValue = sound.getValue();

}





REVIEW