0

I want my Arduino to turn on a LED when sound sensor detects a clap (noise) and keeps the LED on until the sound sensor detects another clap (noise) which turns the LED off.

int Sensor = A0;
boolean status_lights = false;

void setup() {
  pinMode(Sensor, INPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  int status_sensor = digitalRead(Sensor);
  if (status_sensor == 0)
  {
    if (status_lights == true)
    {
      status_lights = false;
      digitalWrite(13, LOW);
    }
    else {
      status_lights = true;
      digitalWrite(13, HIGH);
    }
  }
}

My sound sensor has 3 pins. sound sensor details

EDIT: My code only turns on the led but dosen't turn it off when I clap.

1
  • you have no code that is used for debugging... at least print the value of status_sensor to see what it actually is
    – jsotola
    Commented Dec 20, 2019 at 20:18

1 Answer 1

1

If the sound level is below the threshold, the DO pin of the sensor signals a high value. If the sound level exceeds the threshold, i.e you clap you hands, the DO is pulled to GND and the signal gets LOW for a short while. After this short while the signals gets HIGH again.

Now look at you loop:

void loop() 
{
  int status_sensor = digitalRead(Sensor);

  if (status_sensor == 0)
  {
    // you enter this block if you clap your hands and the 
    // mcu is fast enough to capture the signal / DO pin (LOW)
    // within the short time interval 
    // I'm sure it is fast enough to enter this block multiple 
    // times in the interval the clap is active.
    if (status_lights == true)
    {
      status_lights = false;
      digitalWrite(13, LOW);
    }
    else 
    {
      status_lights = true;
      digitalWrite(13, HIGH);
    }
  }
}

You see, the LED is toggled as long as the LOW interval of the sensor is active (DO=LOW).

I interpret you statement "My code only turns on the led but dosen't turn it off when I clap." so, that the LED is on, when the program starts without a clap. That means that the environmental noise is loud enough to activate the sensor. Then the loop above is iterated permanently and you switch on and off the LED at a very fast speed. That's like you would apply a PWM waveform on the LED pin. The LED is on but dimmed to about 50%.

To find out if I'm right, just add a delay in the loop. My assumption seems to be correct, if the LED blinks with one herz (one time per second). The you can adjust the sound threshold until the LED stops blinking.

void loop() 
{
  int status_sensor = digitalRead(Sensor);

  // Add the delay here ..........
  delay(500);

  if (status_sensor == 0)
  {

If you have calibrated the sensor, remove the delay from the code and add a new delay after changing the LED states. That should be much shorter, because it's only there to ensure that you don't toggle the LED while the sensor is active.

    if (status_lights == true)
    {
      status_lights = false;
      digitalWrite(13, LOW);
    }
    else 
    {
      status_lights = true;
      digitalWrite(13, HIGH);
    }
    // wait until the clap is over 
    delay(100);
2
  • +1 for this code comment: "I’m sure it is fast enough to enter this block multiple times in the interval the clap is active." When you clap, it toggles the DO pin high and low multiple times, and it looks something like this: arduino.stackexchange.com/a/54495/37523
    – VE7JRO
    Commented Dec 21, 2019 at 16:31
  • @VE7JRO The documentation of the sensor is not really helpful, so thank you for giving an example. I was not really sure whether the use something like a smoothing condenser. But obviously that's not the case. (73 Peter, DF4VB) Commented Dec 21, 2019 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.