38

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?

bogen
  • 1,093
  • 3
  • 14
  • 27

5 Answers5

37

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.

dotancohen
  • 283
  • 2
  • 8
Manishearth
  • 2,882
  • 2
  • 22
  • 32
  • 1
    @JamesC4S: Good point, edited. Thanks :) – Manishearth Apr 10 '13 at 15:42
  • 9
    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 – baldengineer Apr 10 '13 at 14:23
  • Much better! :) – baldengineer Apr 10 '13 at 17:53
  • 1
    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? – Johncl Nov 02 '15 at 18:28
  • 1
    This is very concise and informative, thank you, however I believe that there is one small error. According to [the fine docs](https://www.arduino.cc/reference/en/language/variables/constants/constants/): `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`. – dotancohen Nov 13 '19 at 12:09
  • One exception: the Arduino Nano, Pro Mini, and Mini’s extra A6 and A7 pins can only be used as analog inputs, not digital inputs or outputs. This is a hardware limitation specific to those pins, not found on other models. – Orion Lawlor Mar 18 '22 at 22:14
36

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

Anindo Ghosh
  • 50,188
  • 8
  • 103
  • 200
7

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.

asheeshr
  • 927
  • 1
  • 10
  • 25
5

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();
}
Steve Cooley
  • 1,485
  • 1
  • 11
  • 16
  • 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? – Manishearth Apr 14 '13 at 08:32
  • @AnnonomusPerson: The [EasyDriver Stepper Motor Driver](https://www.sparkfun.com/products/10267) appears to an active product, in stock and everything. – davidcary Sep 04 '13 at 20:52
2

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.

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