I found out the hard way today that pins A6
and A7
can't be used as digital outputs, but what actually happens when you try to read/write them?
Some backstory: I soldered some headers onto a pro mini and I wanted to test all the pins to make sure they worked. I wrote a test program that looped through all the digital pins once a second and toggled their state by doing digitalWrite(pin, !digitalRead(pin);
. I then added a second loop that went through pins A0
through A7
and toggled them using the same method. After adding this loop the built in LED on pin 13 no longer toggled on and off every second. Instead, it would just flicker on very briefly. At first I assumed there was something wrong with my loops (like an off by one) that was causing the strange issue, so I completely rewrote things to use a single loop that iterated over all the pins. The resulting code should have been effectively equivalent to the original code, but now pin 13's LED was toggling properly again.
After much experimenting, I was able to whittle it down to the following test program to demonstrate the strangeness:
#define WORKS_CORRECTLY 1
int pins[] = {13, A7};
#define SIZE (sizeof(pins) / sizeof(int))
void setup()
{
#if WORKS_CORRECTLY
for(int i = 0; i < SIZE; i++)
pinMode(pins[i], OUTPUT);
#else
pinMode(pins[0], OUTPUT);
pinMode(pins[1], OUTPUT);
#endif
}
void loop()
{
for(int i = 0; i < SIZE; i++)
{
int pin = pins[i];
int pinVal = digitalRead(pin);
digitalWrite(pin, !pinVal);
}
delay(1000);
}
With WORKS_CORRECTLY
defined as 1
the LED on pin 13 toggles state every second like one would expect. With it defined as 0
, however, it just flickers on very briefly every second. This is very perplexing.
Ultimately the answer is to not try to do a digitalWrite
to A7
(or A6
), but I'd like to understand why the behavior changes based on seemingly irrelevant differences.