A clapper switch! (...once again)

Hello there everybody!

I's my first official time here, but I've been lurking around the forums for almost a year, while trying to build up my custom arduino project...why, it's a clapper switch, of course! :slight_smile:

Let's see...I started with a "simple" SINGLE CLAP ON/OFF LED LIGHT using the code I found on this Arduinonano-Clap-Switch instructables page/.
Code read as follows:

int micpin=10;

int ledpin=12;

void setup() {

pinMode (micpin, INPUT);

pinMode (ledpin, OUTPUT);

}

void loop() {

if (digitalRead (micpin)==HIGH) {

if (digitalRead(ledpin)==LOW) {

digitalWrite (ledpin, HIGH);

} else {

digitalWrite(ledpin,LOW);

}

delay(1000);

}

}

This worked fine, so I went for the next step, turning it into a DOUBLE CLAP ON/OFF LED LIGHT, mixing the original code with a bit I found on a few different pages, such as this HighVoltageCode page and this 14Core page.
This way, I obtained the following code:

int InputSound = 0;
int micpin = 10;
int ledpin = 12;

long DetectSpan = 0;
long SoundDetection = 0;

boolean lightState = false;

void setup() {
  Serial.begin(9600);
  pinMode (micpin, INPUT);
  pinMode (ledpin, OUTPUT);
  Serial.print("Microphone up and running");
}

void loop() {

  int sensorState = digitalRead(micpin);
             
    if (sensorState == 0)
    {
      if (InputSound == 0)
      {
        SoundDetection = DetectSpan = millis();
        InputSound++;
      }
      else if (InputSound > 0 && millis()-DetectSpan >= 50)
      {
        DetectSpan = millis();
        InputSound++;
      }
    }

    if (millis() - SoundDetection >= 400)
    {
      if (InputSound == 2)
      {
        if (!lightState)
        {
          lightState = true;
          digitalWrite(ledpin, HIGH);
          Serial.print("Let there be light!"); 
        }
        else if (lightState)
        { /* altrimenti */
          lightState = false; 
          digitalWrite(ledpin, LOW); 
          Serial.print("Lights of, please!");
        }
      }
      InputSound = 0; 
    }
}

This worked as well...so I went for the big jump and changed the code to use a relay switch to POWER ON/OFF an extension cord. I adapted the code above mixing it with what I found on this Create.Arduino page and again on the HighVoltage page.
Here's the final code:

int InputSound = 0;
int micpin = 10;
int relaypin = 12;
long DetectSpan = 0;
long SoundDetection = 0;

boolean relayState = false;

void setup() {
  Serial.begin(9600);
  pinMode (micpin, INPUT);
  pinMode (relaypin, OUTPUT);
  Serial.print("Microphone up and running");
}

void loop() {

  int sensorState = digitalRead(micpin);
             
    if (sensorState == 0) 
    {
      if (InputSound == 0)
      {
        SoundDetection = DetectSpan = millis();
        InputSound++;
      }
      else if (InputSound > 0 && millis()-DetectSpan >= 50)
      {
        DetectSpan = millis();
        InputSound++;
      }
    }

    if (millis() - SoundDetection >= 400)
    {
      if (InputSound == 2)
      {
        if (!relayState)
        {
          relayState = true;
          digitalWrite(relaypin, HIGH);
          Serial.print("Let there be light!");
        }
        else if (relayState)
        {
          relayState = false;
          digitalWrite(relaypin, LOW);
          Serial.print("Lights off, please!");
        }
      }
      InputSound = 0;
    }
}

Now...this should work...and yet it does not!
I chopped the power extension, then plugged the + cable (blue) to the C (in) and another piece of blue cable from the NO (out) nack to the plug...yet this does not work!
I tried plugging in the arduino nano to its usb cable, and all of a sudden, the board turned on and seemed to work (nano, microphone and even relay turned on and I could CLAP CLAP ON/OFF the relay),but then, when I turn the relay itself off by double clapping, the electricity still flowed to the plug.

What am I boing wrong? Please help!
Tomorrow I'll provide an image of my setup, to better undersand it.

Many thanks to everyone who can provide a solution.

twain28:
Hello there everybody!

I's my first official time here, but I've been lurking around the forums for almost a year, while trying to build up my custom arduino project...why, it's a clapper switch, of course! :slight_smile:

Let's see...I started with a "simple" SINGLE CLAP ON/OFF LED LIGHT using the code I found on this Arduinonano-Clap-Switch instructables page/.
Code read as follows:

int micpin=10;

int ledpin=12;

void setup() {

pinMode (micpin, INPUT);

pinMode (ledpin, OUTPUT);

}

void loop() {

if (digitalRead (micpin)==HIGH) {

if (digitalRead(ledpin)==LOW) {

digitalWrite (ledpin, HIGH);

} else {

digitalWrite(ledpin,LOW);

}

delay(1000);

}

}




