Introduction: Stepper Motor Easy Driver
In this instructable I will provide a code sample and layout to connect a Nema 17 form factor stepper motor to an Easy Driver motor controller board then control the motor with an Arduino Uno.
Step 1: Required Components:
- Nema 17 form factor stepper motor
- Sparkfun Easy Driver Motor controller board
- Arduino Uno microcontroller.
- Power source for Stepper Motor (5 - 24 volts)
- Soldering Iron (or station) and solder
- Colored patch wire for connections
Step 2: Layout:
Notice in the picture of the stepper motor this motor has six wire locations but only 4 are used. The six wire configuration is for a "UniPolar" motor interface. This instructable is for a "BiPolar" motor configuration.
The Nema steppers can generally be used for either. There are many articles online about the difference between UniPolar and BiPolar stepper motor configurations. The location of the wires coming out of the motor also line up with the easy driver "A" and "B" wire connection points.
The wires on your motor may be different colors but the layout will probably be the same as the picture above.
Step 3: Code:
/*
Stepper Motor Control - one rev back and forth
This program is for the SparkFun Easy motor driver board.
This program drives a bipolar stepper motor. The motor is attached to ground plus digital pins 8 and 9 of the Arduino.
The motor should revolve one revolution in one direction, then one revolution in the other direction at a speed determined by the milliseconds in the microSecDelay variable.
Created by Bill Harper, Feb. 10 2014 */
#include <Stepper.h> int oneway; // counter for steps int onerev = 6400; // number of steps each direction (edit this for your particular motor)
int microSecDelay = 30; // delay between steps and speed of the motor (about as fast as the system can react, higher number = slower) int dirPin = 8; // output pin for stepper motor direction int stepPin = 9; // output pin for the pin used to step the motor
void setup() { pinMode(dirPin, OUTPUT); // Assign output mode to pin for direction pinMode(stepPin, OUTPUT); // Assign output mode to pin for setp digitalWrite(dirPin, LOW); // Initialize dir pin digitalWrite(stepPin, LOW); // Initialize step pin oneway = 1; }
void loop() { if (oneway < onerev + 1) // Still in first revolution? { digitalWrite(dirPin, LOW); // Keep direction pin low } else { digitalWrite(dirPin, HIGH); // If not in first revolution change direction pin to High }
digitalWrite(stepPin, HIGH); // Step motor delayMicroseconds(microSecDelay); // Wait microseconds digitalWrite(stepPin, LOW); // Step motor delayMicroseconds(microSecDelay); // Wait microseconds
oneway += 1; // Increment direction counter
if (oneway > onerev * 2) // If we have exceeded two revolutions { oneway = 1; } // Reset counter to start over again } // EOF
11 Comments
9 years ago on Introduction
hi Willie, maybe you could help. my stepper react but rotor is not moving...any clue?
Reply 7 years ago
Is a little bit late but I had the same problem. The wireing was correct but was not working as expected. I used the instructions here:
https://learn.sparkfun.com/tutorials/easy-driver-h...
Basically you have to hook and setup 3 more wires for EN, MS1, MS2. After that it worked perfectly.
Reply 9 years ago on Introduction
That could be happening because current is flowing in just one coil of the motor or maybe other reasons.
1) Make sure your connections are ok.
2) Maybe you can check your motor's integrity. If its a Bipolar stepper: Disconnect your stepper from the circuit and make sure the stepper wires are isolated from each other. Manually turn the rotor, it should spin smoothly. Now, pick a stepper wire and, one by one, connect it to the other wires. In each of these steps, turn the rotor again. When you feel that it became harder to turn, those two wires are ONE coil. This means the other two wires are the other coil and the motor should behave simmilarly when you join them together (and the other pair now is separated). If this doesn't happen... your stepper may be wasted.
3) Make sure your arduino ground is connected to your driver.
4) I don't know man, good luck
7 years ago
hi onerev starts at 6400 right? so i will always be > oneway even if you plus one?
and oneway will never be > onerev*2 right?
please help thanks
Reply 7 years ago
The operation to manipulate the direction bit is 1 to 6400 times 2. The first 6400 has the direction bit set LOW then the next 6400 the direction bit is set HIGH. The variable onerev is set at a constant value of 6400. The variable oneway is incremented each loop. If its value is less than 6400 then the direction bit is set LOW and if its value is higher than 6400 then the direction bit is set HIGH. Remember this code loops endlessly, so the value of oneway changes each loop. I suppose the variable oneway should be changed to a constant.
7 years ago
it can be dirve the NEMA23 Hybrid Type Stepper motor.
8 years ago on Introduction
This sketch won't even compile. Am I doing something wrong?
Reply 7 years ago
Many ( // ) were left out. Also if you are running XP it may be an issue.
Reply 8 years ago on Introduction
be sure to have stepper.h added to your libraries
8 years ago on Step 3
Arduino: 1.6.1 (Windows 8.1), Board: "Arduino Uno"
Nema_17.cpp.o: In function `setup':
C:\Program Files (x86)\Arduino/Nema_17.ino:11: undefined reference to `pinMode'
C:\Program Files (x86)\Arduino/Nema_17.ino:12: undefined reference to `pinMode'
C:\Program Files (x86)\Arduino/Nema_17.ino:13: undefined reference to `digitalWrite'
C:\Program Files (x86)\Arduino/Nema_17.ino:14: undefined reference to `digitalWrite'
Nema_17.cpp.o: In function `loop':
C:\Program Files (x86)\Arduino/Nema_17.ino:25: undefined reference to `digitalWrite'
C:\Program Files (x86)\Arduino/Nema_17.ino:29: undefined reference to `digitalWrite'
C:\Program Files (x86)\Arduino/Nema_17.ino:30: undefined reference to `delayMicroseconds'
C:\Program Files (x86)\Arduino/Nema_17.ino:31: undefined reference to `digitalWrite'
C:\Program Files (x86)\Arduino/Nema_17.ino:32: undefined reference to `delayMicroseconds'
C:\Users\EDUPO_~1\AppData\Local\Temp\build3550156373437986379.tmp/core.a(main.cpp.o): In function `main':
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/main.cpp:32: undefined reference to `init'
collect2.exe: error: ld returned 1 exit status
Error compiling.
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
HOW DO YOU FIX THIS?
8 years ago on Step 3
Arduino: 1.6.3 (Windows 8.1), Board: "Arduino Uno"
sketch_may02a.ino:4:37: error: expected unqualified-id before 'for'
sketch_may02a.ino:7:44: error: expected ')' before 'as'
sketch_may02a.ino: In function 'void setup()':
sketch_may02a.ino:13:11: error: 'dirPin' was not declared in this scope
sketch_may02a.ino: In function 'void loop()':
sketch_may02a.ino:23:20: error: 'dirPin' was not declared in this scope
sketch_may02a.ino:27:20: error: 'dirPin' was not declared in this scope
sketch_may02a.ino:28:41: error: 'direction' was not declared in this scope
sketch_may02a.ino:28:51: error: expected ';' before 'pin'
sketch_may02a.ino:32:21: error: 'microSecDelay' was not declared in this scope
Error compiling.
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Otherwise, exactly what I am trying to do :)