AnalogRead on A5 Reads Differently From Other Inputs

Simple circuit: 12V voltage divider, 4MΩ on top and 1MΩ on bottom, so voltage in the middle should be 20%, which gives me 2.4V as computed and measured on a voltmeter. It is low current (µ-amps) but that shouldn't matter. The 12V source is a very accurate desktop supply.

On Arduino Nano, analog reference is set to default. Pin modes for A0-A5 are all inputs.

When reading this voltage on A0 - A4, I get the digital equivalent of 2.4V every time it is read. However, when reading this on A5, I get lots of variation all over the range of about 1V to 4V.

I've tried all An pins and it's only A5. I've tried on 4 different Nanos with the same program, and the same each time.

So, I think it's not an issue with the specific Nano board. It's not an issue with the source. It must be software or ATmega328 design. The software that configures the analog pins is shown below.

void setup()
{
  analogReference(DEFAULT);
  for (uint8_t loop = 0; loop < 6; loop++)
  {
    pinMode(A0 + loop, INPUT); // Can I increment to A5 this way???
  }

  pinMode(A6, INPUT);
  pinMode(A7, INPUT);

  Serial.begin(9600);
}

The only thing left that I can think of is that A4 and A5 are also shared by the hardware I2C functionality. I didn't include Wire.h nor call Wire.begin(), so it shouldn't be enabled. However, I got the sense it is enabled. A4 is SDA (data) if enabled and probably pulled down internally, so it wouldn't interfere with the reading. A5 is SCL (clock) if enabled and would cause a pulse train, affecting the reading.

Besides the obvious question of "what's going on", I have the follow up questions of "Is there some way to read the state of the hardware I2C to see if it is enabled or disabled?" and "Can I forcibly disable it?".

Can I guess you are using a plug-in breadboard with a bunch of wires?

Paul

is A5 used for any other purpose?

I have learned to avoid I2C & SPI dedicated pins.

Your multi-megaohm voltage divider isn't that far from acting a floating pin. It's not surprising you get weird results from some pins.

From the datasheet:

The ADC is optimized for analog signals with an output impedance of approximately 10 kOhm or less. If such a source is used, the sampling time will be negligible. If a source with higher impedance is used, the sampling time will depend on how long time the source needs to charge the S/H capacitor, with can vary widely. The user is recommended to only use low impedance sources with slowly varying signals, since this minimizes the required charge transfer to the S/H capacitor.

@Paul, everything is soldered. I have 4 Nanos with configured the same way. It's not a one-off kind of problem.

@Gypsum, I kind of have the same sense, but odd that A5 is different. Unless the internal selector to pick between A2D conversion and I2C and digital might have some bearing on this. I'd love to step up the current through the divider, but unfortunately it is a test for continuity through a hair thin wire that would fail with more current.

@Geek, A5 is written to a 0 from the digital standpoint, set to input from the digital standpoint, and I2C/SPI is never enabled. It shouldn't be used for anything as far as I know. Is there anything else I can check? Do you know how to tell if the I2C is enabled, or force it to be off?

@John, Thanks for the quote! Do you have a reference to the section. I had been looking for something like that in the Atmel ATmega328 documentation but all search were too vague or too detailed for me to understand (data sheets). Any thoughts why A5 is different from A0 through A4? Would love to know how to see if I2C is enabled, or how to force it off in code. Thanks again!

put an LED from A5 to ground. if it lights up, you don't know what the problem is, but you know you have one.

@Geek, I can measure the voltage without anything connected and it's effectively zero.

I was measuring voltage with a quality voltmeter and the readings settled down completely. I'm going to propose a theory to why this is happening. Pulling in both John and Gypsum's thoughts, the voltmeter (Tandy auto-ranging) must also charge an internal capacitor too, then do an A2D on the cap reading, but the cap can be constantly charged from the voltage divider circuit, whereas the A2D needs to charge in the short time it takes to sample. This voltmeter cap is helping charge the cap in the ATmega328 A2D. When the voltmeter is gone, the voltage divider is such low current that it isn't able to charge the cap in the A2D. But why A5 and not the other analog pins? It has to be the switch/selector circuitry for A/D/I2C functionality.

So, I am going to try a small cap on the A5 pin. Anyone have a recommendation on size (10uF)?

15pF capacitor on the input appears to resolve the issue. Still hoping for some advice on a more recommended value. :slight_smile: Thanks everyone for the help.

mfort:
@John, Thanks for the quote! Do you have a reference to the section. I had been looking for something like that in the Atmel ATmega328 documentation but all search were too vague or too detailed for me to understand (data sheets). Any thoughts why A5 is different from A0 through A4?

It's one of the four places in the datasheet where the word "impedance" is used.

In "ATmega48A/PA/88A/PA/168A/PA/328/P (c)2018 Microchip Technology Inc. Data Sheet Complete DS40002061A" it's in section 24.6.1, Page 253.

Have you looked at the Arduino Nano schematic or examined the traces? The reference design doesn't seem to have any major difference between the A5 trace and any of the others. The A5 trace is a little longer but I don't think it is significantly longer.

4MΩ on top and 1MΩ on bottom

That's a really high impedance for an ADC. The AVR doesn't actually mention a current requirement, but the inherent capacitance can put limits on the speed of conversion.

It woudn't surprise me to have the pins shared with I2C would behave slightly differently than the other analog pins.