Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 5 down vote accepted

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

share|improve this answer
1  
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 '14 at 10:37
    
Good catch. I updated the answer. – Craig May 22 '14 at 18:56
    
This is the most portable solution I've seen so far, and it's the one I'm using now, thanks. – hoosierEE May 26 '14 at 16:10

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.

share|improve this answer

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);
}
share|improve this answer
    
This does not work on all Arduino boards. Not all boards use 14-19. – Craig May 27 '14 at 16:53

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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