28BYJ-48 Stepper Motor using NodeMCU ESP8266,

Hello and thank in advance for any assistance provided.

I am using the following code. The 28BYJ-48 Stepper Motor will turn about 3/4 of a turn then stop. Two of the lights on the ULN2003 driver board stay on after the movement
and it appears random which two lights stay lit. I think there still are coils remaining active. Then motor becomes hot to the touch. I write Low to the digital pins, but it does not work.

Strange enough, when I try to do only one 500 step rev (about 1/4) turn the driver will go dark every time.

I am using a 12v 2.1 amp external power supply, with a buck converter to lower to 5v for the NodeMCU and 12V directly to motor control board.

If someone could point out what I am doing incorrectly I would appreciate it.

My code:

 */

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Stepper.h>
// Update these with values suitable for your network.
const char* ssid = "";//put your wifi ssid here
const char* password = "";//put your wifi password here
const char* mqtt_server = "";
//const char* mqtt_server = "";

const int stepsPerRevolution = 500;  // change this to fit the number of steps per revolution
// initialize the stepper library on D1,D2,D5,D6
Stepper myStepper(stepsPerRevolution, D1, D3, D2, D4);
WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {
   delay(100);
  // We start by connecting to a WiFi network
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) 
    {
      delay(500);
      Serial.print(".");
    }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) 
{
  Serial.print("Command from MQTT broker is : [");
  Serial.print(topic);
  int p =(char)payload[0]-'0';
  // step one revolution  in one direction:
  if(p==1) 
  {
{
    for (int x = 0; x < 10; x++) {
    myStepper.step(stepsPerRevolution);
    }
        // myStepper.off();
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    Serial.print("  clockwise" );

   }
  // step one revolution in the other direction:
  else if(p==2)
  {
    for (int y = 0; y < 10; y++) {
      myStepper.step(stepsPerRevolution);
    }
            
    // myStepper.off();
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    Serial.print("  counterclockwise" );
   }
   Serial.println();}
 
//  Serial.println();
 //end callback

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) 
  {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    //if you MQTT broker has clientID,username and password
    //please change following line to    if (client.connect(clientId,userName,passWord))
    if (client.connect(clientId.c_str()))
    //if (client.connect(clientId,userName,passWord))
    {
      Serial.println("connected");
     //once connected to MQTT broker, subscribe command if any
      client.subscribe("OsoyooCommand");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 6 seconds before retrying
      delay(6000);
    }
  }
} //end reconnect()

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  // set the speed at 80 rpm:
  myStepper.setSpeed(80);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

}

Isn't the 28BYJ-48 a 5V motor?

https://www.mouser.com/datasheet/2/758/stepd-01-data-sheet-1143075.pdf

Yes...that is the spec. I may have to change driver boards and modify the motor to a bi polar to become successfull. I was just wondering why the ULN2003 can handle 12v if this is the primary driver board.

Ultimately i am planning to turn a window blind shaft to open and close blinds so running this motor at 12v for just a few seconds should be ok. In theory.

If it helps, there is a 12V variant of the 28BYJ-48.

If that is the only way to solve the issue i will consider but i have several of these 28by-48 already on hand

No, 12V is far too high for a 5V motor. If there is too less torque you can try with 6 or 7V because there is a voltage loss at then ULN outputs.

1 Like

As said the uln is rated for 12V but the stepper motor you are using is not.
The uln is simply a transistor array and does not limit the current to the step motor.
Simply use a 5V to power the motors/driver.
Or if you want to use this combination add a limiting resistor ( one for each of the two windings I see there is a common central connection for both windings, so you have to use 4 resistors, one for each connection to the uln ) )
R = 7/0.1 = 70ohm ( W = 7 x 0.1 = 0.7W at least 1W of power )
The motor that heated my have been permanently damaged

Ok...I have dropped the voltage to 5V. Thanks.

However, my original question still is open. Why does the ULN not cut all power when I write digital low to all the pins?

  • Please show us good images of your actual wiring.

  • Confirm the inputs to the motor driver is 3v and 0v.

  • If you don’t want the coils ON when the motor is stopped, send LOWs to the output pins.
    I usually save the coil values then sen the LOWs, return these values prior to moving the motor.

Seems you have a fryed output, uln is simply an array of transistor, with input low or disconnected, the output should be off, no current flowing ( check if you have at least 4 transistor ok )

Not sure how to do either of the above.

After further reading, I changed from D1 D2 D3 D4 to D1 D2 D5 D6

However, I am writing the digital pins D1 D2 D5 D6 LOW AFTER the movement of the motor to try and kill power. If I try to move the motor more than once with
myStepper.step(stepsPerRevolution);
one of the ULN2003 lights remain on, thus telling this relative newbie, the one coil is still active. I usually pull power entirely in about 1 - 2 mins after testing several times.

6.1V is a low as my external Power supply will provide. For reference ">" means connects to.

NODEMCU
D1 gray > IN1, D2 purple > IN2, D5 blue > IN3, D6 green > IN4
VIN red + GnD black = 5.12V

ULN2003
IN1 > D1, IN2 > D2, IN3 > D5, IN4 > D6
V+ in red + GnD black = 6.1V

MP1584EN Buck Converter
6.1 v in stepdown to 5V for NODEMCU
6.1 v in to 28BYJ-48

Please go easy on my soldering skills... :frowning:



  • Let’s assume the ESP outputs D1, D2, D5, and D6 go to the motor driver inputs (change as needed).

     //Disable the stepper after the motor reaches its destination 
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);

  • In your use case it’s probably not necessary to return the previous motor values prior to move the motor again.



  • I don’t usually use a library to step the motor, however, the above will still work in your case.

  • Example I use this as motor stepping, but again I don’t use a library.


//                                         "Setpping Motor"                         2 8 B Y J - 4 8  
//a 5-wire unipolar stepper motor that runs on 5V
//
//Stepping Motor stepping pattern                       
//index                                0       1       2       3       4       5       6       7       8
int lookup[]                     = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001, B00000};
//index 8 in the array is B00000
//we use this step pattern to turn off the motor current when the motor is stopped




  • At its basic, is this your circuit ?

  • When you turn off the ESP outputs, confirm these outputs go to 0v with a DMM.
  • A bit confused here, are you using
    D1, D2, D5, and D6
    Or are you using the GPIO #s ?
    5, 4, 14,12

2024-08-19_18-39-56

Yes. I have since eliminated the MP158EN because I read the NODEMCU can work with 6V max through Vin

I am using
D1 GPIO5 > IN1
D2 GPIO4 > IN2
D5 GPIO14 > IN3
D6 GPIO12 > IN4

  • Which code are you using ?
     //Disable the stepper
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);

  • Or ?
     //Disable the stepper 
    digitalWrite(5, LOW);
    digitalWrite(4, LOW);
    digitalWrite(14, LOW);
    digitalWrite(12, LOW);

That is the code I am using.

  • That's very weird, I have to use this for D1,2,5,6 (on an ESP32)
     //Disable the stepper 
    digitalWrite(5, LOW);
    digitalWrite(4, LOW);
    digitalWrite(14, LOW);
    digitalWrite(12, LOW);

Bingo! Thanks. Kudos!

Now...can you help me make the motor turn more than 1/4 turn at a time. I need it to turn 360 degrees, about 3 - 5 complete times.

  • You have the above.

  • All the 5 Volt 28BYJ-48 motors I have are 2048 full steps which is 4096 1/2 steps. :thinking:




  • Have you tried the examples in your Stepper Library ?