Nothing outrageously complicated but when I used the default arduino stepper library to control my 28BYJ-48 stepper motors on my ESP8266 they only turned one direction. With modifications to the library it was possible to get one stepper motor working correctly but the ESP crashed if I tried to control 2 stepper motors, solution sketch below:
What did work:
– Sketch below. (Note: No fancy acceleration/deceleration but I didnt need it)
int pos_rot = 0; int pos_elev = 0; int rot_counter = 0; int elev_counter = 0; int next_rot = -1; int next_elev = 1; int step_delay = 10; //Delay between steps in ms const int motor_pin_1 = 16; // ESP D0 const int motor_pin_2 = 5; // ESP D1 const int motor_pin_3 = 4; // ESP D2 const int motor_pin_4 = 0; // ESP D3 const int motor_pin_5 = 2; // ESP D4 const int motor_pin_6 = 14; // ESP D5 const int motor_pin_7 = 12; // ESP D6 const int motor_pin_8 = 13; // ESP D7 void setup() { pinMode(motor_pin_1, OUTPUT); // Blue pinMode(motor_pin_2, OUTPUT); // Pink pinMode(motor_pin_3, OUTPUT); // Yellow pinMode(motor_pin_4, OUTPUT); // Orange pinMode(motor_pin_5, OUTPUT); pinMode(motor_pin_6, OUTPUT); pinMode(motor_pin_7, OUTPUT); pinMode(motor_pin_8, OUTPUT); // Begin Serial communication at a baud rate of 9600: Serial.begin(115200); delay(100);Serial.println("Leaving Setup");delay(100); } void loop() { //test_sweep(); rot(2038);delay(2000);rot(-2038); // 2038 steps clockwise followed by the same counterclockwise elev(1019);delay(2000);elev(-1019); // 1019 steps clockwise followed by the same counterclockwise } void test_sweep(){ Serial.print("Rotating CCW, pos_rot = ");Serial.println(pos_rot); while (pos_rot < 2038) { rot(1); pos_rot = pos_rot + 1; if ((pos_rot < 1019) && ((pos_rot % 2) == 0)) { elev(2); } else if ((pos_rot > 1019) && ((pos_rot % 2) == 0)) { elev(-2); } } while (pos_rot > 0) { rot(-1); pos_rot = pos_rot - 1; if ((pos_rot > 1019) && ((pos_rot % 2) == 0)) { elev(2); } else if ((pos_rot < 1019) && ((pos_rot % 2) == 0)) { elev(-2); } } } void elev(int num_steps) { if (num_steps > 0) { while (elev_counter < num_steps) { elev_step(next_elev); next_elev = next_elev +1; if (next_elev > 7) { next_elev = 0;} elev_counter = elev_counter +1; } } else { num_steps = abs(num_steps); while (elev_counter < num_steps) { elev_step(next_elev); next_elev = next_elev - 1; if (next_elev < 0) { next_elev = 7;} elev_counter = elev_counter +1; } } elev_counter = 0; } void rot(int num_steps) { if (num_steps > 0) { while (rot_counter < num_steps) { rot_step(next_rot); next_rot = next_rot +1; if (next_rot > 7) { next_rot = 0;} rot_counter = rot_counter +1; } } else { num_steps = abs(num_steps); while (rot_counter < num_steps) { rot_step(next_rot); next_rot = next_rot - 1; if (next_rot < 0) { next_rot = 7;} rot_counter = rot_counter +1; } } rot_counter = 0; } void rot_step(int go_step) { switch (go_step) { case 0: // 0001 digitalWrite(motor_pin_1, 0); // Blue digitalWrite(motor_pin_2, 0); // Pink digitalWrite(motor_pin_3, 0); // Yellow digitalWrite(motor_pin_4, 1); // Orange break; case 1: // 0011 digitalWrite(motor_pin_1, 0); digitalWrite(motor_pin_2, 0); digitalWrite(motor_pin_3, 1); digitalWrite(motor_pin_4, 1); break; case 2: //0010 digitalWrite(motor_pin_1, 0); digitalWrite(motor_pin_2, 0); digitalWrite(motor_pin_3, 1); digitalWrite(motor_pin_4, 0); break; case 3: //0110 digitalWrite(motor_pin_1, 0); digitalWrite(motor_pin_2, 1); digitalWrite(motor_pin_3, 1); digitalWrite(motor_pin_4, 0); break; case 4: // 0100 digitalWrite(motor_pin_1, 0); digitalWrite(motor_pin_2, 1); digitalWrite(motor_pin_3, 0); digitalWrite(motor_pin_4, 0); break; case 5: // 1100 digitalWrite(motor_pin_1, 1); digitalWrite(motor_pin_2, 1); digitalWrite(motor_pin_3, 0); digitalWrite(motor_pin_4, 0); break; case 6: //1000 digitalWrite(motor_pin_1, 1); digitalWrite(motor_pin_2, 0); digitalWrite(motor_pin_3, 0); digitalWrite(motor_pin_4, 0); break; case 7: //1001 digitalWrite(motor_pin_1, 1); digitalWrite(motor_pin_2, 0); digitalWrite(motor_pin_3, 0); digitalWrite(motor_pin_4, 1); break; } delay(step_delay); } void elev_step(int go_step) { //Serial.println(go_step); switch (go_step) { case 0: // 0001 digitalWrite(motor_pin_5, LOW); digitalWrite(motor_pin_6, LOW); digitalWrite(motor_pin_7, LOW); digitalWrite(motor_pin_8, HIGH); break; case 1: // 0011 digitalWrite(motor_pin_5, LOW); digitalWrite(motor_pin_6, LOW); digitalWrite(motor_pin_7, HIGH); digitalWrite(motor_pin_8, HIGH); break; case 2: //0010 digitalWrite(motor_pin_5, LOW); digitalWrite(motor_pin_6, LOW); digitalWrite(motor_pin_7, HIGH); digitalWrite(motor_pin_8, LOW); break; case 3: //0110 digitalWrite(motor_pin_5, LOW); digitalWrite(motor_pin_6, HIGH); digitalWrite(motor_pin_7, HIGH); digitalWrite(motor_pin_8, LOW); break; case 4: // 0100 digitalWrite(motor_pin_5, LOW); digitalWrite(motor_pin_6, HIGH); digitalWrite(motor_pin_7, LOW); digitalWrite(motor_pin_8, LOW); break; case 5: // 1100 digitalWrite(motor_pin_5, HIGH); digitalWrite(motor_pin_6, HIGH); digitalWrite(motor_pin_7, LOW); digitalWrite(motor_pin_8, LOW); break; case 6: //1000 digitalWrite(motor_pin_5, HIGH); digitalWrite(motor_pin_6, LOW); digitalWrite(motor_pin_7, LOW); digitalWrite(motor_pin_8, LOW); break; case 7: //1001 digitalWrite(motor_pin_5, HIGH); digitalWrite(motor_pin_6, LOW); digitalWrite(motor_pin_7, LOW); digitalWrite(motor_pin_8, HIGH); break; } delay(step_delay); }
That’s it!