Controlling a simple stepper motor with Blynk

Hello everyone ! :slight_smile:

Before getting started I want to say that I read every thread about this subject but could not find a suitable answer :frowning:

So here I come in desperation, hoping someone might help ! :smiley:

So I have an Arduino Uno connected to WiFi through ESP module, which works perfectly fine. I’m able to do a lot of things with it but now I really need to control a Stepper Motor !

I’m using a 28BYJ-48 Stepper Motor with ULN2003 driver, which are fantastic and work absolutely fine with my Arduino.

My idea is to have a simple button on Blynk, and if I press it my motor will go forward. I want to start easy, I don’t care about it being able to stop or whatever ! If I get this to work I should be ok by my self for a more advanced use.

I’m using the Stepper library that, from what I understood is using Delay, which interrupt Blynk.run. So when I try to call a “go forward” function triggered by a blynk button, it all gets very messy and the motor will work at best for 3 seconds, then stop.

I’m making a robot so my Arduino is powered by a 5v battery, and I would like to keep only one Arduino in this robot.

A solution I heard about was to use a second Arduino, so the first triggers the second arduino and this way there is no mess with the delays. At this point I don’t know how to do that, and as I said if I can keep only one Arduino and one battery in this project it would be nice !

I have like 5 test codes so I won’t post them all here, I think everyone understood what I’m trying to achieve here :wink:

Thanks in advance to all the Blynkers !

You better post your code (formatted properly)

#include <Servo.h>
#include <Stepper.h>
#include <SoftwareSerial.h>
#include <BlynkSimpleStream.h>

SoftwareSerial DebugSerial(2, 3); // RX, TX

#define BLYNK_PRINT DebugSerial
#define BLYNK_INTERVAL 100
uint32_t lastUpdate = 0;

      char auth[] = "**";

      const int stepsPerMotorRevolution = 48;  //No of steps per internal revolution of motor,                                    
      const int stepsPerOutputRevolution = 48*64; //no of steps per revolution of the output shaft
      const int motorpin1 = A3;                    //Assign motor  pins to Arduino pins
      const int motorpin2 = A2;                    //
      const int motorpin3 = A1;                   //
      const int motorpin4 = A0;                   //

      // initialize the stepper library on pins 8 through 11, Motor rev steps, initialise firing sequence 1-3-2-4 
      Stepper myStepper(stepsPerMotorRevolution, motorpin1,motorpin3,motorpin2,motorpin4);    

void setup() 
{
      myStepper.setSpeed(60); 
      DebugSerial.begin(9600);

      // Blynk will work through Serial
      Serial.begin(9600);
      Blynk.begin(auth, Serial);
}

      BLYNK_WRITE(V3)
    {
      int b2 = param.asInt();
      if (b2 == 0)
      {
        avance();
      }
    }

void avance()
    {
     myStepper.step(200);
    }

void loop()
{
        if (millis() - lastUpdate > BLYNK_INTERVAL)
          {
            lastUpdate = millis();
             Blynk.run();
          }
       
}

this is the simplest one, working through USB Serial.

I tried to remove every possible cause of malfunction so it’s very simple code. Yet it’s not working :frowning:

I’ve got another one with v3 as a slider and myStepper.step(param.asInt);

None of those work :head_bandage:

shouldn’t it be ==1 ?

Still does not work…

I will change my approach and use a simple dc motor instead, so there is no delay and no problem !

Also I have a little question : I used a servo in a previous project on blynk using serial connection with a computer and it was working like a charm.

Now that I use the esp, everytime there is a input (everytime I press something on my blynk app) the servo is twitching back and fort approximately 5 degres. If there is no input at all, it also does it every 10 seconds. I tried with 3 servos and it does that everytime. Any idea ? :slight_smile:

Thx for your answer !

It appears to be a minor conflict with shared hardware timers between the servo libraries, SimpleTimer and even Blynk running in he background.

I have been able to mitigate the issue by disconnecting the servos after any required movement. Here is a functional servo sweep routine that used timers instead of delays and detaches the servo when not moving:

//===== Servo Sweep Switch Function =====
BLYNK_WRITE(V0) // Switch Widget
{
  SrvoButtonState = param.asInt();
  if (SrvoButtonState == 1) {
    myservo.attach(servoPin);
    timer.setTimer(800L, Servo1, 1);  // Run Servo1 loop every 800ms
  } else
    myservo.detach();
} // END Blynk Function

void Servo1() {
  myservo.write(160);
  timer.setTimer(800L, Servo2, 1);  // Run Servo2 loop every 800ms
}

void Servo2() {
  myservo.write(0);
  Blynk.syncVirtual(V0);  // Check if switch still activated
}

For any advanced use of steppers and servos, I would recommend external smart controllers that take the info from the MCU but handle all the timing and control of the motors themselves.