2

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

2 Answers 2

5

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
    Commented Nov 13, 2020 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
    Commented Nov 13, 2020 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
    Commented Nov 13, 2020 at 21:00
0

const int buttonPin = A7; //is an invalid statement because A7 is not an integer.
Use a number instead:
const int buttonPin = 21; // 21 is the decimal pin number for A7.
Don't use A6 or A7 for pinMode(21, INPUT); on Nano, they don't work. You can use analogRead() (returns 0..1023) and then convert it to wanted boolean value (eg. >511 or <511 for crossing half Vref level)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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