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?

3
  • BrettAM's Swiss watch seems very reasonable to me because it only fails in the event of a nonconsecutive mapping which, to the best of my knowledge, remains a purely hypothetical concern. What BrettAM's answer does do best is make clear the salient point that A0 is just a predefined constant of the same type (8 bit unsigned integer) as any literal you might reasonably use in that for loop. A0 is just a number. What number is it exactly? Well, that depends, and furthermore, doesn't matter much. But what you can count on is that it is a number and that you're free to use it as such.
    – user26332
    Sep 4, 2016 at 0:56
  • Sorry to resurrect this thread but I wanted to note the pros and cons of the answers provided above.. Craig's nuclear bunker is the most bulletproof; it would work even if there were nonconsecutive mappings. Geometrikal's paper airplane is the least bulletproof as his/her magic numbers will fail as soon as you move to a different board (because there's nothing hypothetical about the mappings being different between boards). Now in fairness, they make it clear that they're offering "another way of writing BrettAM's answer" and thus perhaps they're not making a serious recommendation at all.
    – user26332
    Sep 4, 2016 at 0:56
  • I like your poetic descriptions of the different answers! The metaphors are apt - but you may want to put them in quotes for our non-native English readers, to highlight you are being a little hyperbolic: "nuclear bunker", "paper airplane", etc. Sep 4, 2016 at 7:16

5 Answers 5

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,...

3
  • 4
    According to arduino.cc/en/Reference/analogRead you can read from analog pins merely by number (e.g. 0, 1, 2, 3, etc.); the "A" in "A0" is optional. Of course, it can be more clear if you distinguish them with the "A" prefix but it is not required for analogRead. On the other hand, digitalWrite(i) assumes that i refers to a digital pin; to use digitalWrite with an analog pin one needs to include the "A" prefix.
    – heypete
    May 22, 2014 at 10:37
  • Good catch. I updated the answer.
    – Craig
    May 22, 2014 at 18:56
  • This is the most portable solution I've seen so far, and it's the one I'm using now, thanks. May 26, 2014 at 16:10
4

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.

1

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);
}
1
  • This does not work on all Arduino boards. Not all boards use 14-19.
    – Craig
    May 27, 2014 at 16:53
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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.