ADC and strange readings

Hello guys,

I ran this sketch that would simply read an ADC port and print it out to my pc. It went just fine and results were pretty consistent, something like: 300 300 300 300 300 299 300 300 299 300 300 and so on....

But when I use Serial.print(analogRead(whatever_pin)); in a larger sketch, results are very different, something like: 300 290 305 288  310 315 306 296 and so on. This "larger" sketch has 3 interrupt service routines, and the hardware setup has 3 different chips and a loudspeaker.

I am not really sure why that happens. Is it software related (interrupt service routines ---> make the analogread lag?) or is it hardware related (lots of noise coming from chips ---> intereferes with readings)? Oh btw, i don't know if this really matter, but i am using pull up resistors on those pins (pinMode = input, digitalwrite = high) and i'm connecting "thing that gives the reading" between the ADC pin an GND.

 

Thank you!

"Sounds" like a job for a scope

Like Robofreak and his Sharps, you could use a scope to confirm any fluctuations in the voltage levels. Or exclude that as a possibilty.

I have had mixed experiences with soundcard/software scopes lately. But a ripple in an otherwise steady voltage should be pretty easy to find.

My first suspect would be the speaker. Easy enough to eliminate, I suppose.

It is slightly possible that

It is slightly possible that you are getting noise from your unused analog pins. You can use the internal pullups for the Arduino by setting the unused analog pins high.

digitalWrite(A0, HIGH);  // set pullup on analog pin 0

Most people don’t have a problem leaving the unused pins floating, but it is something you can try if your problem is not eliminated by the other steps that have been suggested. It only costs you a few lines of code to set them high. I have also read that setting your unused lines high can reduce your power consumption. See link below.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1243213127/all

 

 

 

@ignoblegnome: already had

@ignoblegnome: already had all analog pins set to HIGH, but thanks for the tip :=)

After further experiments i have come to the conclusion that when audio is being played there’s a lot of noise. Calling the same “check_adc” routine when there’s no sound gives consistent results. What could i do? Should i put a cap between the + and - of the speaker just like we do for motors, or shall i isolate/shield the ADC somehow?

speaker

How is the speaker wired up to the MCU? How big is it? What kind of power are we talking about? The last thing you want is to short out the AC signal that IS your audio. That may be what you’re suggesting.

MCU —> DAC —> AMP —>

MCU —> DAC —> AMP —> speaker 

that’s how everything’s wired up. Then i just connect the adc pin to a potentiometer. The speaker is 8 ohm 0.5W, DC current.

No capacitor in series to

No capacitor in series to the speaker?

nope. The amp’s datasheet

nope. The amp’s datasheet showed the speaker directly connected to the amp. It’s a TDA7052

Ok, I see in some schematics

Ok, I see in some schematics the capacitor in serie to the amplifier, like here:

2pte4wh.gif

But I'm not sure if this got something to do with your problem. Without a scope it's hard to say what goes wrong.

 

Oh, so you’re saying that

Oh, so you’re saying that all this noise generated by speaker/amp is not normal? I thought it was just ok and was searching for a way to shield the adc pins. Ok then, i’ll see if i can fix the amp somehow, and i’ll try to follow the above schematic strictly to see if it gets better. Thanks!

I don’t know if this has

 

I don’t know if this has anything to do with your problem, but the reason you normally put a cap in series with an audio speaker is to drop the DC voltage. The cap effectively blocks the DC, but lets AC (your audio signal) through. You should use a non-polarized cap on either of the speaker connections.

It seems unlikely to me that your speaker is causing interference with the microprocessor through the amp.

The amplifiier doesn’t like

The amplifiier doesn’t like DC ditto. But again, I don’t know if this is the cause for this problem.

Adding a .1uF cap in series

Adding a .1uF cap in series with the speaker makes the readings way more stable but also makes the sound almost unhearable :=( This confirms the speaker is the big problem. Shall I try shielding it? I’ve seen some guys on google that use foam pads for that and get good results.

I have tested various

I have tested various combinations of 33uF, 47uF and 10uF capacitors in parallel (in parallel to each other, in series with the speaker) and it looks like 33uF gives the best results, much better than having no cap at all. Here’s a comparison:

33uf cap: 228 228 228 229 229 231 229 229 234 234 229 230 228 229 237 228 223 239

no cap: 229 228 234 235 232 233 240 236 220 232 228 238 231 247 232 239

that’s a significant difference!