20 January, 2015

Making the lights turn on/off with two claps: easy and cheap with Arduino

The box ready with micro in the center
I recently saw how have different modes of intensity in a non-dimmable LED lamp using Arduino,this time we will build a switch operable by two claps followed, very useful to turn off lights, televisions, etc. In our room we stayed switch to desmano bed, so it occurred to me that it would be possible with a micro, build a relay be activating as we had seen so many times on TV, with two claps.
Also it can come in handy for people with disabilities , although in that case it would be better a remote control.
When at last I got with it, it occurred to me that surely someone had done it before, and bingo!one German commented how he had developed on his page , saving me hours of testing with micro.
A micro with an activatable certain volume output greatly simplifies things, so the theory is that the Arduino program simply be listening, and produced two (only two) high hits in less than 0.4 seconds , is likely they are two slaps.
3 component dupont few cables and the USB connector is all we need to prove
It may happen that two consecutive loud sounds (a door, heels, etc.) occur, despite the short detection time and adjust the sensitivity of micro, for those cases in which the lamps can be turned on without being one, I might add lines of code so that the relay after 6 hours turns off .
Also I had thought of adding a ultrasonic sensor to light the lamp motion for a minute, but in a room that is impractical, plus it was complicate unnecessarily. Maybe you do, but today the infrared motion sensors are so cheap (about 10 €) not worth it.Another option is "sampling" the sound, so that the Arduino is to decide if there was a "plas plas" or not, could be more reliable, as explained in this post .
I do not know what it will cost a device that does the same, but I think it cost me less than the next necessary equipment:
  1. An  Arduino "Nano"  with Atmel 328p 32KB Rom (2 Ks occupied by the bootloader).
  2. A relay "Keyes" , which will simply output 5V Arduino pin to activate.
  3. And a  special microphone to clap , ( it does not, the potentiometer is unstable , but we can use the digital output), or this one , that detects sound levels above an adjustable value, one of the digital outputs (D0) that activates a volume set resistance, and other analog output (A0) for the detected sound level (50 to 1024 for the volume of noise). Well calibrated, we can assert to record the number of times the sound exceeds a certain limit per day, for example in a job.
  4. And a transformer 7 to 12V DC to power the Arduino.
A transformer without an output capacitor sad ... with what little they cost!
I will not go into the details of programming, or how the program, etc, so that there is abundant information on other websites is introduced. This would be the simple program:




/ *
 * PROGRAM relay activation by slapping
 * (C) Creative Commons
 * Modified from http://en.code-bude.net/2014/12/08/how-to-build-a-clap-switch-using-arduino/ 
 * With operating hours saved in EEPROM
 * /
 
#include
soundSensor int = 7;
relay int = 4;
claps int = 0;
int pos = 0;
int value = 0;
long hours = 0;
long detectionSpanInitial = 0;
long detectionSpan = 0;
long millisPrevious = 0;
long millisNow = 0;
RelayState boolean = true; // Start with it enabled
 
void setup () {
  pinMode (soundSensor, INPUT);
  pinMode (relay, OUTPUT);
  digitalWrite (relay, HIGH); // Turn on when you turn on the light
}
 
void loop () {
 
  // For (int pos = 0; pos <256 all="" clear="" div="" eeprom="" once="" only="" pos="" run="" the="" to="">
  // EEPROM.write (pos, 0);
  //}
  
  sensorState int = digitalRead (soundSensor);
 
  if (sensorState == 0)
  {
    if (claps == 0)
    {
      detectionSpanInitial = detectionSpan = millis (); // Save the counter initial milliseconds
      claps ++;
    }
    else if (claps> 0 && millis () - detectionSpan> = 50)
    {
      Millis detectionSpan = ();
      claps ++;
    }
  }
  
  // Turn off without it has been 6 hours (in case of false activation)
  if (hours> 5) {// change this to the desired
    = 0 hours;
    RelayState = false;
    digitalWrite (relay, LOW);
  }     
 
  if (millis () - detectionSpanInitial> = 350)
  {
    if (claps == 2)
    {
      if (! RelayState)
        {
          RelayState = true;
          digitalWrite (relay, HIGH);
          = 0 hours; // Starts counting
        }
        else if (RelayState)
        {
          RelayState = false;
          digitalWrite (relay, LOW);
        }
    }
    claps = 0;
  }
  // While (1) {delay (500);} // infinite loop
  // If it's been an hour, we keep on increasing eeprom first value to 1, and if = 255, increase the following
  // What empowers us get to save 260,000 hours
  Millis millisNow = ();
  
  if (millisNow-millisPrevious> = 3600000)
  {
    ++ hours; // Increment
    millisPrevious = millisNow;
    pos = 0;
    while (EEPROM.read (pos) == 255) {
      pos ++;
      if (pos == 1024) {break;} // the Atmel328p has 1K EEPROM; 512 modified by minor versions
    }
    value = EEPROM.read (pos);
    EEPROM.write (pos, value ++);
  }
  else
  {
      Millis millisNow = ();
  }
  
}


