digital and analog pins

Hi everyone
Reading several guides and code examples, I've found that people using analog sensors refer to analog pins with the numbers 0 1 2 3 4 5
for example

var tempPin = 0
void setup()
{
Serial.begin(9600);
}
void loop()
{
reading = analogRead(tempPin);

but they also do it when referring to digital pins like:

int relay =7;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

  • // set up the LCD's number of columns and rows:*
  • lcd.begin(16, 2);*
  • pinMode(relay,OUTPUT);*
    }

Shouldn't Analog pins been refer as A0, A1, A2, A3?

Why does the code works even if you use the number without the A?

The IDE takes of things like that for the sake of artists and others that the IDE was intended for to make Arduino easy to use.

Using A0 and so on makes it independent of a board (Uno, Mega, ...). So that is the correct approach.

Imagine one board uses pins 0, 1, 2 and 3 (A0 .. A3) for analog inputs and another one uses pins 4, 5, 6 and 7 (the numbers are just fictive numbers).

If one uses the numbers and wants to develop for both boards, one has a problem as one needs to modify the code (every single variable relating to pins) when compiling for another board. The below solves the issue

#define BOARD1


#ifdef BOARD1
#define A0 0
#define A1 1
#define A2 2
#define A3 3
#endif

#ifdef BOARD2
#define A0 4
#define A1 5
#define A2 6
#define A3 7
#endif

Change the first line to #define BOARD2 if you compile for BOARD2. This is basically what the IDE does if you select a different board; it changes that first line.

To see what A0 stands for, you can use a line like below in your normal code.

Serial.print("Pin A0: "); Serial.println(A0);

This will print the pin number for A0. It will (hopefully) answer the question 'why it works'.

Thank you both for your answers.

Ok, so I followed your advice sterretje and printed A0 to check the pin number for A0.
It returned a value of 14. So A1 = 15, etc.

Having in mind the digital pins are labeled from 0 to 13, it makes sense that analog pins are a continuation of these.

BUT, I still don't get why, then, when in, for example, this code:

float tempC;
int reading;
int tempPin = A1;

void setup()
{
analogReference(INTERNAL);
Serial.begin(9600);
}

void loop()
{
reading = analogRead(tempPin);
tempC = reading / 9.31;
Serial.println(tempC);
delay(1000);
}

it makes NO difference where I define tempPin as A1, 1 or 15.

I understand using A1 or 15 should be the same
but if I choose to use 1
How does Arduino know i'm referring to Analong 1 or Digital 1?

Morke:
it makes NO difference where I define tempPin as A1, 1 or 15.

I understand using A1 or 15 should be the same
but if I choose to use 1
How does Arduino know i'm referring to Analong 1 or Digital 1?

That is the horror. It doesn't really know, but makes an educated assumption that since you are calling it with an analog function, you must mean it as an analog pin. The code in the analog function explicitly makes this conversion.

In my opinion, the confusion caused by this outweighs the slight convenience of being able to specify either A0 or 0, for example. If I had written it, A0 would be the convention, the 0 designation would be an error. A0,A1, and so on are what is clearly marked on the board. So it makes no sense to call A0, 0.

but if I choose to use 1
How does Arduino know i'm referring to Analong 1 or Digital 1?

It's coded into the AnalogRead~~/Write and digitalRead/Write functions~~.

if analogRead encounters 1 it's coded to recognize that you must mean pin A1.

to use digitalRead on pin A1 you must use A1 or 15 because digitalRead recognizes 1 to mean only pin 1.

A number is just a number, It's the context that determines it's meaning.
So

myservo.write(A1);

would set servo position at 15 degrees not try to use pin A1. (not recommended but it will work)

Looking at the analogRead() function in wiring_analog.c, we have this condition that would be processed:

if (pin >= 14) pin -= 14; // allow for channel or pin numbers

If I had written it, A0 would be the convention, the 0 designation would be an error.

I am with you on this.

Hutkikz:
It's coded into the AnalogRead/Write and digitalRead/Write functions.

if analogRead encounters 1 it's coded to recognize that you must mean pin A1.

to use digitalRead on pin A1 you must use A1 or 15 because digitalRead recognizes 1 to mean only pin 1

analogRead() is the only function that does anything special, it maps 0..5 to A0..A5 -
remember you can read the source code to all this machinery, its in every copy of the Arduino you
download there waiting to be read.

Just to avoid any confusion, analogWrite is nothing to do with analog inputs, and except for the DAC
pins on the Due, nothing to do with analog anything.

Oh I see. So it's REALLY newbie proof, SO newbie proof that causes newbies that wonder how does it work, to get confused about it, lol.

Thank you all for your answers!! They were really helpful

Just to avoid any confusion, analogWrite is nothing to do with analog inputs, and except for the DAC
pins on the Due, nothing to do with analog anything

Oops , I knew that