24
\$\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\$
22
\$\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\$
  • \$\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\$ – Anonymous Penguin Jul 19 '13 at 14:40
25
\$\begingroup\$

https://electronics.stackexchange.com/search?q=url%3Ahttp%3A%2F%2Farduino.stackexchange.comYou 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
    \$\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\$ – baldengineer Apr 10 '13 at 14:23
  • \$\begingroup\$ @JamesC4S: Good point, edited. Thanks :) \$\endgroup\$ – Manishearth Apr 10 '13 at 15:42
  • \$\begingroup\$ Much better! :) \$\endgroup\$ – baldengineer Apr 10 '13 at 17:53
  • \$\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 Nov 2 '15 at 18:28
6
\$\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\$
4
\$\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\$
  • \$\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\$ – Manishearth Apr 14 '13 at 8:32
  • \$\begingroup\$ @AnnonomusPerson: The EasyDriver Stepper Motor Driver appears to an active product, in stock and everything. \$\endgroup\$ – davidcary Sep 4 '13 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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