Strange AnalogRead Behavior of ATMEGA128

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!!!

Simplified code:

void setup(){
  Serial.begin(9600);}

void loop(){
  Serial.print(analogRead(A4)); 
  Serial.print("\t");
  Serial.println(analogRead(A6));
  delay(100);}

It is not odd behavior at all.

There are things you can do to settle the readings down. One is to take one dummy reading and then take a real reading like this;

Then there are things called filters. One kind of filter is a rolling average filter like this; GitHub - sebnil/Moving-Avarage-Filter--Arduino-Library-: A moving average, also called rolling average, rolling mean or running average, is a type of finite impulse response filter (FIR) used to analyze a set of datum points by creating a series of averages of different subsets of the full data set.. I use a Kalman Filter to do my data smoothing like this; SimpleKalmanFilter - Arduino Reference.

With the Arduino Uno and Mega, I'd shove a 103 ceramic cap into the aref pin and ground to help smooth the readings.

Do use the cap but do not connect AREF to VCC.

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

Thanks for the correction. I disconnected AREF and implemented the cap.

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.

Right; that's promising. I suspect a grounding issue with your other boards; i.e. one of the uC's GND pins not actually connected to GND.

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!

That does sound somewhat plausible indeed, although I'm surprised the power supply did not give any problems with 328P's.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.