Why are A6 and A7 defined when I compile for the Uno

So I’m running out of digital inputs and decide to use the analogs. I have a brain cramp and start using them from the end of the board inward (shorter jumpers to my breadboard) and start at A6 working down but only need to borrow 4 analog pins.

The resulting sketch compiles fine but A6 always returns zero. I put a logic analyzer on and the breadboard is behaving correctly, driving A6 to zero and one as designed.

To everybody but me the problem is obvious - the Uno only has 6 analog pins, known as A0 to A5. To me the question is, why does the IDE even define A6 (and A7) IF THEY DON"T EXIST? Why allow this coding error to compile? I can’t use A8 unless I compile for a board that has 16 analog pins but I can use A6 on the classic Arduino Uno.

Turning on the verbose option, I dig down and discover that avr/variants/standard/pins_arduino.h contains the following lines:

#define NUM_DIGITAL_PINS 20
#define NUM_ANALOG_INPUTS 6
#define analogInputToDigitalPin(p) ((p < 6) ? (p) + 14 : -1)

#define PIN_A0 (14)
#define PIN_A1 (15)
#define PIN_A2 (16)
#define PIN_A3 (17)
#define PIN_A4 (18)
#define PIN_A5 (19)
#define PIN_A6 (20)
#define PIN_A7 (21)

static const uint8_t A0 = PIN_A0;
static const uint8_t A1 = PIN_A1;
static const uint8_t A2 = PIN_A2;
static const uint8_t A3 = PIN_A3;
static const uint8_t A4 = PIN_A4;
static const uint8_t A5 = PIN_A5;
static const uint8_t A6 = PIN_A6;
static const uint8_t A7 = PIN_A7;

Why explicitly #define the number of analog pins as 6, then #define PIN_A6 (and 7) as well as the associated static constants, all in the same header file? Other device-specific pins_arduino.h are internally consistent in this respect, just not the Uno.

If this is a pro mini, A6 an A7 are analog only.

The Arduino AVR Boards standard variant file is also used for the Pro Mini and Nano, which do have A6 and A7 broken out, as well as the Uno. The only difference is that NUM_ANALOG_INPUTS is set to 8 but that macro is almost never used (the only place I've seen it is in Firmata). The DIP Unos don't have A6 and A7 but on the SMD Uno, even though they're not broken out to headers, you could tack some magnet wire to those pins if you wanted.

larryd: If this is a pro mini, A6 an A7 are analog only.

Nice answer to a completely different question.

Well, the SMD UNO uses the same 238 chip as the Pro Mini hence the SMD chip has A6 and A7.

pert: The Arduino AVR Boards standard variant file is also used for the Pro Mini and Nano, which do have A6 and A7 broken out, as well as the Uno. The only difference is that NUM_ANALOG_INPUTS is set to 8 but that macro is almost never used (the only place I've seen it is in Firmata). The DIP Unos don't have A6 and A7 but on the SMD Uno, even though they're not broken out to headers, you could tack some magnet wire to those pins if you wanted.

Why does the IDE bother to ask for which specific board, out of 28, I'm using, if it really doesn't use the answer? Why bother with all the extra work to #define NUM_ANALOG_INPUTS as 8 in an additional header file and not check here (#ifndef NUM_ANALOG_INPUTS) before defining it as 6, only to completely ignore both cases?

Doesn't make sense, isn't sound software engineering, exhibits poor human factors, would fail a code review where I worked, but at least it is free!

@jmmilner, I agree with you, pin A6 and A7 should not be defined when using the Arduino Uno. But they are. Ignore it and go on ;)

There are many more things which are annoying. Arduino is to learn programming and for fast prototyping. It has become popular because of that. Overall it is a good thing and I'm a big fan of Arduino, despite some annoying things.

All the ATmega328P chips use the same chip die. That means that the DIP version of the Arduino Uno does have A6 and A7. However, they are not connected to pins of the ATmega328P DIP. When you read the analog signal of A6 and A7 using the registers, it is possible to read noise from them. That noise even changes when you put your hand over the ATmega328P DIP.

When you read the analog signal of A6 and A7 using the registers, it is possible to read noise from them.

If that’s the case there’s an application for their use.

Let’s say you are using all the analogs on your UNO. Now let’s say you want to create a random number. Use the noise from A6 or A7 as a seed.

"Use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin."

jmmilner: To everybody but me the problem is obvious - the Uno only has 6 analog pins, known as A0 to A5. To me the question is, why does the IDE even define A6 (and A7) IF THEY DON"T EXIST? Why allow this coding error to compile?

I'm reminded of the old joke:

Pateint: "Doctor, it hurts when I do this." Doctor: "So, don't do that."

jmmilner: Why does the IDE bother to ask for which specific board, out of 28, I'm using, if it really doesn't use the answer?

It does use the answer. Select Tools > Board > Arduino Pro or Pro Mini and then try uploading to your Uno. I suppose due to historical precedent, the Pro Mini has a different bootloader than the Uno, and thus requires different hardware configurations

The same has been true for the Nano until the last week or so. Even now the two hardware definitions need to be a bit different because they forgot to change the boot section size when they changed to using the Optiboot bootloader.

So the Zen of Arduino is just different than what I'd grown to expect from my years of software development, including writing an industrial-grade C compiler and managing an SDE for a project where we measured development cycles in thousands of man-years (yea, I'm that old).

I'm off to share a park bench with another old man. Carry on.

It is all open source. If you don’t like it, change it. My years in this industry have taught me to be grateful when something is more or less usable.

jmmilner: So the Zen of Arduino is just different

I suppose the Zen is that the build environment does not stop you from doing weird, funky things with the underlying hardware, using undocumented features, that sort of thing. It's all pretty close to the metal.