Problem using Analog pins as Digital outputs

Hi All, I recently bought my first Arduino (an Uno R3) and I have just been playing around with it today when I stumbled upon a problem.

I was trying to get the Analog In pins to behave as digital outputs. I found that only the A0 pin will behave as a digital out.

I uploaded the following sketch:

void setup(){
  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(A2,OUTPUT);
  pinMode(A3,OUTPUT);
  pinMode(A4,OUTPUT);
  pinMode(A5,OUTPUT);
  digitalWrite(A0,HIGH);
  digitalWrite(A1,HIGH);
  digitalWrite(A2,HIGH);
  digitalWrite(A3,HIGH);
  digitalWrite(A4,HIGH);
  digitalWrite(A5,HIGH);
}
void loop(){
}

and measured voltages (with a multimeter) between each Analog pin and the GND. The only pin outputting +5V was the A0.

I also tried replacing A0,A1 ... A5 with 14,15 .. 19 etc, like so

pinMode(14,OUTPUT);
digitalWrite(14,HIGH);

and again, only the A0 pin seems to be working.

Any ideas? Cheers.

I just tested that on my Leonardo. It didn't work if I used numbers like 14...16 But It did work for A0, A1, and A2 . I didn't thest more than those 3. I used the basic blink program and added extra leds.

int led13 = 13;
int led = 17;
void setup() {                
  pinMode(led13, OUTPUT);     
  pinMode(led, OUTPUT);     
  pinMode(A2, OUTPUT);     
}
void loop() {
  digitalWrite(led13, HIGH); 
  digitalWrite(led, HIGH); 
  digitalWrite(A2, HIGH); 
  delay(100);              
  digitalWrite(led13, LOW);   
  digitalWrite(led, LOW);   
  digitalWrite(A2, LOW);   
  delay(100);             
}

On the Leonardo, pin 17 is an additional onboard led, and it does blink with this code, but the A3 pin does not.

jackwp: I just tested that on my Leonardo. It didn't work if I used numbers like 14...16

That's because on the Leonardo, A0 is not 14. It's something like 18. That's why you should use the names: A0, A1, etc if you want your sketches to be portable.

Good call johnwasser. I changed to 18 and 19 for the A0 and A1, and you was right (of course). So your suggestion to use A0 .. A5 has a very good merit.

So, Whiteblooded, did you get yours working with this info?

Whiteblooded: and measured voltages (with a multimeter) between each Analog pin and the GND. The only pin outputting +5V was the A0.

What were the other readings, did they get closer to 5 volts, or stay near GND ?

Works for me using UNO and Arduino1.0.4

void setup()
{
  pinMode(14,OUTPUT);
  pinMode(15,OUTPUT);
  pinMode(16,OUTPUT); 
  
}

void loop()
{
  digitalWrite(14,HIGH);
  delay(250);
  digitalWrite(14,LOW);  
  digitalWrite(15,HIGH);
  delay(250);
  digitalWrite(15,LOW);
  digitalWrite(16,HIGH);
  delay(250);
  digitalWrite(16,LOW);  
}

[quote author=Runaway Pancake link=topic=185519.msg1374227#msg1374227 date=1377959879] Works for me using UNO and Arduino1.0.4

void setup()
{
  pinMode(14,OUTPUT);
  pinMode(15,OUTPUT);
  pinMode(16,OUTPUT); 
  
}

void loop()
{
  digitalWrite(14,HIGH);
  delay(250);
  digitalWrite(14,LOW);  
  digitalWrite(15,HIGH);
  delay(250);
  digitalWrite(15,LOW);
  digitalWrite(16,HIGH);
  delay(250);
  digitalWrite(16,LOW);  
}

[/quote]

Not a question of if it will work or not, it will on a 328p based board, the problem is that it is avr chip specific and not portable across the arduino board selection options. Using the Ax pin names will allow portability where using digital pin names in the digitalWrite() function will not.

retrolefty: Not a question of if it will work or not, it will on a 328p based board, the problem is that it is avr chip specific and not portable across the arduino board selection options. Using the Ax pin names will allow portability where using digital pin names in the digitalWrite() function will not.

OP's stated concern is/was application with a Uno.

FWIW, I pinModed A0-A2 as outputs and used digitalWrites to 14-16, results OK I pinModed 14-16 as outputs and used digitalWrites to A0-A2, results OK I pinModed A0-A2 as outputs and used digitalWrites to A0-A2, results OK