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.
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.
@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.
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 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.
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: