Go Down

Topic: How to make the LED react on input (Read 3933 times) previous topic - next topic

BK2505

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... :'( :'(

Grumpy_Mike

#1
Sep 16, 2008, 01:04 pm Last Edit: Sep 16, 2008, 01:05 pm by Grumpy_Mike Reason: 1
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.

BK2505

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 :'( :'(

Oracle

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?

BK2505

#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

madworm

#5
Sep 18, 2008, 10:19 am Last Edit: Sep 18, 2008, 10:21 am by madworm Reason: 1
Code: [Select]
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);
}
}
• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Grumpy_Mike

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.

Quote
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.

BK2505

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 :) :)

mem

try this:
Code: [Select]

 
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
}

BK2505

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

Syvwlch

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!

----------
Mathieu

BK2505

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

Syvwlch

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.
----------
Mathieu

BK2505

i wil be abel to try it out with a potentiometer on monday. but so far [glow]thank you [/glow]:D ;)

Syvwlch

You're welcome and good luck on Monday!  ;)
----------
Mathieu

Go Up