I'm trying to make the led turn on when the sound is above the threshold and then have the led remain off when it is below the threshold.
The Problem:
I've tried finding the exact threshold by turning the knob on the potentionmeter that is connected to the sound sensor and the value changes every time I upload any code (the knob is shown below)
I'm not sure if it is a problem with the code :
int pin = A0;
int DO = 3;
int led = 9;
int threshold = 420;
float volume = 0.0;
void setup()
{
Serial.begin(9600);
pinMode (led, OUTPUT);
pinMode(pin, INPUT);
pinMode(DO,INPUT);
}
void loop()
{
volume = analogRead (DO);
if (volume> threshold)
{
digitalWrite (led, HIGH);
}else if (volume< threshold){
digitalWrite (led, LOW);
}
Serial.println (volume);
}
or if I wired it wrong (see the attachements for wiring)
No attachments and I've never heard of a Comidex Sound Sensor so a link would help.
But why are you doing an analogRead() to what seems to be a digital pin unhelpfully named D0? And why is the rather idiotically named 'pin' never used for anything?
I apologize about the DO pin - when I modified the code I did not remove it because I thought I might need it later on.
Here's the link to the comidox sound sensor
The point about the DO pin is that you are trying to do an analogRead() read from it rather than from the analog pin A0 (named 'pin'). The D pin on the sensor is probably digital on/off with the threshold set by the pot on the sensor. The A pin is the one with the analog output.
And I can't tell anything from your pictures because the thing you're connecting to doesn't look like any Arduino I've ever seen so I've no idea what pins you're actually connecting to.
The board is a copy of Arduino that was made for the class I'm taking in school.
The red wires are attached to d9 and gnd for the lightbulb.
The purple wire is attached to d3 , the white wire is attached to GND, the yellow wire is attached to 5V, and the blue wire is conencted from A0 on the sensor to A0 on the "arduino" board.
Thank you for pointing out the error in my code - I switched it to read from the analog pin named "pin".
I still dont know how to adjust the potentiometer on the sensor to get the threshold.
As I said before the pot on the sensor sets the threshold FOR THE DIGITAL D0 OUTPUT, i.e. when D0 switches from LOW to HIGH. It has no effect on the analog output A0.
If you're using A0 you need to set the threshold within the program as indeed you are doing with 'int threshold = 420'. For a different threshold you have to change that number.
If you want the sensor's potentiometer to set the threshold then instead of the analogRead(A0) you need to do a digitalREAD() of the D0 pin to check if it is LOW (below threshold) or HIGH (above threshold).