8

I want to do something along the lines of

for (int i = 0; i < 4; i++) {
  analogRead(i);
}

Which appears to work, but the following does not:

for (int i = 0; i < 4; i++) {
  pinMode(i, INPUT);
  pinMode(i + 4, OUTPUT); // should make Analog Pin (i + 4) into an output
  digitalWrite(i + 4, LOW);
  analogRead(i);
}

Instead, it appears to treat the pin addressed by digitalWrite(i + 4, LOW); as one of the digital pins.

Do I really have to explicitly specify A0, A1, A2, ... anytime I want to loop over the analog pins?

Alex Shroyer
  • 437
  • 2
  • 5
  • 9

6 Answers6

11

Yes, the analog pins must be addressed using A0, A1,... when using them for digital I/O.

Depending on the board you are using A0,A1,etc. are mapped to different values (for instance it looks like A0 is 18 on some boards but 14 on others.

One solution for looping over the analog pins would be this:

static const uint8_t analog_pins[] = {A0,A1,A2,A3,A4};
// Setup pins for input
for (int i = 0; i < 5; i++) { //or i <= 4
  digitalRead(analog_pins[i]);
}

If you are using the analog pins only with the analogRead() call you can use 0,1,... instead of A0,A1,...

Craig
  • 2,120
  • 10
  • 11
5

At least an Uno/Megas/leonardos, all the values mapped to analog pin numbers are consecutive, so

for (int i = A0; i < A4; i++) {
  pinMode(i, OUTPUT); 
  digitalWrite(i, LOW);
}

will set A0, A1, A2, and A3 to OUTPUT, and then LOW.

BrettFolkins
  • 4,441
  • 1
  • 15
  • 26
0

Your first loop will work just fine indeed, however, you might want to add delay(1); after you analogRead(i);, to give the ADC some time to settle.

Could you elaborate on what you are trying to do with your second piece of code? As it looks right now, it doesn't really make sense to use analog inputs as digital outputs.

Besides, you're trying to read a pin's input just a couple of lines after you specified the pin to be an output.

Please explain what you are trying achieve, so the nice folks around here can you help you better.

Tom
  • 464
  • 2
  • 7
0

Pins 14 through 19 are the analog pins A0 to A5. A0 is just an alias for 14 and so on.

So another way of writing BrettM's answer:

for (int i = 14; i < 18 i++) {
  pinMode(i, OUTPUT); 
  digitalWrite(i, LOW);
}
geometrikal
  • 2,925
  • 16
  • 21
0

I know this is old, but if you hover your mouse over A0 to A7 in visual micro it will show you the true value, they are just a variable (it actually shows as 14U but while addressing you do not need to include the U). What Craig said is wrong, they do NOT have to be addressed by A0, A1 ect..

A0 = 14 A1 = 15 . . A7 = 21

//Will set all pins, digital and analog to LOW (0)
for (int i = 1; i < 22; i++) {
    digitalWrite(i, LOW);
}

//Will set all analog pins to LOW (0)
for (int i = 14; i < 22; i++) {
    digitalWrite(i, LOW);
}

//Will set all analog pins to LOW (0)
for (int i = A0; i < A7 + 1; i++) {
    digitalWrite(i, LOW);
}

The last for loop is basically saying i = 1 (A0), and stop on 21 (A7).

0

Assuming pin numbers are consecutives you can iterate them:

int acc = 0;
for (int Ai = A0; Ai < A0 + NUM_ANALOG_INPUTS; Ai++) {
    acc += analogRead(Ai);
}
Serial.println(acc);