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.

  • 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

    @JamesC4S: Good point, edited. Thanks :)

    Much better! :)

    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?

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

  • 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);
      digitalWrite(9, LOW); 

    Also from the same page, here's some example code to run two motors with two easystepper boards, with acceleration/deceleration:

    #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()
    void loop()
      if (stepper1.distanceToGo() == 0)
        pos1 = -pos1;
      if (stepper2.distanceToGo() == 0)
        pos2 = -pos2;

    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?

    @AnnonomusPerson: The EasyDriver Stepper Motor Driver appears to an active product, in stock and everything.

  • Adding as answer, because can't comment.

    To answer briefly, yes you can. Please refer to Anindo Ghosh's Answer on how to do so.

    Please Note however that for the Arduino Nano, pins A6 and A7 are analog only and cannot be used in the digital mode.

    Please note this answer is almost only contains a link. Links/websites may go obsolete, making this answer useless. Do keep the link, but please summarize or quote the relevant parts of the website.

    @Huisman To be fair, links to other questions on EE.SE are less likely to deprecate or become inaccessible; but a brief summary would certainly improve the answer.

    @JYelton I didn't follow the link, but you're right.

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM