Looking for a little advice. I have an analog input (specifically a microphone) which works fine when I have no output running. However when I have the input drive an output (specifically the PWM digital pins 9, 10, 11 using the analogWrite function), I get all sorts of noise on the analog input. Any ideas how to fix this?
Which Arduino are you using? What is connected to the PWM pins? What is your power supply and what all is it powering?
I am using the Lilypad Arduino 328 Main board. At the moment I am just driving the power from either the 5V USB connection to a laptop using the lilypad FTDI breakout board or from a simple 3x 1.2V AA battery pack for a 3.6V supply.
The sensor is a mic with an op-amp (SparkFun Electret Microphone Breakout - BOB-12758 - SparkFun Electronics). The analog input reads it as 511 when there is no signal and 0 - 1023 when there is sound present. I'm doing some basic envelope amplitude finding and sending that to PWM output driving some LEDs. I intend to do some more complicated FFT analysis of the signal, but for now I'm trying to start simple.
When I don't send the signal to the LEDs and it is quiet I get a steady 511 +- 5 on the input (sound level ~0) and with sound I get a nice trace of the sound level as I can see using serial output to monitor the values. However once I send the sound level to the LEDs to show the level visually I get all sorts of noise on the input and even when it is quiet the LEDs flash randomly.
How much current are the LEDs pulling? Is there any difference between powering with USB and the battery pack? Are all the grounds connected? Post your code, please, in code tags.
Is there an easy way to verify how much current they are actually pulling? Nominally they were designed to pull 20mA each at 3.6 => 28mA at 5V. I have 8 drawing current at any one time so that should be 160 mA at 3.6V and 220 mA at 5V. They are common cathode with all the grounds connected to the minus pin and the positives to the PWM pins via current limiting resistors.
I don't know what a code tag is, but nevertheless a bare-bones version of the code is below. As I mentioned if I comment out the analogWrite statements I get a fine signal on the Mic level, but with them active I get a lot of noise and they flash constantly even with no background sound.
int MicPin = A1;
int RedPin = 9;
int GreenPin = 10;
int BluePin = 11;
int MicLevel = 0; // 0 - 1023 from ADC
int LedLevel = 0; // 0 - 255 for PWM
void setup(){
Serial.begin(9600); // monitor levels on serial viewer for debugging
}
void loop(){
MicLevel = analogRead(MicPin); // 0 - 1023 with 511 = silent.
MicLevel = abs(MicLevel - 511); // sound amplitude.
// To do: Low pass filter MicLevel and add FFT and other processing here later.
LedLevel = map(MicLevel, 0, 511, 0, 255); // remap to PWM range.
// To do: assign different levels to different colors. For now just test output:
analogWrite(RedPin, LedLevel);
analogWrite(GreenPin, LedLevel);
analogWrite(BluePin, LedLevel);
// Print values to serial
Serial.print("MicLevel= "); Serial.print(MicLevel);
Serial.print("LedLevel= "); Serial.println(LedLevel);
}
LED current = (supply voltage (Vcc) - LED forward voltage) / resistor value. I ran your code on my Uno with a RGB LED connected and using a pot instead of the mike. The MicLevel was very steady where ever I set it (±1 LSB) even with the LEDs at full bright. When you run off the battery, is it straight from the battery? Your description just leads me to a power or grounding problem. If Vcc fluctuates that fluctuation is passed on to the analog reference, hence fluctuating analogRead values even when the input voltage is steady.
You should read the 'how to use the forum" stickies at the top of the topics.
When using analog inputs, you should provide capacitors. On the power rail, and most important, on the analog pin.
Thanks for the suggestions! I'll have to try the capacitors next, especially on the input from the batteries. I'll let you know if I have any luck. I bought a mic with a op-amp on a breakout board since I had thought that a capacitor on the audio input was already taken care of (see https://www.sparkfun.com/datasheets/BreakoutBoards/Amplified-Mic-Electret-v14.pdf).
Meantime, last night I added a 10kOhm pull-down resistor between analog in and ground, which honestly I think had no effect at all. I dealt with the rest in software by adding a simple running average low pass filter and applying a threshold below which to leave the LEDs off. For now this works well enough for my purposes since I only care about the sound level, but I'd hate to think what the quality of the audio is if I wanted to record it. Nevertheless the LEDs flash when I'm talking or in time to music when playing which is all I need for now, but I will try the capacitors and let you know how it goes.
Thanks again.
Put a 0.1uF capacitor from AVcc to ground.