I discovered a problem with the Arduino “analogRead()” function while running the “Example4_analogRead” code within the Arduino IDE 1.8.11 on a Redboard Artemis board. The example code is located within the “Examples\Artemis Examples” folder in the IDE. The problem I have found is that the following call to the function analogRead:
int myValue1 = analogRead(A3);
causes a subsequent call:
int internalTempAnalog = analogRead(ADC_INTERNAL_TEMP);
to produce an incorrect result. If I comment out the first call then the subsequent call produces a correct result. This only affects the ADC_INTERNAL_TEMP results. The call for the battery divider div3:
int div3 = analogRead(ADC_INTERNAL_VCC_DIV3);
is unaffected even though it precedes the call for the temperature results. I see the same problem for any of the ADC pin numbers A0 thru A5. If I substitute ADC_INTERNAL_TEMP for A3 in the first call the problem goes away. The problem occurs with or without an input to the A3 pin.
With the analogReadResolution set to 10 bits (problem occurs for other resolutions as well) and a 3.3V input present on A3 I get the following analog values:
A3 = 1023, Div3 = 571, internalTempAnalog = 160
With no input to A3 I get:
A3 = 164, Div3 = 571, internalTempAnalog = 41
With the call to A3 commented out I get (with or without an input to A3):
Div3 = 571, internalTempAnalog =514
The analog value of 1023 for A3, with 3.3V applied, translates to an input voltage of 2V (given an ADC reference voltage of 2V), which is expected because the ADC upper detection limit is 2V. The analog value of 164 could be anything because the pin is floating.
The analog value of 571 for div3 translates to a VCC voltage of 3.34V, which is correct.
The analog value of 514 for internalTempAnalog translates to an ambient temperature of 68 F, which is correct for my environment. The other analog values of 41 and 160 are incorrect.