Hi there,
I just ran into a strange problem with analogRead(), which seems
to ignore the requested internal pullup resistors:
analogReadResolution( 12 );
pinMode( A0, INPUT_PULLUP );
value = analogRead( A0 );
returns values about 1500 when the pins is not connected.
In other words, the pullup is NOT active. BTW, I also tried
analogReadResolution( 12 );
pinMode( A0, INPUT );
digitalWrite( A0, HIGH ); // alternative way to enable pullup
value = analogRead( A0 );
but the result is the same.
I guess this is due to a bug in cores/arduino/wiring_analog.c
because analogRead() internally calls:
pinPeripheral( pin, PIO_ANALOG )
which in turn disconnects the pullups from the pin PMUX... (it sets
the pins PORT_PINCFG_PMUXEN bit, which according to Fig 22-3 in
the SAMD21G18 datasheet disconnects the pullup/pulldown logic).
For now, I changed it like this:
// pinPeripheral( pin, PIO_ANALOG )
pinPeripheral( pin, PIO_INPUT_PULLUP )
which seems to work. But I am nowhere sure that this is the correct fix.
Anyway, the behaviour seems to be the same for all SAMD controllers:
am I the first one trying to read analog values with internal pullups enabled?
Best, Norman