I want to use the analog pin A7
as a digital input pin. As stated in the Arduino docs the analog pins can be configured in the same way like any other digital pin.
However, when trying to configure an analog pin as a digital input pin, I get unexpected behaviour:
- Using a digital pin (e.g.
D2
) works as expected. - When using an analog pin (e.g.
A7
) the LED is lit after start-up without pressing the button and does not change/react to any button press. - Since the docs state, that analog pins can be configured as digital pins and used in the same way, this behavior seems to be kind of confusing.
See my attached snippet to reproduce/examine the issue:
// using pin 2 (D2) works as expected (LED is lit on button press only)
// const int buttonPin = 2;
// using analog pin 7 (A7 / pin 21) as a digital input is not working,
// LED is lit directly after start-up & does not react to button press,
// analog pins are generally usabable as digital inputs
// see https://www.arduino.cc/en/Tutorial/Foundations/AnalogInputPins
const int buttonPin = A7;
const int ledPin = 13;
int buttonState = 0;
void setup() {
// button is connected to GND, so enable internal pull-up resistors
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
};
void loop() {
// as we use internal pull-up resistors, logic HIGH & LOW are inverted
buttonState = !digitalRead(buttonPin);
if (buttonState) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
};