using a variable to store an analog pin number

I want to use the word soilMoistureSensorPin, i.e. pinMode(soilMoistureSensorPin,INPUT); to set A0 to an input.

I hope that question makes sense. With digital pins it is common to use int soilPin=0; is there an analogue for analog pins?

int soilMoistureSensorPin = A0;

will work fine. A0 is just a variable that contains the actual pin number. For example: https://github.com/arduino/ArduinoCore-avr/blob/master/variants/standard/pins_arduino.h#L56-L72

#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;

Analog pins don't need pinMode. Just use, for example, analogRead(A0).

Pete

I can just use A0 but it would be nice to be able to change the software side, if i change the hardware side, in one spot at the top.

so just to make sure i understand this fully, "A0" is just a variable that the ide already recognises as analog pin 0, and i can store it as an int even though it contains a letter?

A0 is the value of the variable soilMoistureSensorPin. AFAIK the letter A is ignored. const byte soilMoistureSensorPin = A0; // also works Leo..

roosterqmoney: so just to make sure i understand this fully, "A0" is just a variable that the ide already recognises as analog pin 0, and i can store it as an int even though it contains a letter?

Yes, that's exactly what I showed you in my last reply. Give it a try.

Wawa: AFAIK the letter A is ignored.

No, it's not ignored. It's just a variable name. On the Uno the value of the A0 variable is 14. You can use the analog channel number as the argument to analogRead(). So these are equivalent: analogRead(A0); analogRead(0); analogRead(14);

But you can also use A0 for digital IO. These are not equivalent: digitalRead(A0); digitalRead(0);

The first reads pin A0. The second reads pin 0. I think code is the most clear and consistent when the A*n* pin names are used rather than the channel number.

A0 is NOT a variable name. It is a #define'd name that has an associated value. The value is replaced everywhere in the code where the name A0 is used.

PaulS: A0 is NOT a variable name. It is a #define'd name that has an associated value. The value is replaced everywhere in the code where the name A0 is used.

See reply #1 which quotes the actual Arduino (hardware/avr/variants/standard) core code:

#define PIN_A0   (14)
static const uint8_t A0 = PIN_A0;
1 Like

something super obvious that I didn't realise when I wrote this question, there are two different read functions, analog and digital, so of course the "A" before the "0" is entirely unnecessary...

It is entirely necessary to avoid confusion.

If you were writing evacuation instructions for your building, would you say "Go out through 1" or would you say go out through door 1"?

The simple rule for Arduino pins is to always use the pin name that is written on the board. You could use PORTB1 for a pin name but without consulting the datasheet and schematic, you don't know where that pin is.

roosterqmoney: something super obvious that I didn't realise when I wrote this question, there are two different read functions, analog and digital, so of course the "A" before the "0" is entirely unnecessary...

Except '0' is zero and 'A0' is 14 on an UNO and different numbers on other processors. The name 'A0' works for both digital I/O and analogRead(). The number '0' means different things to analogRead() and digitalRead(). It also makes the meaning clear when you are assigning names to pins.

// Using just numbers:
const byte Input1Pin = 2;
const byte Input2Pin = 3;
const byte Input3Pin = 4;
const byte Input4Pin = 2;
const byte Input5Pin = 3;
// Using names:
const byte Input1Pin = 2;
const byte Input2Pin = 3;
const byte Input3Pin = 4;
const byte Input4Pin = A2;
const byte Input5Pin = A3;

For analog inputs I recommend ALWAYS using the name.