Hi,
I am going crazy at the moment since my analog pins are behaving strangely: I am trying to read a voltage from an analog Arduino Due pin using the following code:
float ReadVoltage(int Pin)
{
return float(analogRead(Pin)) / 1023.0 * 3.3;
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(String(ReadVoltage(A0))+"V\t"+String(ReadVoltage(A1))+"V");
delay(300);
}
If nothing is connected to both analog pins, I receive following values:
1.36V 1.90V
1.46V 1.70V
1.28V 1.67V
1.26V 1.61V
1.22V 1.64V
1.25V 1.58V
1.21V 1.63V
If I connect the 3.3V pin to analog pin 0 (pin 1 is not connected), the serial monitor shows following values:
3.30V 3.30V
3.30V 3.30V
3.30V 3.30V
3.30V 3.30V
3.30V 3.30V
3.30V 3.30V
3.30V 3.30V
Why do both pins read a voltage of 3.3V now?
If I connect the 3.3V pin to analog pin 1 (pin 0 is not connected), the serial monitor shows following values:
1.42V 3.30V
2.07V 2.76V
2.56V 2.74V
2.55V 2.76V
2.56V 2.73V
2.55V 2.75V
2.56V 2.74V
The first value of analog pin 1 is correctly 3.3V, then both seem to read random values. Why? This only works after a reset. If I connect the 3.3V pin to analog pin 1 while the program is running, I never read a voltage of 3.3.
This behaviour influences my readings, even if both analog pins (0 and 1) are connected:
Example:
I connect 3.3V to analog pin 0 and 2.0V to analog pin 1 and read A0 and A1 in the loop-function, I receive the following values:
A0: 3.30 //this is correct
A1: 1.98 //this is correct
A0: 3.30 //this is correct
A1: 3.30 //this is wrong, should be around 2.0V
A0: 3.30 //this is correct
A1: 3.30 //this is wrong, should be around 2.0V
(...)
Conclusion:
- Just reading one analog pin in a single sketch/program just works fine as expected.
The correct reading of a single analog pin is independent of the analog pin number. All pins work fine if only one pin is read per program. - Reading more analog pins in one single sketch/program fails.
- The first reading of the analog pin with the greater number succeeds, the following ones fail and return the voltage of the pin having the lower pin number.
- Switching the reading order (first reading the analog pin with the higher pin number) allows two readings of the analog pin number to succeed instead of only one (see previous point).
- I can reproduce those issues on two Arduino Due boards.
Can anybody reproduce this issue and/or help me?
Thanks in advance.
Kind regards
Birk