Stepper Speed Control

guclusat

Tanınmış Üye
Süper Moderatör
Hardware Required
  • Arduino or Genuino Board
  • 10k ohm potentiometer
  • stepper motor
  • U2004 Darlington Array (if using a unipolar stepper)
  • SN754410ne H-Bridge (if using a bipolar stepper)
  • power supply appropriate for your particular stepper
  • hook-up wires
  • breadboard
Circuits
Below you'll find circuits for both unipolar and bipolar steppers. In either case, it is best to power your stepper motors from an external supply, as they draw too much to be powered directly from your Arduino board.

In both circuits, connect a 10k pot to power and ground, with it's wiper outputting to analog pin 0.

Note: Both circuits below are four wire configurations. Two wire configurations will not work with the code provided.

Unipolar Stepper Circuit and schematic
Show images for the unipolar circuit and schematic

Bipolar Stepper Circuit and schematic
Show images for the bipolar circuit and schematic

bipolarKnob_bblg.png

bipolarKnob_schlg.png

Code

Kod:
/*
Stepper Motor Control - speed control

This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
A potentiometer is connected to analog input 0.

The motor will rotate in a clockwise direction. The higher the potentiometer value,
the faster the motor speed. Because setSpeed() sets the delay between steps,
you may notice the motor is less responsive to changes in the sensor value at
low speeds.

Created 30 Nov. 2009
Modified 28 Oct 2010
by Tom Igoe

*/

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;  // number of steps the motor has taken

void setup() {
  // nothing to do inside the setup
}

void loop() {
  // read the sensor value:
  int sensorReading = analogRead(A0);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  }
}
 
Unipolar Stepper Motor
This page shows two examples on how to drive a unipolar stepper motor. These motors can be found in old floppy drives and are easy to control. The one we use has 6 connectors of which one is power (VCC) and the other four are used to drive the motor sending synchronous signals.

The first example is the basic code to make the motor spin in one direction. It is aiming those that have no knowledge in how to control stepper motors. The second example is coded in a more complex way, but allows to make the motor spin at different speeds, in both directions, and controlling both from a potentiometer.

The prototyping board has been populated with a 10K potentiomenter that we connect to an analog input, and aULN2003A driver. This chip has a bunch of transistors embedded in a single housing. It allows the connection of devices and components that need much higher current than the ones that the ATMEGA8 from our Arduino board can offer.


Picture of a protoboard supporting the ULN2003A and a potentiometer

Example 1: Simple example

Kod:
/* Stepper Copal
* -------------
*
* Program to drive a stepper motor coming from a 5'25 disk drive
* according to the documentation I found, this stepper: "[...] motor
* made by Copal Electronics, with 1.8 degrees per step and 96 ohms
* per winding, with center taps brought out to separate leads [...]"
* [http://www.cs.uiowa.edu/~jones/step/example.html]
*
* It is a unipolar stepper motor with 5 wires:
*
* - red: power connector, I have it at 5V and works fine
* - orange and black: coil 1
* - brown and yellow: coil 2
*
* (cleft) 2005 DojoDave for K3
* http://www.0j0.org | http://arduino.berlios.de
*
* @author: David Cuartielles
* @date: 20 Oct. 2005
*/

int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int delayTime = 500;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
}

void loop() {
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(delayTime);
}

Example 2: Stepper Unipolar Advanced

Kod:
/* Stepper Unipolar Advanced
* -------------------------
*
* Program to drive a stepper motor coming from a 5'25 disk drive
* according to the documentation I found, this stepper: "[...] motor
* made by Copal Electronics, with 1.8 degrees per step and 96 ohms
* per winding, with center taps brought out to separate leads [...]"
* [http://www.cs.uiowa.edu/~jones/step/example.html]
*
* It is a unipolar stepper motor with 5 wires:
*
* - red: power connector, I have it at 5V and works fine
* - orange and black: coil 1
* - brown and yellow: coil 2
*
* (cleft) 2005 DojoDave for K3
* http://www.0j0.org | http://arduino.berlios.de
*
* @author: David Cuartielles
* @date: 20 Oct. 2005
*/

int motorPins[] = {8, 9, 10, 11};
int count = 0;
int count2 = 0;
int delayTime = 500;
int val = 0;

void setup() {
  for (count = 0; count < 4; count++) {
    pinMode(motorPins[count], OUTPUT);
  }
}

void moveForward() {
  if ((count2 == 0) || (count2 == 1)) {
    count2 = 16;
  }
  count2>>=1;
  for (count = 3; count >= 0; count--) {
    digitalWrite(motorPins[count], count2>>count&0x01);
  }
  delay(delayTime);
}

void moveBackward() {
  if ((count2 == 0) || (count2 == 1)) {
    count2 = 16;
  }
  count2>>=1;
  for (count = 3; count >= 0; count--) {
    digitalWrite(motorPins[3 - count], count2>>count&0x01);
  }
  delay(delayTime);
}

void loop() {
  val = analogRead(0);
  if (val > 540) {
    // move faster the higher the value from the potentiometer
    delayTime = 2048 - 1024 * val / 512 + 1;
    moveForward();
  } else if (val < 480) {
    // move faster the lower the value from the potentiometer
    delayTime = 1024 * val / 512 + 1;
    moveBackward();
  } else {
    delayTime = 1024;
  }
}
 
Arduino Code for Controlling a Stepper Motor
The Arduino programming environment comes with a function library for controlling a stepper motor.

To use the library, in the Arduino Editor from the top menu bar: Sketch > Import Library > Stepper.

Copy the example code below into an Arduino program.

Arduino code example

Example Code Notes
  1. The example code assumes that the stepper is being controlled by Arduino pins 4, 5, 6 and 7, that control motor coil 1, 2, 3 and 4 (in that order) but you can use any set of four pins.
  2. The "#define STEPS 96" line defines the number of steps per rev. A 3.75 deg motor has 96 steps/rev while a 7.2 deg motor has 48 steps/rev.
  3. The "Stepper stepper(STEPS, 5, 7, 6, 4)" line is where you enter the four pins used to control the stepper. The order the pins are entered (coil 2, coil 4, coil 3, coil 1) is non-obvious, but can be understood if you want to dive deep into how the Arduino stepper library works.
  4. The "stepper.setSpeed(x)" command sets the motor speed to x rpm.
  5. The "stepper.step(x)" command turns the motor x steps at the speed last set in the stepper.setSpeed() command. The motor turns one direction for postive x and the reverse direction for negative x.
  6. When the example program finishes, two of the coils will remain on, so all coils are turned off to prevent your battery from draining.
  7. If you motor shudders but does not move, it is likely an error in the ordering of the coils.


Arduino Stepper Resources
Arduino tutorial for programming stepper motors:
http://www.arduino.cc/en/Tutorial/StepperUnipolar

Arduino stepper motor software library:
http://arduino.cc/en/Reference/Stepper

Example showing how to control a stepper with a potentiometer:
http://arduino.cc/en/Tutorial/MotorKnob
 
Geri
Yukarı