arduino based clap switch

soham's picture

Offline

Joined: Sep 30, 2018

Replies: 0
arduino based clap switch
September 30, 2018 - 5:41pm

the following code and circuit  does not work , instead LED blinks continuously without clap, even blinks after removing all components with GND and pin 7 connection.

please help

const int analogInPin = A0;  // Analog input pin 0
int sensorValue = 0;

void setup()
{
  DDRD = 0xFF;
}

void loop()
{
  sensorValue = analogRead(analogInPin);    //read ADC value on channel 0
  if (sensorValue > 60)
  {
    PORTD ^= (1 << 7); //If there is a peak toggle the LED on and OFF on pin7.
    delay(250);
  }
}

tedder's picture

Offline

Joined: Oct 01, 2018

Replies: 2
You didn't say what you are
October 1, 2018 - 7:51pm

You didn't say what you are using as a 'sensor', but you are probably failing to use a pullup/pulldown.

Jayant's picture

Offline

Joined: May 19, 2015

Replies: 187
Do not leave Analog pin in floating
October 2, 2018 - 11:27am

Hi Soham, welcome to Circuit Digest forums 

As Tedder rightly pointed out the problem might be with the absence of pull up or pull down resistor.

When you are reading the analog values from a Analog pin of Arduino, the pin should always be connected to some voltage potential. If it is left free it is called as floating pin, and can give any random value to the ADC module.

One best feature in Arduino is with the ease of debugging ,so use serial monitor and print the value of sensorValue so that you yourself can debug your code. An image of the circuit will help you in getting more relevant answers.