28BYJ-48 Stepper Motor using NodeMCU ESP8266,

When I try to put in 2048 the motor sound is higher pitched, shorter in duration, and no movement. Anything above 500 same.

I have tried a FOR loop to do the 500 steps 5 times (for kicks) and it does move farther, yet the kill power then remains on.

Yes, that library has some code I have come this far with among other sources.

Wooo Hoo! I think I got it. I put in a For loop and had it loop AFTER the Digital Write Low sequence. With 20 loops, the motor rotates 360 degrees 4 1/4 times. Which should be good enough...

Thank you all for putting up with me and sharing your knowledge and time.

Cheers!!
MM

1 Like
  • Suggest you use a loop that runs every 2.5ms for each of the 2048 steps, about 5 seconds per revolution.
    This way you give the motor enough time to move. i.e. do not run stepping at full speed.
    Also, you can make your sketch non-blocking doing this.



  • Please post your sketch for new people that might have the same problem.
/*  ___   ___  ___  _   _  ___   ___   ____ ___  ____  
 * / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \|    \ 
 *| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
 * \___/(___/ \___/ \__  |\___/ \___(_)____)___/|_|_|_|
 *                  (____/ 
 * This sketch is about how to use the MQTT protocol to control a step motor
 * Tutorial URL  http://osoyoo.com/2017/05/17/nodemcu-lesson-16-step-motor-mqtt/
 * CopyRight www.osoyoo.com
 *
 * modified by inthenash 2024/08/19
 * Special thanks to LarryD, davidefa, MicroBahner, DaveX from Arduino.CC Forum
 * https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/
 * https://www.youtube.com/watch?v=9KFbgEBwMSc
 *
 * Equipment:  NodeMCU; 28Byj-48 stepper motor 5v; ULN2003 driver
 * Power: external 6.1 volts; NodeMCU will be ok at 6v (however do your own research to confirm) 
*/

#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 = "iot.eclipse.org";

const int stepsPerRevolution = 500;  // change this to fit the number of steps per revolution. This is NOT revolution of the motor spindle, but coil revolutions.

// initialize the stepper library on D1,D2,D5,D6 - note these are the pins on the NODEMCU.
//GPIO numbers are 5, 4, 14, 12

Stepper myStepper(stepsPerRevolution, D1, D5, D2, D6);  // this specific order is very important to hit the correct coils in the correct order. I used these pins because they are all set low at boot.

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 < 20; x++) {
    myStepper.step(stepsPerRevolution);
      
    
    // myStepper.off() - kill all power to the stepper motor;
    digitalWrite(5, LOW);
    digitalWrite(4, LOW);
    digitalWrite(14, LOW);
    digitalWrite(12, LOW);
    Serial.print("  clockwise" );
    }

   }
  // step one revolution in the other direction:
  else if(p==2)
  {
    for (int y = 0; y < 20; y++) {
      myStepper.step(-stepsPerRevolution);
      
            
    // myStepper.off() - kill all power to the stepper motor;
    digitalWrite(5, LOW);
    digitalWrite(4, LOW);
    digitalWrite(14, LOW);
    digitalWrite(12, 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. Make sure to surround in ""
    //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("blinds");
    } 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("192.168.1.70", 1883);  //note if using home assistant, this is the homeassistant.local IP and the mqtt port assigned in your mqtt configureation.

  client.setCallback(callback);
  // set the speed at 80 rpm:
  myStepper.setSpeed(80);
}

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

}

  • Do you mean . . .

Stepper myStepper(stepsPerRevolution, 5, 14, 4, 12);

:thinking:

  • BTW, you can try to set the speed to 5 then set steps per revolution to 2048