Syntax question regarding analog pins...

How do I reference an analog pin using a variable? Normally on pins 0 thru 13 this will work:

=============
int led = 13;
void setup() {
pinMode(led, OUTPUT);
}
void loop()
{
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}

What value would I assign to the variable "led" if I wanted to call pin A1 instead of pin 13? I tried using the value 15 but it didn't work. I can't assign an integer variable the value "A1".

Call it A1

(deleted)

The following will work:

int led = A1;
int ad0;
float av0;

ad0 = analogRead(0);    // read analog pin A0
av0 = ( (float)ad0 -offset0)/scale0;

does the same as

int ad0;
float av0;

ad0 = analogRead(A0);    // read analog pin A0
av0 = ( (float)ad0 -offset0)/scale0;

I think that A0=0; must get set in a header somewhere.

If you wish to use analog pins as digital pins, you need to look into the pins_arduino.h inside the arduino IDE. Each board has a different mapping. For UNO A0=14,...A5=19

Here is for Leonardo and micro:

// Mapping of analog pins as digital I/O
// A6-A11 share with digital pins
static const uint8_t A0 = 18;
static const uint8_t A1 = 19;
static const uint8_t A2 = 20;
static const uint8_t A3 = 21;
static const uint8_t A4 = 22;
static const uint8_t A5 = 23;
static const uint8_t A6 = 24;	// D4
static const uint8_t A7 = 25;	// D6
static const uint8_t A8 = 26;	// D8
static const uint8_t A9 = 27;	// D9
static const uint8_t A10 = 28;	// D10
static const uint8_t A11 = 29;	// D12

All of the Axx are const words equal to a number. As far as I know, all analog pins are defined to digital pins in a continuous manner so if you want to work on A0-A11, just do for (byte i=A0;i<=A11;i++)

Thank you!

moses1592:
Thank you!

Don't be fooled by my number of posts. I just learned this myself not long ago :wink:

You raised a good question. I had the same question and I wish I answered it earlier.

If you wish to use analog pins as digital pins, you need to look into the pins_arduino.h inside the arduino IDE.

Is that true ?

void setup()
{
  Serial.begin(115200);
  Serial.println(A0);
}

void loop()
{
}

Prints 14, which is what I would expect. No need to refer to pins_arduino.h yourself when the Arduino does it for itself.
pinMode(A0, INPUT)works perfectly well.

