1

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);
  }
};
4

A6 and A7 are special on the nano. They are the only pins that can't be used for digital. They are only analog inputs.

3
  • Is that due to Arduino's hardware abstraction / pin mapping or related to the capabilities of the Atmega328? – albert Nov 13 '20 at 17:02
  • It's the hardware capabilities of the ATMega328P-AU. Those pins only connect internally to the ADC MUX and don't have any digital drivers. – Majenko Nov 13 '20 at 17:02
  • @albert, in fairness to the authors of the website, it is likely true at the time they wrote that statement, that analog pins could always be used as digital. The DIP package ATMega328P, ATMega168P, and ATMega8 were used on the earlier Arduino boards. The -AU in the part Majenko mentions refers to a surface mount version of the chip that has pins not present on the DIP version of the 328P. You can see the lack and presence of A6 and A7 between the DIP and SMT versions of the Arduino UNO board. – timemage Nov 13 '20 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.