Specifying "A" before number when referring to analog pins.

Hello, as of recently, I've discovered that you don't have to specify the "A" before the number on analog pins. Since you can use analogWrite / analogRead on PWM pins, how does Arduino know if you're talking about analog or normal pins?

analogRead knows the difference.

toxicxarrow: Since you can use analogWrite / analogRead on PWM pins

You can not analogRead from digital pins.

AWOL: analogRead knows the difference.

Whandall: You can not analogRead from digital pins.

Ohh, so I take it you can't analogWrite to digital pins, only to PWM?

So the ONLY time I can specify analog pins without the "A" in front is when using "analogRead()"?

Find the file wiring_analog.c on your PC. On a Windows system, C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\wiring_analog.c

Study the functions analogRead() and analogWrite().

You can print an analog pin number

Serial.println(A0)

You can use the number is printed while analysing the analogRead() code. For a 328P based system, this is the relevant line in analogRead()

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

You can analogWrite to any pin; if the pin is not a PWM pin, it will result in a digitalWrite The relevant part in analogWrite() in analog_wiring.c

case NOT_ON_TIMER:
default:
    if (val < 128) {
        digitalWrite(pin, LOW);
    } else {
        digitalWrite(pin, HIGH);
    }

You can't "AnalogWrite" to an Analog pin, they are input only...

Conversely, you cannot "AnalogRead" a digital pin...

So the compiler just knows that if you are Reading, it has to be an analog pin, and if you are Writing, it has to be a digital pin, and if it's a PWM pin, it sets the voltage...

daba: You can't "AnalogWrite" to an Analog pin, they are input only...

Nope. Analog is an additional feature of a normal digital pin (besides A6 and A7 on an 328p).

daba: and if it's a PWM pin, it sets the voltage...

analogWrite() does not set voltages, it sets a duty cycle.

You can use analogWrite() on any digital pin... but if the pin is not a PWM pin you only get two levels of PWM: OFF and ON.

You can use analogRead() only on analog input pins. If you pass it 0 it knows you mean A0 because Pin 0 is not an analog input pin.

If you use digitalRead() or digitalWrite() it makes a big difference if you use 0 or A0. They are two different pins.

On some boards there are analog input pins that are NOT also digital pins. For example on the Arduino Nano you can use analogRead() on A6 and A7 but if you try to use pinMode(), digitalRead() or digitalWrite() on A6 or A7 you will not be able to get or set the state of the pin.

Analog input pins are given digital pin numbers... but those numbers are DIFFERENT on different boards. That is the main reason I recommend ALWAYS using the names A0, A1, A2... when you are referencing an analog input pin, even if you are using it as a digital pin (pinMode(), digitalRead(), digitalWrite()).

sterretje:
You can analogWrite to any pin; if the pin is not a PWM pin, it will result in a digitalWrite The relevant part in analogWrite() in analog_wiring.c

case NOT_ON_TIMER:

default:
   if (val < 128) {
       digitalWrite(pin, LOW);
   } else {
       digitalWrite(pin, HIGH);
   }

So if do, say, “analogWrite(3, 200);”, does it use PWM pin 3, or A3 pin? Or can A3 analogRead, but only digitalWrite? Is that how it works?

johnwasser: You can use analogWrite() on any digital pin... but if the pin is not a PWM pin you only get two levels of PWM: OFF and ON.

You can use analogRead() only on analog input pins. If you pass it 0 it knows you mean A0 because Pin 0 is not an analog input pin.

If you use digitalRead() or digitalWrite() it makes a big difference if you use 0 or A0. They are two different pins.

On some boards there are analog input pins that are NOT also digital pins. For example on the Arduino Nano you can use analogRead() on A6 and A7 but if you try to use pinMode(), digitalRead() or digitalWrite() on A6 or A7 you will not be able to get or set the state of the pin.

Analog input pins are given digital pin numbers... but those numbers are DIFFERENT on different boards. That is the main reason I recommend ALWAYS using the names A0, A1, A2... when you are referencing an analog input pin, even if you are using it as a digital pin (pinMode(), digitalRead(), digitalWrite()).

Very informative; thank you.

toxicxarrow: Hello, as of recently, I've discovered that you don't have to specify the "A" before the number on analog pins.

I think that the question is why would you want to ?

Using the A prefix makes it clear which pin is being read and that it is an analogue pin

UKHeliBob: I think that the question is why would you want to ?

Using the A prefix makes it clear which pin is being read and that it is an analogue pin

I wouldn't want to; I need all the help I can get with clear markings.

Just wondering how the chip knows which pin the user is talking about when initializing that way.

Because you're calling analogRead

Whandall: You can not analogRead from digital pins.

The following codes which contain unsigned int x = analogRead(7); instruction are compiled/uploaded without any error? I think that the programmer must know that the analog channels (as alternate functioms) exit with PC0-PC5 lines of Port-C Register. The poor Compiler can not take of all kinds of chaos that the user might possibly create?

void setup() 
{
  pinMode(7, INPUT);
  unsigned int x = analogRead(7);
}

void loop()
{

}[/code}

The following codes which contain unsigned int x = analogRead(7); i

Some 328s have A6 and A7. They are inputs to the analogue mux, and are analogue-only inputs.

void setup()
{
  pinMode(7, INPUT);   // pin 7 already is an input
  unsigned int x = analogRead(7);  // this is NOT the same pin 7
}

AWOL: Some 328s have A6 and A7. They are inputs to the analogue mux, and are analogue-only inputs.

Then those A6 and A7 should be mapped to 20 and 21? (PD0-PD7; PB0-PB5; PC0-PC5; PC6-PC7 --> 0 - 7; 8 - 13; 14 - 19; 20 - 21)

GolamMostafa: Then those A6 and A7 should be mapped to 20 and 21? (PD0-PD7; PB0-PB5; PC0-PC5; PC6-PC7 --> 0 - 7; 8 - 13; 14 - 19; 20 - 21)

Why don't you find out, and print the value of A7?

@AWOL

Let me revise the codes of my Post#15 as follows, which are compiled/uploaded without error?

void setup() 
{
  pinMode(9, INPUT);
  unsigned int x = analogRead(9);
}

void loop()
{

}