Using a Ax name (it's a macro substitution I believe?) makes your code portable across the various different arduino boards, where as using the numeric pin name is board specific.

UKHeliBob,

I understand your argument. But that means testing end results that obviously should be clear if you just read the source.

retrolefty:
Using a Ax name (it's a macro substitution I believe?) makes your code portable across the various different arduino boards, where as using the numeric pin name is board specific.

I don't think the analog pins were ever macros.
They seem to have showed up in 0019 in WProgram.h but now starting with 1.0 are in the variant pins_arduino.h
as liudr posted.
They are static const ints.

--- bill

It does not mater if it's a define (#define ...A0 )or a declaration (int A0) you must use the name not the number, one of the posters made the big mistake of saying use 0 for A0 which will not work (if will compile/verifiey ) but after that .....

Mark

liudr:
As far as I know, all analog pins are defined to digital pins in a continuous manner so if you want to work on A0-A11, just do for (byte i=A0;i<=A11;i++)

This is not guaranteed to be true.
The variant is allowed to define its pin mappings anyway it wants.
While they tend to be consecutive for most boards, not all boards do their pin mapping this way.
The Sleeping Beauty 1284p based board for example does not do its analog mapping
to consecutive digital pins.
Here is the mapping for that board:

static const uint8_t A0 = 21;
static const uint8_t A1 = 20;
static const uint8_t A2 = 19;
static const uint8_t A3 = 18;
static const uint8_t A4 = 17;
static const uint8_t A5 = 16;
static const uint8_t A6 = 30;
static const uint8_t A7 = 31;

So it is best to honor the variants right to do any mapping it wants and that means
you must use the symbolic name (A0, A1, etc..) and make no assumptions about how other pins
may be related.

Now you can pass in a naked constant. like 0, 1, 2, ... etc...
However, that is supposed to directly specify the AVR analog channel in the part
not the Arduino analog pin number.
Due to the flexibility in the pin mapping, the AVR analog channels are not necessarily mapped
1:1 to the Arduino analog pin numbers.
i.e. ADC channel 0 is not necessarily arduino pin A0

In fact on some of the 1284 cores, the ADC channel #s and arduino analog pin numbers are reversed
to make certain PCB layout issues easier.
i.e.
ADC 0 is arduino pin A7, ADC 1 is arduino pin A6 etc... to ADC7 is arduino pin A0

So again, if you want to use the pin labeled A0, A1, etc. on your board. you should use the symbolic name
rather than specify an arduino digital pin number, or even an AVR ADC channel #.
Using the symbolic names, ensures that the correct pin is used should you happen to change
boards to a board that uses a different pin mapping.

--- bill

liudr:
UKHeliBob,

I understand your argument. But that means testing end results that obviously should be clear if you just read the source.

Now it's me that doesn't understand your argument. You can refer to the analogue pins by their Ax names in any context for any board that you have selected and the code will work. What could be simpler than that ?
pinMode(10, OUTPUT);initialises the pin labelled 10 for output.
pinMode(A2, OUTPUT);initialises the pin labelled A2 for output.

No need to find and read the header file or do any testing beyond the norm.

Bob, OP wanted to know the digital pin numbers of analog pins. They are all defined in header files. Stick to the question. Referring to all analog pins programmatically was my best guess what OP wanted to do, not addressing one pin.

bperrybap:

liudr:
As far as I know, all analog pins are defined to digital pins in a continuous manner so if you want to work on A0-A11, just do for (byte i=A0;i<=A11;i++)

This is not guaranteed to be true.
The variant is allowed to define its pin mappings anyway it wants.
While they tend to be consecutive for most boards, not all boards do their pin mapping this way.
The Sleeping Beauty 1284p based board for example does not do its analog mapping
to consecutive digital pins.
Here is the mapping for that board:

static const uint8_t A0 = 21;

static const uint8_t A1 = 20;
static const uint8_t A2 = 19;
static const uint8_t A3 = 18;
static const uint8_t A4 = 17;
static const uint8_t A5 = 16;
static const uint8_t A6 = 30;
static const uint8_t A7 = 31;




So it is best to honor the variants right to do any mapping it wants and that means
you must use the symbolic name (A0, A1, etc..) and make no assumptions about how other pins
may be related.

Now you can pass in a naked constant. like 0, 1, 2, ... etc...
However, that is supposed to directly specify the AVR analog channel in the part
not the Arduino analog pin number.
Due to the flexibility in the pin mapping, the AVR analog channels are not necessarily mapped
1:1 to the Arduino analog pin numbers.
i.e. ADC channel 0 is not necessarily arduino pin A0

In fact on some of the 1284 cores, the ADC channel #s and arduino analog pin numbers are reversed
to make certain PCB layout issues easier.
i.e.
ADC 0 is arduino pin A7, ADC 1 is arduino pin A6 etc... to ADC7 is arduino pin A0

So again, if you want to use the pin labeled A0, A1, etc. on your board. you should use the symbolic name
rather than specify an arduino digital pin number, or even an AVR ADC channel #.
Using the symbolic names, ensures that the correct pin is used should you happen to change
boards to a board that uses a different pin mapping.

--- bill

Bill, there are bobsuinos etc that are not official. I will restrict the observation to official boards with atmel chips, which I checked. They all have sequential digital pin numbers for analog pins.

If you want to set all analog pins to do something and save SRAM, you may iterate through A0-Ax. Otherwise you do it with an array or repeat commands instead of using loop.

liudr:
Bob, OP wanted to know the digital pin numbers of analog pins

That is not what he asked at all. What he wanted to know was how to use a variable to refer to an analogue pin. As was correctly pointed out earlier
int led = A1;works fine. As I said

You can refer to the analogue pins by their Ax names in any context