Led light Clap Switch Issue

I am currently working on my first ever arduino project, a clap switch for a led light but it doesn’t seem to be working :smiling_face_with_tear:, please I need help

I will type out my code below

#include <Arduino.h>

const int=11;
const int= A2;
const int threshold= 200;

Void setup () {
Serial.begin (9600);
pinMode(ledpin, OUTPUT);
PinMode(soundpin, INPUT);

}

Void loop () {
int soundValue = analogRead(soundpin);
Serial.println(soundValue);

If ( soundValue >= threshold) {
digitalWrite( ledpin, HIGH);
Serial.print(“sound Value: “);
Serial.print (“Sound Detected”);

}else {
digitalWrite(ledpin, LOW);
Serial.println ( “LED OFF”);
}

delay(500);
}

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
    Give links to components.

Void setup () {
Should be:
void setup () {

  • delay( ) stops code execution for that amount of time, avoid using it.
1 Like

Read the forum guidelines, it explains how to use code tags.

What part is not working? What sensor are you using? Is the clap soft or har4d? How far are you from the sensor when clapping? Which Arduino are you using?

Eu encontrei pelo menos 5 erros no seu codigo:
I found at least 5 errors in your code:

1 Void setup() with a uppercase V;
2 pinMode(ledpin, OUTPUT); ledpin pin not defined;
3 PinMode(soundpin, INPUT); soundpin pin not set;
4 PinMode(soundpin, INPUT); with a uppercase P;
5 Void loop () with a uppercase V;

2 Likes

Thank you dear

I will do it sir

Sir, I have not been able to get a good schematic of the circuit due to network issues

But this is my connection below

  1. Sound sensor
    G to GND pin
  • to 5v pin
    AO to AO pin

On Breadboard

2.Led

Cathode with resistance to pin 11 and anode of led to GND


Product link

Purchased from Aliexpress

Please drop correction

I don't know about that sensor, but il you clap during that 0.5s delay, fore sure it's not heard. And that delay is about 100% of total time in loop, so...

const int=11;
const int= A2;

The above should be changed.

const int ledpin =11;
const int soundpin= A2;

Wrong

  • Let’s confirm the LED is properly installed in the breadboard (looks like it might be installed backwards).
    Try toggling the LED with the Blink sketch that is in the IDE examples.

  • The LED should be wired similar to LED D2 in the image below.


Try these changes too.

const byte ledpin = 11;
const byte soundpin = A2;

const int threshold = 200;

void setup () 
{
Serial.begin (9600);
pinMode(ledpin, OUTPUT);
}

void loop () 
{
  int soundValue = analogRead(soundpin);

  if (soundValue >= threshold)
  {
  digitalWrite( ledpin, HIGH);
  Serial.print(“Sound Value: “);

  Serial.println(soundValue);

  Serial.print (“Sound Detected”);
  delay(500);
  }

else 
  {
  digitalWrite(ledpin, LOW);
  Serial.println ( “LED OFF”);
  }

} 
1 Like

Much better, before the sensor was "deaf" 99.98% of time

  • Does your LED turn ON ?
1 Like

Yes sir

1 Like

It comes on without waiting for the clap signal :smiling_face_with_tear:

  • Play with this.
    const int threshold = 200;

  • Also, you can add a low pass filter on the output of the sound sensor to get rid of high frequency spikes.

Now it’s not coming on at all

Five days later. Assume we've not been able to follow along with what you have tried, what your results were and what your code might have become during that time.

Use a few more of your lifetime supply of key strokes and tell us what's up.

a7

Hello please I really need help, I have Been trying out different codes online and non of them seems to be working, my project is due on Wednesday :face_holding_back_tears:

I will send a sample of the code I used last

int soundsensor = A2;
int led = 10;

int clap = 0;
long detection_range_start = 0;
long detection_range = 0;
boolean status_lights = false;

void setup() {
pinMode(soundsensor, INPUT);
pinMode(ledpin, OUTPUT);
}

void loop() {
int status_sensor = digitalRead(soundsensor);
if (status_sensor == 0)
{
if (clap == 0)
{
detection_range_start = detection_range = millis();
clap++;
}
else if (clap > 0 && millis()-detection_range >= 50)
{
detection_range = millis();
clap++;
}
}
if (millis()-detection_range_start >= 400)
{
if (clap == 2)
{
if (!status_lights)
{
status_lights = true;
digitalWrite(relay, HIGH);
}
else if (status_lights)
{
status_lights = false;
digitalWrite(ledpin, LOW);
}
}
clap = 0;
}
}

My connection
Plus pin on sound sensor to 5v
G to GND
AO to A2

Led Anode with resistance to 11
Led cathode to GND

Sir please help me

How long is the led meant to stay on for after you clap? How many claps to make the led come on? Just one?
Or is it supposed to be clap hands to make led come on then clap again to turn it off?