Pages: [1]   Go Down
Author Topic: Problem using Analog pins as Digital outputs  (Read 829 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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
Code:
pinMode(14,OUTPUT);
digitalWrite(14,HIGH);

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

Any ideas?
Cheers.
Logged

Texas
Offline Offline
Edison Member
*
Karma: 25
Posts: 1298
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
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.
Logged

Good luck, Jack

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8822
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Texas
Offline Offline
Edison Member
*
Karma: 25
Posts: 1298
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

Good luck, Jack

The Netherlands
Online Online
Edison Member
*
Karma: 49
Posts: 1633
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote from: 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 ?
Logged

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Offline Offline
Faraday Member
**
Karma: 65
Posts: 2500
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Works for me using UNO and Arduino1.0.4

Code:
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); 
}
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Works for me using UNO and Arduino1.0.4

Code:
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); 
}

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

Offline Offline
Faraday Member
**
Karma: 65
Posts: 2500
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Pages: [1]   Go Up
Jump to: