Hi there,
With the script shown here (fom Alex @tinkerlog) i want to drive the led trough audio like an light organ.
I have connected my audio output from my pc to the input pin2 on the arduino.
I did not use an opamp circuit to amplify the audio signal before it reaches the arduino input pin.
What goes wrong? Is the output from my laptop (set at max. volume) still to low?
Thanx anyway,
Moekie, "The Arduino Newbie"
/*
* Monitor for sound sensor
*/
int potPin = 2; // select the input pin for sound sensor
int ledPin = 13; // select the pin for the LED
int value = 0;
int amp = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin);
amp = (val >= 512) ? val - 512 : 512 - val;
if (amp > 100) {
digitalWrite(ledPin, HIGH);
delay(20);
}
else {
digitalWrite(ledPin, LOW);
}
}
Hi Arne,
What do you mean by transfer some samples over the serial line?
I tried to measure the audio signal with a voltmeter but no voltage was produced. Is this the correct way to measure?
Moekie.
u should set the mode of the potPin, too:
pinMode(potPin, INPUT);
in setup() u can initialize the serial communication with:
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
see Serial.begin() - Arduino Reference
then u can use
Serial.println(analogRead(ledPin));
in loop() in order to send the samples to the serial port...
how do u connect ur arduino to ur computer?
with usb?
in the IDE (integrated development environment) is a button right off the programming-button, that reads from the serial port...
the voltmeter shows u an average value, which is expected to be 0...
that means, that ur signal contains negative voltage...
maybe the arduino doesnt like negative voltage?
the voltage limits for input pins r: -0.5 and 5.5 (Vcc+0.5) according to the datasheet...
u should consider using
analogReference(INTERNAL);
(this will reduce the resolution from about 5mV to about 1mV)...
This quite confusing for me! :-[
So, if i understand what u try to say is that i need to check the input pin? Can u help with the code for this. Mine isnt working well... :-/
/*
* test serial port
*/
int pinMode(potPin, INPUT);
int potpin = 2
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(potPin, INPUT);
}
void loop() // run over and over again
{
Serial.println(analogRead(ledPin));
}
You would need a lot more code before you could get good results this way.
I recommend a hardware approach to getting the signal processed before you connect it to the Arduino. The op-amp not only amplifies the signal, it can also be used to create an ideal diode and peak detector.
I have used the circuit below with an Arduino and it works very well. The image is an LTSPICE simulation with a virtual audio source.
/*
* test serial port
*/
int potpin = 2
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
digitalWrite(potPin+14,LOW); // make sure that pull up resistor is disabled -- this is usually not necessary
pinMode(potPin+14, INPUT); // set input mode for ADC pin potPin -- this is usually not necessary
}
void loop() // run over and over again
{
Serial.println(analogRead(potPin));
}
this would read from analog pin number 2...
if u r sure that ur audio signal doesn't go below 0.5V and not above 5.5V, u should be able to c some values...
have u found the "read from serial port" button in ur IDE (it's right of the upload button)?
@Arne
I will try this first to see if everything is ok.
Thank you for helping me out with the script and yes i found the serial button. @Macegr
Hi, after the test above i wil try out the circuit. Where do i connect my audio input (from pc) to. Which connection of this circuit goes to the arduino board?
Sorry guys for my limited knowledge of scripting and electronics. I just can't wait to build this little project an most of all to get it running!
Thanx!
Hi guys!
After running the test script i saw that the arduino was doing something. That gave me my confidence back.
I used the following script (via examples in the arduino compiler) to get this simple project running.
/*
*
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int ledPin = 13;
int potPin = 2; // connected to speaker output pc
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin); // read the value from the sensor or audio input
digitalWrite(ledPin, HIGH);
delay(val);
digitalWrite(ledPin, LOW);
delay(val); // stop the program for some time
}
As you can see on the youtube link it is almost acting as an light organ. However, when no audio is played the led keeps on?
How can i make the led react faster?
i think when the audio signal is off, val is 0...
that means the LED flickers with about 10kHz, which cant be recognized by the human eye (50Hz r already difficult)...
for my taste the LED was too fast in the youtube movie...
maybe u want to build the average value over 1 sec, and
if the average is high (what would be high values?), u would turn on the LED,
else u would turn the LED off...?
int avg = 0;
void loop() {
val = analogRead(potPin); // read the value from the sensor or audio input
avg = (avg * 127L + val) / 128; // *duck* i dont know what kind of average this is... :-)
digitalWrite(ledPin, avg > 100 ? HIGH : LOW); // use lower border for more HIGH-time
delay(10); // use lower delay, if it doesnt flicker enough
}
Arne,
Tried several values but led stays off.
Can you explain -in short- the lines of code:
digitalWrite(ledPin, avg > 100 ? HIGH : LOW);
avg = (avg * 127L + val) / 128;
I put the pinMode(potPin, INPUT);line in but is it necessary?
/*
*
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int ledPin = 13;
int potPin = 2; // connected to speaker output pc
int val = 0; // variable to store the value coming from the sensor
int avg = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
pinMode(potPin, INPUT);
}
void loop() {
val = analogRead(potPin); // read the value from the sensor or audio input
avg = (avg * 127L + val) / 128; // *duck* i dont know what kind of average this is... :-)
digitalWrite(ledPin, avg > 100 ? HIGH : LOW); // use lower border for more HIGH-time
delay(10); // use lower delay, if it doesnt flicker enough
}
yup
do u know this expression: (A ? B : C)?
it means "if A then use expression B else use expression C"...
so if "avg > 100" turn on LED else turn it off...
this avg = (avg * F + val) / (F + 1)
computes the new "average" out of the last "average" and the new value... the last "average" weights F times more than the new value...
the problem seems to be that the division by 128 (==F+1) makes the resulting "average" 0...
example: in the beginning: "avg = (0127 + val) / 128", so val would need to be greater than 128 to make avg non-zero...
maybe this works better:
digitalWrite(ledPin, avg > max/2 ? HIGH : LOW);
avg = (avg127. + val)/128;
max *= .999;
if (avg > max)
max = avg;
and avg should be a float (not an int)... max is a float, too (declared before avg and initialized with 0: "float max = 0;")...
can u measure the current through the LED when the pin is HIGH?
a single pin can't do more than 40mA...