# Can I use the analog pins on the Arduino for my project as digital?

I am new to using Arduino, and I have an Arduino Uno. For the projects I've done, I've only used the digital pins.

I am building a small vehicle that uses stepper motors. I have run out of pins to control the motors for this vehicle. What are the analog pins for? Is it possible for me to use analog pins to control the rest of the step motors which I connect to the Arduino, or do I have to buy a bigger Arduino than Arduino Uno to control this contraption?

Yes, the analog pins on the Arduino can be used as digital outputs.

This is documented in the Arduino input pins documentation, in the Pin Mapping section:

Pin mapping
The analog pins can be used identically to the digital pins, using the aliases A0 (for analog input 0), A1, etc. For example, the code would look like this to set analog pin 0 to an output, and to set it HIGH:
pinMode(A0, OUTPUT);
digitalWrite(A0, HIGH);

• Yeah... sorry. I remember it was late when I posted that and I was getting sleepy and losing concentration. I removed the comment. Commented Jul 19, 2013 at 14:40

You can always use the analog pins for digital writing.

• digitalRead() works on all pins. It will just round the analog value received and present it to you. If analogRead(A0) is greater than or equal to 512, digitalRead(A0) will be 1, else 0.
• digitalWrite() works on all pins, with allowed parameter 0 or 1. digitalWrite(A0,0) is the same as analogWrite(A0,0), and digitalWrite(A0,1) is the same as analogWrite(A0,255)
• analogRead() works only on analog pins. It can take any value between 0 and 1023.
• analogWrite() works on all analog pins and all digital PWM pins. You can supply it any value between 0 and 255.

The analog pins let you read/write analog values - basically, instead of giving out a voltage of 0 or 5 (as with digital), they can give a range of voltages between 0 and 5 (both as input and output). Note that the voltage during analog output is only the observed voltage with a multimeter. In reality, the analog pins send pulses of 0V and 5V signals to get an output that "looks" analog (this is PWM).

Regarding the number of pins: keep in mind that the PWM pins can be used for analog output. If you run out of pins, you can use multiplexing to make more. It is not necessary to get another Arduino.

• AnalogWrite() does not output voltages between 0 to 5 (except on the Due)! AnalogWrite() only changes the pulse width modulation of the signal. The output voltage is still 5volts (or whatever VCC is). The pins marked "analog" will only output 0V or 5V. They are not variable and are note Commented Apr 10, 2013 at 14:23
• @JamesC4S: Good point, edited. Thanks :) Commented Apr 10, 2013 at 15:42
• Much better! :) Commented Apr 10, 2013 at 17:53
• Are digitalRead() and digitalWrite() on analog pins slower? If digitalRead() rounds the value I can imagine it being way slower than digitalRead() on a digital pin? Commented Nov 2, 2015 at 18:28
• This is very concise and informative, thank you, however I believe that there is one small error. According to the fine docs: the Arduino (ATmega) will report HIGH if: a voltage greater than 3.0V is present at the pin (5V boards) which contradicts the statement in this post If analogRead(A0) is greater than or equal to 512, digitalRead(A0) will be 1, else 0. Commented Nov 13, 2019 at 12:09

The analog pins on the Arduino can be used as digital pins, as pointed out by Anindo Ghosh.

However, even if you run out of pins to control your stepper motors, you dont really need to buy another board. You can simply use an intermediate component such as a register or a multiplexer to control the appropriate stepper motor.

If you can afford it, and you really want to make working with a stepper super easy check out Easy Stepper. I was very pleased.

From the example code page



Example 1: Basic Arduino setup
This is the most basic example you can have with an Arduino, an Easy Driver, and a stepper motor. Connect the motor's four wires to the Easy Driver (note the proper coil connections), connect a power supply of 12V is to the Power In pins, and connect the Arduino's GND, pin 8 and pin 9 to the Easy Driver.

Then load this sketch and run it on your Arduino or chipKIT:
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}

void loop() {
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delay(1);
}



Also from the same page, here's some example code to run two motors with two easystepper boards, with acceleration/deceleration: http://www.sc-fa.com/blog/wp-content/uploads/2013/04/20130414-081018.jpg


#include <AccelStepper.h>

// Define two steppers and the pins they will use
AccelStepper stepper1(1, 9, 8);
AccelStepper stepper2(1, 7, 6);

int pos1 = 3600;
int pos2 = 5678;

void setup()
{
stepper1.setMaxSpeed(3000);
stepper1.setAcceleration(1000);
stepper2.setMaxSpeed(2000);
stepper2.setAcceleration(800);
}

void loop()
{
if (stepper1.distanceToGo() == 0)
{
delay(500);
pos1 = -pos1;
stepper1.moveTo(pos1);
}
if (stepper2.distanceToGo() == 0)
{
delay(500);
pos2 = -pos2;
stepper2.moveTo(pos2);
}
stepper1.run();
stepper2.run();
}

• This really doesn't answer the question; it would be better as a comment. Could you flesh it out so that it answers the question? Commented Apr 14, 2013 at 8:32
• @AnnonomusPerson: The EasyDriver Stepper Motor Driver appears to an active product, in stock and everything. Commented Sep 4, 2013 at 20:52