How to make the LED react on input

Hello
I'ts the first time i use the arduino board, and i'm having troble programming.
I'm tying to make a sounddetector.
I have a input signal, from a mic, that signal is wery low, like 0.1 to 0.3V, what i'm trying to do, is that tre LED should light up, depending on how big to signal in the input is.
I've been trying a lot of things, but nothing i do seems to work right. some times, the LED' is on constantly, on some time it doesn't light at all..
I'm Despered, PLEASE HELP ME... :cry: :cry:

Make a sketch that just prints the output from your microphone so you can see what values you are getting. In that way you will know what value to set the LED on / LED off threshold.
Also if it's straight from a microphone you are feeding AC into a DC input. Connect the microphone through a capacitor to the input. Then connect two resistors to the input (10K or so it doesn't matter much). Connect the other ends to 5V and 0V. In that way you will get a signal centred around a reading of 512, your threshold can then be just above this value.
Also once the LED is on put in a small delay to keep it on until you look at the input again.

i alread have done that, i have readed the value of from the output, on the end that i should put in the input, it is 0.1V 0.2V and 0.3V, but my teacher says that, it was the programming that went wrong...
but he still cant figure out how to use the program....

can somebody please tell me how to progam the board to get the LED to react on the microphone signal...PLEASE ;D ;D ;D ;D ;D ;D ;D ;D :cry: :cry:

You've already created a sketch that prints the values read from the input? What it is it printing? What does your code look like? How are you connecting the mic to the arduino?

#define ledPin(green) = 13
#define ledPin(yellow) = 12
#define ledPin(red) = 11

int ledPingreen = 13;
int ledPinyellow = 12;
int ledPinred = 11;
int analogPin = 0;
int val = 0;

void setup()
{
pinMode(analogPin, INPUT);
pinMode(ledPingreen, OUTPUT);
pinMode(ledPinyellow, OUTPUT);
pinMode(ledPinred, OUTPUT);
}

void loop()
{

val = analogRead(analogPin); // read the input pin
if (analogPin < 0.0)
{
digitalWrite(ledPingreen, LOW);
digitalWrite(ledPinyellow, LOW);
digitalWrite(ledPinred, LOW);
}
if (analogPin < 0.1)
{
digitalWrite(ledPingreen, HIGH);
digitalWrite(ledPinyellow, LOW);
digitalWrite(ledPinred, LOW);
}
if (analogPin < 0.2)
{
digitalWrite(ledPingreen, HIGH);
digitalWrite(ledPinyellow, HIGH);
digitalWrite(ledPinred, LOW);
}
if (analogPin < 0.3
{
digitalWrite(ledPingreen, HIGH);
digitalWrite(ledPinyellow,HIGH);
digitalWrite(ledPinred, HIGH);
}
}

I've made a lot of scriphts, but nothing seems to work? but this is what i've used last time, but it still wont react, PLEASE tell me if I does it all wrong

void loop()
 {
   VAL = analogRead(analogPin);   // read the input pin. 0V = 0, 5V = 1023
 if ( VAL < 20 ) // signal below 0.1V
 {
   digitalWrite(ledPingreen, HIGH);
   digitalWrite(ledPinyellow, LOW);
   digitalWrite(ledPinred, LOW);
 }
 if ( (VAL > 20) && (VAL < 40) ) // signal between 0.1V and 0.2V
 {
   digitalWrite(ledPingreen, HIGH);
   digitalWrite(ledPinyellow, HIGH);
   digitalWrite(ledPinred, LOW);
 }
 if ( (VAL > 40) && (VAL < 60) ) // signal between 0.2V and 0.3V
 {
   digitalWrite(ledPingreen, HIGH);                                          
   digitalWrite(ledPinyellow,HIGH);
   digitalWrite(ledPinred, HIGH);
 }
}

Hi BK2505. Have you spotted what you did wrong from madworm's code?

Analogue input dose NOT give you a floating point number it gives you an integer between 0 and 1023.

i alread have done that,

No you haven't, if you had there is no way you would measure 0.1 volt on the input.

thanks madworm
but i'v just tryed it out.
do u think i'm doing anything wrong.
Grumpy_Mike, how do i make a skecth that prints the output, from the microphone :slight_smile: :slight_smile:

try this:

int val = 0;         

void setup()
{
 // note that you should not call pinMode for the analog pins
 Serial.begin(9600); // initialise serial port for 9600 baud
}

void loop()
 {
 
   val = analogRead(analogPin);   // read the input pin
   Serial.println(val,DEC);
   delay(250); // delay 1/4 second between readings
}

Thanks, but when i upload it to the board, the green light just lightning up and, there's no reaction else. :cry: :cry:
I REALLY DONT KNOW WHATS WRONG :frowning: :frowning: :frowning:

What Grumpy Mike and Mem are trying to help you with is to print the values you are measuring out to the serial port. In order for you to see anything, you need to ask your computer to show you what the Arduino is sending it.

In the IDE (the software on your computer you use to send your programs to the Arduino) there is a row of buttons you must be using for this. There is one to compile, one to save and one to send the program to the Arduino. After that, last button on the right, there is one called serial monitor.

Try clicking on the serial monitor button just after you have sent Mem's code to your Arduino, and hopefully after a little wait you should see some numbers appearing in a new window. These numbers are the data measured by your Arduino.

Once this works, please tell us what those numbers are (an average value is fine, we don't need hundreds of samples).

Good luck!

thanks, BUT i dont know what i am doing wrong, because,when the i put the input in, the readen value i 0...
but when i take it out the numbers go to 200 to 300...
and when i take the microphones ground off, the numbers go to 1023...
is it my microphones connection, to the other components that is wrong

It's normal to get a floating value if there is nothing connected to the pin when you read it (i.e. when you disconnected your microphone), so that is nothing to worry about. Odd values in the absence of a ground connection are also normal.

If you have a potentiometer (a variable resistance with a knob you turn to change the value), you could try to connect one end of it to 5V, the other to ground (the same ground as your Arduino) and to put the middle connection (the one with the changing value) to your microphone input.

You should be able to see the value in the serial monitor, with Mem's code running, go from nearly 0 to nearly 1023 as you turn the knob.

If you do not have a potentiometer, you can try the same thing with various pairs of resistors in series... something called a voltage divider.

i wil be abel to try it out with a potentiometer on monday. but so far thank you :smiley: :wink:

You're welcome and good luck on Monday! :wink:

Hey again
I tryed it with a potentiometer, and i got it to work like i would like with the microphone, but when i connect the microphone again, and change the values, it doesn't work

Well, now we know that your circuit works to measure 0-5V signals.

This probably means that the signal from the microphone is too small relative to the 0-5V amplitude you are measuring, as Mike was saying earlier, I believe.

You will need to amplify it somehow. You should mention this to your professor as how to do this will depend on what parts you have available in class.

I have already amplified the signal, the level is now 0.2V. What do you suggest?

So the arduino measures 0-5V just fine with the potentiometer, and you know you have 0.2V at the amplified microphone, but when you connect the two, the arduino measures 0?

(going to lunch, by the way, so I won't be able to reply for a bit)

I have already amplified the signal, the level is now 0.2V.

Is this measurement taken before you connect it up to the Arduino's input or when it is connected up?

I suspect that the output of your amplifier is being killed by the arduino's input impedance. Have you connected this up through a capacitor like I said earlier or is it a direct connection? Have you connected the -ve supply on your amplifier to the GND on the arduino. Note that GND on the amplifier is NOT the same as GND on the Arduino if you are running it all off the same supply.