This worked fine, so I went for the next step, turning it into a DOUBLE CLAP ON/OFF LED LIGHT, mixing the original code with a bit I found on a few different pages, such as [this HighVoltageCode page](https://www.highvoltagecode.com/post/arduino-clap-switch-controlling-home-appliances-with-a-double-clap) and [this 14Core page](https://www.14core.com/making-your-own-sound-activated-switch-on-arduino/).
This way, I obtained the following code:


int InputSound = 0;
int micpin = 10;
int ledpin = 12;

long DetectSpan = 0;
long SoundDetection = 0;

boolean lightState = false;

void setup() {
  Serial.begin(9600);
  pinMode (micpin, INPUT);
  pinMode (ledpin, OUTPUT);
  Serial.print("Microphone up and running");
}

void loop() {

int sensorState = digitalRead(micpin);
           
    if (sensorState == 0)
    {
      if (InputSound == 0)
      {
        SoundDetection = DetectSpan = millis();
        InputSound++;
      }
      else if (InputSound > 0 && millis()-DetectSpan >= 50)
      {
        DetectSpan = millis();
        InputSound++;
      }
    }

if (millis() - SoundDetection >= 400)
    {
      if (InputSound == 2)
      {
        if (!lightState)
        {
          lightState = true;
          digitalWrite(ledpin, HIGH);
          Serial.print("Let there be light!");
        }
        else if (lightState)
        { /* altrimenti */
          lightState = false;
          digitalWrite(ledpin, LOW);
          Serial.print("Lights of, please!");
        }
      }
      InputSound = 0;
    }
}




This worked as well...so I went for the big jump and changed the code to use a relay switch to POWER ON/OFF an extension cord. I adapted the code above mixing it with what I found on [this Create.Arduino page](https://create.arduino.cc/projecthub/mtashiro/the-clapper-09dc8a) and again on [the HighVoltage page](https://www.highvoltagecode.com/post/arduino-clap-switch-controlling-home-appliances-with-a-double-clap).
Here's the final code:


int InputSound = 0;
int micpin = 10;
int relaypin = 12;
long DetectSpan = 0;
long SoundDetection = 0;

boolean relayState = false;

void setup() {
  Serial.begin(9600);
  pinMode (micpin, INPUT);
  pinMode (relaypin, OUTPUT);
  Serial.print("Microphone up and running");
}

void loop() {

int sensorState = digitalRead(micpin);
           
    if (sensorState == 0)
    {
      if (InputSound == 0)
      {
        SoundDetection = DetectSpan = millis();
        InputSound++;
      }
      else if (InputSound > 0 && millis()-DetectSpan >= 50)
      {
        DetectSpan = millis();
        InputSound++;
      }
    }

if (millis() - SoundDetection >= 400)
    {
      if (InputSound == 2)
      {
        if (!relayState)
        {
          relayState = true;
          digitalWrite(relaypin, HIGH);
          Serial.print("Let there be light!");
        }
        else if (relayState)
        {
          relayState = false;
          digitalWrite(relaypin, LOW);
          Serial.print("Lights off, please!");
        }
      }
      InputSound = 0;
    }
}




Now...this should work...and yet it does not!
I chopped the power extension, then plugged the + cable (blue) to the C (in) and another piece of blue cable from the NO (out) nack to the plug...yet this does not work!
I tried plugging in the arduino nano to its usb cable, and all of a sudden, the board turned on and seemed to work (nano, microphone and even relay turned on and I could CLAP CLAP ON/OFF the relay),but then, when I turn the relay itself off by double clapping, the electricity still flowed to the plug.

What am I boing wrong? Please help! 
Tomorrow I'll provide an image of my setup, to better undersand it.

Many thanks to everyone who can provide a solution.

is it a single relay by itself or a relay module board?

tjones9163:
is it a single relay by itself or a relay module board?

Single relay.

I am trying to post an image for a (hopefully decent) view on the hardware and wirings. Be here soon...
EDITED: see attached image. :slight_smile:

My general idea was to directly power the nano itself via the extension, and then using it to power on or off the plug at the end of the relay...but I guess I am missing something, and I DO guess it is not something related to the code...

20200322_103232.jpg
Expand!

I fail to see how the Arduino and all the low voltage stuff is being powered

wolframore:
I fail to see how the Arduino and all the low voltage stuff is being powered

That IS correct...and in fact that's why the "CLAP CLAP ON/OFF" circuit actually works only whenthe Arduino is plugged via USB to power...but it works bad: basically, the plug seems to get power even when the circuit should not allow it...

So, my issues are actually two:

  1. how to power the Arduino itself without having to resort to an additional power plug (through USB); and
  2. how to really make the relay circuit work as it's supposed to work... :smiley:

I don't know if issue no.1 can be solved, but I would indeed be grateful if you could help me solve at least issue no.2.

First the easy part. Note this circuit that was added in the HV example.. it looks like AC/DC power supply that supplies the 5V from the AC line. Just be cautious and if you get one try to get one with a transformer as they are safer.

As far as why it isn’t working... that’s more difficult to diagnose without having it in front of us. Remove the AC portion and put a multimeter on continuity test and see if it shows turning on and off without the AC load on it.

Please note that they are switching the line not neutral.

wolframore:
First the easy part. Note this circuit that was added in the HV example.. it looks like AC/DC power supply that supplies the 5V from the AC line. Just be cautious and if you get one try to get one with a transformer as they are safer.

Thanks, I had not noticed this...I guess I have still a lot to learn, now the fritzing scheme on that page makes more sense


...it'll be a while, before I can get my hands on that piece of hardware, though...I'll have to order it and get it shipped home, and with the COVID-19 emergency, things and deliveries are getting quite slow, here in Italy...

...in the meantime, I am however quite curious about something: I noticed the AC/DC power supply is not present in the Create Arduino page project nor in the 14Core project page...so how would they power the board? Am I missing something? :o

As far as why it isn’t working... that’s more difficult to diagnose without having it in front of us. Remove the AC portion and put a multimeter on continuity test and see if it shows turning on and off without the AC load on it.

Please note that they are switching the line not neutral.

Ehm...I'll have to buy a multimeter as well... ;D I guess you can safely say I was utterly unprepared to the task at hand...we live and learn...

I’m glad you mentioned live

This project however innocent it looks could be deadly to you or someone using it if you make a mistake and don’t know what you’re doing.

wolframore:
I’m glad you mentioned live

This project however innocent it looks could be deadly to you or someone using it if you make a mistake and don’t know what you’re doing.

Indeed :slight_smile:

So what you are saying is the mentioned projects are probably missing relevant electric parts, to be effective.

twain28:
in the meantime, I am however quite curious about something: I noticed the AC/DC power supply is not present in the Create Arduino page project nor in the 14Core project page...so how would they power the board? Am I missing something?

The USB cable?

Paul__B:
The USB cable?

Fair enough...but if I plug the power cord, the electricy seems to go through, even if the relay should cut it off...I guess I will try again that circuit using my UNO board, but I doubt I will get a different result...

Are you saying that it starts up with the relay on? Does it work correctly afterwards? Your logic to the relay may be reversed in that case and needs a low to trigger... if it is then just switch the on and off commands in your code.

wolframore:
Are you saying that it starts up with the relay on? Does it work correctly afterwards? Your logic to the relay may be reversed in that case and needs a low to trigger... if it is then just switch the on and off commands in your code.

No, leaving aside the possibility to power the NANO through the same power cord, my issue is actually a different one: the CLAP -> Relay ON apparently works if I plug the Nano via USB.
The NANO closes the circuit, so the relay green light turns on, electricity goes through and powers the plug, and that's ok...but then when I CLAP again to turn it OFF, it only seems to open the circuit again, meaning that the relay green light actually does turn off...but electricity still seems to pass through.
That's the ineffectiveness I'm dealing with...I'll try to attach a small gif to show you the thing, if I manage to turn it small enough...

Ok...I think I made myself an idiot once more... :-[

...where to begin...

To test my circuit, I plugged an old phone via its charger to the plug connected to the relay.

What I did wrong was plugging the phone in before I had powered the NANO via its usb cable...this meant the plug started getting power through the inert circuit, thus bypassing the relay, no matter how many times I clapped my hands...

If, however, I first plug the NANO and only afterwards I plug the phone to the plug...IT WORKS! :o

I can't believe I've been that stupid... :frowning:

This means the only issue left to solve is powering the NANO through the same cable...

I AM learning, I would say... :slight_smile:

please change your wiring to switch the "line" not neutral for safety reasons.

European wire colors per IEC:

wolframore:
please change your wiring to switch the "line" not neutral for safety reasons.

European wire colors per IEC:

...so what you are saying is that I should have connected the brown instead of the blue cable to the relay? :o

Gee...I don't know where had I actually found out the wrong cable colour guide....

I am astonished I did not fry everything up, so far... :confused:

I'm just glad no one got hurt... it's possible to think you have something off then go to work on it and accidentally be grounded which would not be good... that's the reason for it.

wolframore:
I'm just glad no one got hurt... it's possible to think you have something off then go to work on it and accidentally be grounded which would not be good... that's the reason for it.

Indeed...and I am surprised myself...I did check a cable color table....looks like I was sorely mistaken...
In any case, I've ordered the AC/DC converter...let's see when it comes round...

A small update: the AC/DC converter has finally arrived...I'll try to fit it in, following the HVC schematics and see if it works as it should.