digitalWrite() writes wrong pins

Atmega328P processors (including Arduino Nano). Very simple program:

void setup() {
  pinMode(5, OUTPUT);
  pinMode(A6, OUTPUT); //20
  pinMode(A7, OUTPUT); //21
  digitalWrite(5, 1);
}

void loop() {
  digitalWrite(A6, !digitalRead(A6));
  digitalWrite(A7, !digitalRead(A7));
  delay(2500);
}

When program is running, pins D10 and D13 are triggered. Nothing happens on A6 or A7. What the heck?
Arduino ide 1.8.16, Arduino AVR boards version tested 1.8.5, 1.8.6.

The Nano A6 and A7 pins are strictly analogue only pins. They cannot be used as digital pins

4 Likes

OK, didn't know that. But why are D10 and D13 flashing instead?

even if you delete wrong commands?

That is a bug.
Reading and writing to digital pins that do not exist should not result in the usage of other pins.

The Wokwi simulation is so good, that it also simulates this Arduino bug:

Source code of digitalWrite(): https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/wiring_digital.c#L138

1 Like

Koepel is right. Even as i didn't know about A6 and A7 being different, if they would not work only, I would quite quickly figure out what is wrong. But got off the track because some other pins where triggered instead. I was looking for a problem in my code first.

If nothing at all happened if you used digitalWrite() on A6 and A7 where would you look first ?

2 Likes

But something did happen. Pin 10 and 13 start blinking.

I think @UKHeliBob is asking out of curiosity or to create a learning moment.

Like where on the list A, B, C &c. would you find "check to see if this pin can be used as a digital input pin"?

For me that would have been the last thing I checked. Last because it would end the checking. Last because about 4 or 5 other things would have come before.

I blame whoever labeled the Arduino pins. How many ppl know out of the box that any analog pins can be used as digital pins?

Then the next thing we learn, when running out of pins, is that you can use analog pins. Yes, sometimes someone will say A0..A? (see, I can't even finish that thought) can be used as digital pins. Maybe mentioning on an UNO.

We go away thanking the stars that analog pins can be used as digital pins. Because that day will come.

And get bited sooner later when we go to use An where that is a strictly analog pin.

Same same for PWM capable pins. And why can servos be used on many pins, they do PWM, right? (I know, don't explain that, just sayin').

So there's a crap-ton of stuff that if you don't know now you know.

Of course it's all documented. Documentation seems to be a 20th century concept - now we more like poke around, look at examples, ask on fora kind and unkind, improvise, experiment, extrapolate and so forth, all at our own peril.

Maybe we find one page that has the short list of pins, what they can and cannot do listed and labeled and sorted by board type. More likely, such a page does not exist.

FWIW I would probably have moved to another pin, but gotta say that the bug might have kept me off track.

a7

The point that I was trying to make, obviously not clearly enough, is that @zarsss implied that he/she wasted time first looking for a problem in their sketch because something unexpected happened when it was run because 2 other pins were affected. I wondered where they would have looked first if nothing had happened when the sketch was run.

My suspicion is that they would wasted just as much time time looking for a problem with the sketch either way

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.