He added lines of code to save to EEPROM the number of hours you are on; is very likely that the power supply or relay lasting less than the board itself, so I can check how it worked before failure. 
And if you wonder how durable an Arduino running, the conclusion is that with a power supply quality, at least 50,000 hours; between 5 and 10 years. Here is a  interesting discussion about its reliability . Its consumption is minimal; 1W reckon with this source of 7V; 50mA Nano, 20mA relay (without activating, activated spends more but will be shortly) and 15mA of micro and 60mA (minimum) source, but if you want to lower your consumption rather, it is best to put a switching power supply 5V (typ mobile charger), and can be in 0.5W when not in use. However, as it is after the light switch (the program does when the arduino is turned on, the relay is connected, it takes 1 sec. to turn it on), only when we consume off with slaps and leave the light switch connected. The potentiometer setting that brings the microphone to do with him up; I have loosened (counterclockwise to clockwise) until it is permanently activated the LED indicator sound, and then I pushed a couple of laps. Otherwise, their sensitivity was very small, and there was virtually hurt your hands to detect us. After thoroughly test it, I placed all (somewhat poorly organized, it must be said) in a recessed box next to the feed ceiling ., but can also put next to the switch, making a hole in the wall, that's because consumer tastes In the next picture connections are; Arduino down to the left. with a hole to connect the computer in case you want to adjust anything and everything stuck with some termoencoladora (not going to take a lot of heat in my case, but better usad silicone). For the microphone, I have welded the other side to pass through a small hole in the top, and you can hear good spanking. The power connections: As only two elements are of little use, it is best to feed the Arduino in its Vin (pin 30) 7 to 12V, and its 5V output (pin 22 I think), feed the relay and microphone. In case of using a relay voltage, would be fed directly to the source so that you can activate.

The transformer (red and yellow coil) I've put a good fat capacitor, for not wearing
And finally it is so on the roof next to the lamp, with three wires. White / Red is the neutral, directly connected to the lamp and the Arduino (transformer), and brown goes to the Arduino, and output (red) goes to the lamp (from the relay).

Put the lid ... and enjoy!
The truth is that it is a bit "pijada" but how good it is in the winter evenings to avoid having to get out of bed! XD A couple of slaps ... and light off. Another pair ... and light on. Another pair ... hey this is addictive! hehe.

Again, I recommend reading the page Raffi , not wasted, explains the whole process of problem analysis and technical solution that gives you.

Here is another example using the output audio level A0  (with this sensor) :


 / *
Code to activate an LED with sound. 
You have to set the minimum so as to be sufficiently sensitive.
This type of sensors only work well with loud sounds.
You connect an LED to PIN13 and sound sensor to analog pin A0 
* / 
#define DEBUG 1 
ANALOG_PIN A0 #define 
#define LED_PIN 13 

int threshold = 50; // Set this 
int volume; 

void setup () {  
pinMode (LED_PIN, OUTPUT);  
#if DEBUG 
Serial.begin (9600); // For debugging 
#endif 


void loop () { 

volume = analogRead (ANALOG_PIN); // Read the value of the analog pin. 
#if DEBUG 
Serial.println ("Volume" + String (volume)); 
delay (100); 
#endif 

if (volume> threshold) { 
digitalWrite (LED_PIN, HIGH); Led Turn ON // 
}  
else { 
digitalWrite (LED_PIN, LOW); Led Turn OFF // 

}



And as always, if you liked it or have any questions, what are you waiting to comment! :)

The only downside I have is that also operated with coughs ... I put no bad! XD

References: How to build a clap switch using Arduino Sound Sensor stroke using a piezoelectric speaker and a resistor with Arduino Controller Sound Activated

No comments :

Post a Comment