Stepper motor controller circuit and code
I build a step motor controller based on the schematics found on the Arduino Web site. First I try the 2 pins one for bipolar stepper motor. I wont be able to make it work, even less with the library available on the site. Maybe I’m dumb and I really dont understand something, but this circuit keeps both coils active at the same time.
So I tried the second circuit, the 4 pins one. With the same library I wasn’t able to make it work either. So I create a piece of code of my own and I finally managed to make the motor run correctly.
After that, I realize that I can merge the to circuits to possibly create a PWM abled bipolar motor controller with the L297D. A somewhat bizzare idea, but I think it’s possible if we can program the Arduino to send 4 synced PWM signals. I’ll come back later (maybe never) with this thought. After some research I found that the TI DRV8811 and the Allegro A3977 are much more appropriate chips to do microstepping.
But for now, there is my circuit. It’s advantage is that it can release the coils. Download the schematics (Fritzing format).
The two PNP transistors are connected like the circuit proposed on the Arduino Web site. Plus, I use the enable pins. So my circuit needs four pins, and I don’t found a way to use less.
Parts list
- 1 x Arduino
- 1 x L293 Quadruple Half H-Bridge
- 2 x 1K Ohm Resistors
- 2 x 10K Ohm Resistors
- 2 x 2N2222 PnP Transistors
- 1 x Bipolar Step Motor
There is the sequence to make the motor turn one direction, invert it to make it turn the other.
| CTRL A | EN A | CTRL B | EN B |
|---|---|---|---|
| 0 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 |
| 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 0 |
There is my code to control the stepper motor. Like you see in my code I use direct port command to take less processor time. And I create a function called in the main loop for each steps. This way the code can do something else when the motor is running.
/*
Stepper Motor Controller
by Kevin Filteau 2010-05-29
Playwithmyled.com
*/
// Motor controller pins.
#define pinMotorCtrlA 4
#define pinMotorCtrlB 7
#define pinMotorEnA 5
#define pinMotorEnB 6
// Direction constants.
#define GOLEFT 0
#define GORIGHT 1
// Motor states and position.
boolean motorRunning = false;
int motorMax = 700;
int motorPos = 0;
/**********************************************
MOTOR
**********************************************/
// Setup the motor.
void motorSetup() {
pinMode(pinMotorCtrlA, OUTPUT);
pinMode(pinMotorCtrlB, OUTPUT);
pinMode(pinMotorEnA, OUTPUT);
pinMode(pinMotorEnB, OUTPUT);
// Everybody to LOW.
digitalWrite(pinMotorCtrlA, LOW);
digitalWrite(pinMotorCtrlB, LOW);
digitalWrite(pinMotorEnA, LOW);
digitalWrite(pinMotorEnB, LOW);
}
// Free the motor.
void motorFree() {
digitalWrite(pinMotorEnA, LOW);
digitalWrite(pinMotorEnB, LOW);
}
// Make the motor spin one step in the direction specified.
// Pins 7654 ----
// Binary 0000 0000
void motorSpin(boolean dir) {
static int pos = 0; // Starting step pos.
int stepsSeq[] = {0xC0,0x30,0x40,0x20}; // Registry value.
int st;
// Timing
static unsigned long previousMillis;
long interval = 3;
if(millis() - previousMillis > interval) {
previousMillis = millis();
st = stepsSeq[pos];
if( dir == GOLEFT ) {
pos++; if(pos>3) pos=0;
motorPos++;
} else {
pos--; if(pos<0) pos=3;
motorPos--;
}
PORTD &= 0xF;
PORTD |= st;
}
}
/**********************************************
MAIN FUNCTIONS.
**********************************************/
void setup() {
motorSetup();
}
void loop() {
motorSpin(GOLEFT);
// motorSpin(GORIGHT);
}
And to close this post. Some pictures of the prototype, the finalized circuit board (useful for the pinout) and a video taken while testing the circuit.






