Let's Make Robots!

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!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

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! 

@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?

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 ---> 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 the speaker?

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

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

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.

 

 

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.

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.

The 0.1uF Capacitor is way too small. Use between 10uF and 100uF. That should improve your volume.