BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Controlling Stepper motor with wemos D1 mini using l298n motor controller


#1

I want to control the direction of stepper motor unipolar 6 wires via buttons in blynk interface … If button v1 clicks its starts rotating clockwise and if button v2 is clicked it starts rotating anticlockwise …
What I have used for this is

Wemos D1 mini
L298n h bridge
In1 to D5
In2 to D6
In3 to D7
In4 to D8
External power supply !

Wire Connections ok !

Program I have used…

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "cfd39c62ed714c73887231859768195d";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "NFY";
char pass[] = "qwertyuiop";

   

    #include <Stepper.h>
    const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
    Stepper myStepper( stepsPerRevolution, D5, D6, D7, D8 );

    int previous = 0;
     
          void setup()
    {
     
             // set the speed at 60 rpm:
      myStepper.setSpeed(60);
     
      Serial.begin(9600);
      
     
      while (Blynk.connect() == false) { 
      // Wait until connected
     }
     
    }

    BLYNK_WRITE(V1)
    {
       int b1 = param.asInt();
       
      // Execute clockwise routine if V1 is pressed in dashboard
      if (b1 == 0)
      {
        
        clockwise();
      }
    }

    BLYNK_WRITE(V2)
    {
      int b2 = param.asInt();
      
      // Execute counterclockwise routine if V2 is pressed in dashboard
      if (b2 == 0)
      {
        counterclockwise();
      }
    }
void loop()
    {
      Blynk.run();  
    }

    void clockwise()
    {
      // step one revolution  in one direction:
      Serial.println("clockwise");
      myStepper.step(stepsPerRevolution);
      delay(500);
    }

    void counterclockwise()
    {
      // step one revolution in the other direction:
      Serial.println("counterclockwise");
      myStepper.step(-stepsPerRevolution);
      delay(500);
    }

Results …
No rotation at all

https://drive.google.com/file/d/1noW9_eumjTPbapD2OW8Ge2AKXabkWoyP/view?usp=sharing
https://drive.google.com/file/d/10oKbswKKHqzF-0DT3fFUpKz7H61EhRmz/view?usp=sharing
https://drive.google.com/file/d/1ZjHQseJFH1re7mKb1vH-MmL8jB6JeluO/view?usp=sharing

Screenshots…attached
Need Help Pls…


Control stepper motor with slider or joystick
#2

try to use AccelStepper.h It is more simple and includes various control method.

Here is the link http://www.airspayce.com/mikem/arduino/AccelStepper/

I think best spec is acceleration. Because stepper motor is syncronous motor. That mean, if you want to drive your motor on high speed, you need to accelerate your rotor.

First of all, you have to run your stepper without blynk.

define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;

#include "AccelStepper.h"
AccelStepper stepper(1, 0, 2);

int previous = 0;
int long degreeVal = 0;
int stepPos;
char auth[] = "tooookeeeeennnn";
char ssid[] = "yurdakul";
char pass[] = ".................";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(3, OUTPUT);
  pinMode(1, INPUT_PULLUP);

  stepper.setMaxSpeed(550);
  stepper.setAcceleration(800);
  timer.setInterval(5000L, stp);

  digitalWrite(3, LOW);
  while (digitalRead(1) == HIGH) {
    digitalWrite(0, HIGH);
    delay(1);
    digitalWrite(0, LOW);
    delay(1);
  }
  digitalWrite(3, HIGH);
  
}

void loop()
{
  Blynk.run();
  timer.run();
}
bool isFirstConnect = true;
BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();
    isFirstConnect = false;
  }
}

BLYNK_WRITE(V0) {
  stepPos = param.asInt();
}

void stp () {
  if ((stepPos > previous) || (stepPos < previous)) {
    degreeVal = map(stepPos, 20, 80, 0, 400);
    digitalWrite(3, LOW);
    stepper.runToNewPosition(degreeVal);
    digitalWrite(3, HIGH);
    previous = stepPos;
  }
}

I use this code with easy driver and esp8266 to control my heater. This lib compatible with all kind of stepper motors


#3

Would you pls explain
AccelStepper stepper(1, 0, 2);

does 1,0,2 represents pins of stepper motor 1n1, in2 etc…

Also clarify me about pin mode 3 and 1
why you have used
may be used as a enabler pin for pwm …
Pls help out …


#4

@ManiYr07 I agree with this statement… Blynk itself doesn’t control steppers, relevant stepper code and libraries do.

Get it working and understand how it works with good old fashioned buttons or whatever the library examples use, then you can add in Blynk’s GUI interface to make it work from your phone.


#5

@Gunner Would you :slight_smile:


#6

Sorry, I have my own projects I am working on… I can’t teach you how to use steppers and stepper libraries. That is not what we do here.

But when you understand the stepper side, then we can help teach you about the Blynk integration.


#7

I actually run stepper and it works fine using Arduino uno
I want to use it through wireless…


#8

I haven’t tried the AccelStepper library yet…

But my past experience has been, due to steppers timing needs, I just used a normal UNO for the stepper code and sent commands to it from another ESP running Blynk… as demonstrated a bit here…

https://community.blynk.cc/t/turning-old-arduinos-into-perfectly-usable-iot-devices/23456/2


#9

First of all google accelstepper wiring.

In my code 1, 0, 2 config shows that

1 for configuration. 2 pin stepper controller. For h bridge you must select right option.

Other digits indicates connection numbers. I used 8266esp so 0 and 2 works as step pulse and direction

https://www.pjrc.com/teensy/td_libs_AccelStepper.html

Follow this link. It is pretty clear. Lastly i offer accelstepper because running and stopping cycle controlled with acceleration to increase accuracy.


#10

My stepper projects started without blynk. Accelstepper has goto function to move stepperforward and backward specific angles.

Just we need to get value from blynk slider, button or etc. To goto variable.


#11
/*     Simple Stepper Motor Control Exaple Code
 *      
 *  by Dejan Nedelkovski, www.HowToMechatronics.com
 *  
 */
// defines pins numbers
const int stepPin = 3; 
const int dirPin = 4; 
 
void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
}
void loop() {
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  delay(1000); // One second delay
  
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for(int x = 0; x < 400; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);
}

I am using now A4988 stepper driver to control stepper motor
From this code I can successfully run stepper left right after delay using uno …

However I cant understand which pin ( Direction or step ) you have used in your code

pinMode(3, OUTPUT);
pinMode(1, INPUT_PULLUP);

this is the code you have attached …

What is 3 and 1 represent here…
Also tell me which stepper controller you have used …

If possible pls reply as quickly as possible
Awaiting for your kind response…


#12

Those two lines and pin numbers represent essential Arduino pin mode settings, nothing to do with Stepper control

https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/

You need to find a tutorial or something that will teach you the basics of programming Arduinos… we do NOT do that here.

Until you understand Arduino basics a bit better there is not much we can do to assist.

Oh, and I had to edit your post again to format any posted code here, as per the directions…