39
\$\begingroup\$

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?

\$\endgroup\$

5 Answers 5

36
\$\begingroup\$

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);

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

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.

\$\endgroup\$
7
  • 9
    \$\begingroup\$ 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 \$\endgroup\$ Commented Apr 10, 2013 at 14:23
  • 1
    \$\begingroup\$ @JamesC4S: Good point, edited. Thanks :) \$\endgroup\$ Commented Apr 10, 2013 at 15:42
  • \$\begingroup\$ Much better! :) \$\endgroup\$ Commented Apr 10, 2013 at 17:53
  • 2
    \$\begingroup\$ 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? \$\endgroup\$
    – Johncl
    Commented Nov 2, 2015 at 18:28
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – dotancohen
    Commented Nov 13, 2019 at 12:09
7
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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

http://www.sc-fa.com/blog/wp-content/uploads/2013/04/20130414-080645.jpg



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();
}
\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Apr 14, 2013 at 8:32
  • \$\begingroup\$ @AnnonomusPerson: The EasyDriver Stepper Motor Driver appears to an active product, in stock and everything. \$\endgroup\$
    – davidcary
    Commented Sep 4, 2013 at 20:52
2
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Huisman
    Commented Jan 17, 2020 at 18:54
  • \$\begingroup\$ @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. \$\endgroup\$
    – JYelton
    Commented Jan 17, 2020 at 19:19
  • \$\begingroup\$ @JYelton I didn't follow the link, but you're right. \$\endgroup\$
    – Huisman
    Commented Jan 17, 2020 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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