The analog pins of my atmega128 are acting very strange. The pins don’t read accurately and seem to be influencing each other.
When I solder one of the analog pins to ground it does not read solid zeros but the readings fluctuate from 0 to about 16. If I solder it to VCC I get readings from about 1010 to 1023. If I connect a voltage divider the readings change across a similar range.
The analog pins also influence each other. If I solder one pin to ground a nearby analog pin starts reading around 50. If I solder a pin to VCC the nearby pin starts reading around 990.
This problem is happening across multiple chips and boards. I have tied AVCC and AREF to VCC and tried to use the internal as well as external voltage reference to no avail. I am using decoupling capacitors and have tried different power supplies. I am using the MegaCore bootloader.
Could someone tell me what is causing this odd behavior of the analogRead function?
Thanks!!!
IMO it is. If you tie an analog input to GND, it should read 0 or at most 1 if there's something already somewhat fishy going on with the PCB. Anything over 2 is problematic.
@Sunnyflorida I understand this is a custom/DIY PCB. Could you post the schematic, PCB layout and clear photos of top and bottom sides of the PCB?
Idahowalker, I have implemented the dummy reading and the cap from AREF to Ground. The readings have improved a lot and should be usable with some filtering as you suggested.
I am not used to a grounded pin reading anything but zero. It does not seem right that I still get readings as high as 8 with a pin soldered directly to ground. I've never experienced that with the Atmega328.
I know the circuit will work with your helpful suggestion but I feel a little uneasy about the microcontroller not reading what (I think) it should read...
I have etched a minimalistic version of my board and this time it works perfectly. Soldering to GND reads all 0s and VCC reads all 1023s. I'm now going to troubleshoot my real board and I will let you know what I found. Thanks for everyone's input.
Floating pins are still influenced by their neighbors but that is conceivable, I just did not expect such a large effect, even for pins that are not neighboring each other.
I found the problem. It was noise caused by power supplies, one of which I have used without issues on many atmega328 projects. No number of decoupling capacitors would solve the problem. Evidently the atmega128 is very susceptible to any kind of power rail noise.
I am now using a linear regulator and I have no more problems with the analog pins. Thanks everyone!