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();
}