Let’s turn the LED by finger snap?


Hello this is lifeonroom !

Today I’m going to use a sound sensor to turn on the LED with a clap or finger snap. 

I’m going to look at the preparations first. 

1. Ready water 

In the case of a sound sensor, Output is the thing which is digital analog. Usually the sound sensor has a chip, and if it says lm393, it is Digital. 

Personally, I think it would be nice to buy analog because the price difference between Digital and Analog is not much. 

 

lm393

2. Connection 

The connection should look like below. It’s easy to do an extra analog sound sensor even if you enter the digital pin. 

3. Code 

I’ll go see the basic code. 

int LED = 2;
int soundSensor = 3;
int val = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(soundSensor, INPUT);
}
void loop() {
  // put your main code here, to run repeatedly:
  val = digitalRead(soundSensor);
  Serial.println(val);
}

 

If you enter it, you’ll have to upload the sketch and open the tool > serial plotter 

And shouts from the sound sensor!!! You’ll see the following: 

Adjust the variable resistor to get a pretty waveform when clapping or finger snaps. 

Please enter the code below and upload it. 

#define LED 2
#define soundSensor 3
int soundValue;
int state = 0;
int currentSoundTime = 0;
int stateOneTime = 0;
int stateTwoTime = 0;
int stateThreeTime = 0;
int relayStatus=0;
void setup() {
  pinMode(soundSensor, INPUT);
  pinMode(LED, OUTPUT);
  //Serial.begin(9600);
}
void loop() {
  soundValue = digitalRead(soundSensor);
  currentSoundTime = millis();
  if (soundValue == 1)
  {
    
    if (state == 0)
    {
      state = 1;
      stateOneTime = millis();
      //Serial.println("state 1 set ");
    }
    else if (state == 1)
    {
      stateTwoTime = millis();
      if ((stateTwoTime  > stateOneTime + 800) &&
          (stateTwoTime  < stateOneTime + 1200))
      {
        state = 2;
        //Serial.println("state 2 set ");
      }
    }
    else if (state == 2)
    {
      stateThreeTime = millis();
      if ((stateThreeTime  > stateTwoTime + 200) &&
          (stateThreeTime  < stateTwoTime + 500))
      {
        relayStatus = !relayStatus;
        digitalWrite(LED, relayStatus);
        initVal(currentSoundTime);
        //Serial.println("state 3 set ");
      }
    }
  }
  if ((state != 0) &&
    (currentSoundTime > stateOneTime + 2000))
    {
      //Serial.println("Time Out ");
      initVal(currentSoundTime);
    }
}
void initVal(int currentSoundTime)
{
  state = 0;
  stateOneTime = currentSoundTime;
  stateTwoTime = currentSoundTime;
  stateThreeTime = currentSoundTime;
}

 

 

To outline this code, when the first sound is detected, the sound should be detected between 0.8 seconds and 1.2 seconds, and the LED will turn on if the sound is detected between 0.2 and 0.5 seconds after the second sound is detected. 

That’s three times the applause. 

Clap…….. Clap.. Clap

This is a very nice 

When you clap or finger-snap, the LEDS will be won. 

Now I’ve tried the sound sensor today! Digital sound sensors seem to be hard to write with switches that turn off critical devices. If you squeeze the cord out of the way, you can just turn the LED into a living noise, otherwise the LED will be well lit up and you will have to do more research on the Liberty heh. 

Thank you for your post today. 

You may also like...

Leave a Reply

Your email address will not be published.