I’m attempting to free up I/O pins on my Uno (eventually Pro Mini) by multiplexing 3 28BYJ-48 stepper motors with UNL2003 stepper motor control modules. To do this I am using a MC74HC595 shift register.
In my test sketch, I’ve manged to get a single motor to turn clockwise. However, I’m missing something (probably completely obvious) in my attempts to turn it counterclockwise. What am I not seeing?
const int latchPin = 3; //ST_CP of 74HC595 (12)
const int clockPin = 5; //SH_CP of 74HC595 (11)
const int dataPin = 4; //DS 0f 74HC595 (14)
const int stepsPerRevolution = 2048;
/*
convert binary nums to decimal
10100000, 01100000, 01010000, 10010000
160 96 80 144
*/
int forDataArray[4] = {160, 96, 80, 144}; //Clockwise
int revDataArray[4] = {144, 80, 96, 160}; //Reversing the order does NOT reverse the motor
void setup() {
Serial.begin(9600);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
//Clockwise rotation of stepper
//Works as expected
for (int e = 0; e < (stepsPerRevolution / 4); e++) { //one revolution
for (int i = 0; i < 4; i++) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, forDataArray[i]);
digitalWrite(latchPin, HIGH);
delay(5);
}
}
delay(2000); //temp delay for visual stop
//Counterclockwise rotation of stepper
//Does NOT work as expected.... Continues clockwise
for (int f = 0; f < (stepsPerRevolution / 8); f++) { // half revolution
for (int i = 0; i < 4; i++) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, revDataArray[i]);
digitalWrite(latchPin, HIGH);
delay(5);
}
}
delay(2000); //temp delay for visual stop
}
Any help is really appreciated.[Solved]
This is a snippet from a sketch I did to turn the stepper without the use of a library. The stepper module was wired to four I/O pins. And reversing was as easy as running the cases backwards. I had theorized this "method" would work for the shift register as well. Seems I was wrong.
case 0:
digitalWrite(stepPin[0], HIGH); // 1
digitalWrite(stepPin[1], LOW); // 0
digitalWrite(stepPin[2], HIGH); // 1
digitalWrite(stepPin[3], LOW); // 0
break;
case 1:
digitalWrite(stepPin[0], LOW);// 0
digitalWrite(stepPin[1], HIGH); // 1
digitalWrite(stepPin[2], HIGH); // 1
digitalWrite(stepPin[3], LOW); // 0
break;
case 2:
digitalWrite(stepPin[0], LOW); // 0
digitalWrite(stepPin[1], HIGH); // 1
digitalWrite(stepPin[2], LOW); // 0
digitalWrite(stepPin[3], HIGH); // 1
break;
case 3:
digitalWrite(stepPin[0], HIGH); // 1
digitalWrite(stepPin[1], LOW); // 0
digitalWrite(stepPin[2], LOW); // 0
digitalWrite(stepPin[3], HIGH); // 1
break;
I know y'all don't care much for snippets. But it just showing where I got my numbers from for the shift register.
Forgot that the two middle pins need swapped on these steppers. So, made a slight change.
const int latchPin = 3; //ST_CP of 74HC595 (12)
const int clockPin = 5; //SH_CP of 74HC595 (11)
const int dataPin = 4; //DS 0f 74HC595 (14)
const int stepsPerRevolution = 2048;
/*
convert binary nums to decimal
10100000, 01100000, 01010000, 10010000
160 96 80 144
*/
//this is the original lines up top
//int forDataArray[4] = {160, 96, 80, 144}; //Clockwise
//int revDataArray[4] = {144, 80, 96, 160}; //Reversing the order does NOT reverse the motor
int forDataArray[4] = {160, 80, 96, 144}; //Clockwise
int revDataArray[4] = {144, 96, 80, 160}; //Reversing the order does NOT reverse the motor
void setup() {
Serial.begin(9600);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
//Clockwise rotation of stepper
//Works as expected
for (int e = 0; e < (stepsPerRevolution / 4); e++) { //one revolution
for (int i = 0; i < 4; i++) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, forDataArray[i]);
digitalWrite(latchPin, HIGH);
delay(5);
}
}
delay(2000); //temp delay for visual stop
//Counterclockwise rotation of stepper
//Does NOT work as expected.... Continues clockwise
for (int f = 0; f < (stepsPerRevolution / 8); f++) { // half revolution
for (int i = 0; i < 4; i++) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, revDataArray[i]);
digitalWrite(latchPin, HIGH);
delay(5);
}
}
delay(2000); //temp delay for visual stop
}
The last update above made no difference. However, it led me to my error. I needed to swap the middle wires going to the ULN2003 module. All is good in my world tonight.