BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

NEMA 17 stepper motor and drive A4988 does not work on app blynk

Hello! I can’t get my NEMA 17 stepper motor to work on the BLYNK app, I’m using an UNO arduino, an A4988 drive and a W5500 ethernet shield.

In program 1 (without the blynk app) the stepper motor worked correctly.

In program 2 (with Blynk app) I can download this program on the UNO arduino, the a4988 drive is working normally, the w5500 ethernet shield is transmitting correctly, the blynk app gives a signal that it is connected, BUT, BUT the step does not rotate according to the program, when I press the virtual button on the blynk app (Android), the stepper motor gives a “spasm” Please help me with what I did wrong in program 2. Thank you very much.


PROGRAM 1:

/*     Simple Stepper Motor Control ExaMple 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);
}

PROGRAM 2:

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet2.h>
#include <BlynkSimpleEthernet2.h>

const int stepPin = 3; 
const int dirPin = 4;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";
char ssid[] = “id”;
char pass[] = “password”;

// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  
  if (pinValue == 1){
  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

else {

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

@Galileu I’ve edited your post to add triple backticks at the beginning and end of your Program 2 code so that it displays correctly. Please add these yourself in future otherwise your code will be deleted.
For refernce, triple backticks look like this:
```

This is your problem, GPIO4 is reserved as the Chip Select (CS) pin on the Ethernet shields and cant be used for anything else when the Ethernet shield is in use.

Pete.

I’ll change the pin, thank you very much, when I get home and change the program.

Thanks

1 Like

Program 2 was incomplete and I cannot send it. Anyway, void setup () and void loop () were basically missing from the configurations of the blynk examples from the “Virtual Pin Read” program.

Thanks

Do you mean in the Sketch Builder?

If so, it looks fine to me…
https://examples.blynk.cc/?board=Arduino%20Uno&shield=Ethernet%20Shield%20W5500&example=GettingStarted%2FVirtualPinRead

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  This sketch shows how to read values from Virtual Pins

  App project setup:
    Slider widget (0...100) on Virtual Pin V1
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet2.h>
#include <BlynkSimpleEthernet2.h>

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

// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1
BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable

  // process received value
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth);
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 80);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8080);
}

void loop()
{
  Blynk.run();
}

Pete.

That’s right. It was this Sketch that I used as an example.

Well the void setup and void loop in that example are fine.

Pete.

Both Blynk and stepper motors are a bit prima donna in their specific timing needs.

It can be done, but you may need to work some fancy coding… Here is an example I made using ESP8266/32 You should be able to change the libraries needed for the UNO/Ethernet and start experimenting, but as the UNO is quite a bit slower, the stepper may stutter more.

1 Like

@Gunner nice to see you dropping by and sharing some good stuff again. Hope you’re keeping well.

Pete.

@PeteKnight I changed the pins to const int stepPin = 9;
const int dirPin = 8; and it still doesn’t work. Is there a mistake in the sketch?

What exactly does this mean?

Pete.

The stepper motor spins very irregularly when I press the virtual button in the blynk app, it appears “connected” in the blynk app, but the engine does not rotate as it should. Without the ethernet board, the stepper motor spins normally, according to this sketch (https://howtomechatronics.com/tutorials/arduino/how-to-control-stepper-motor-with-a4988-driver-and-arduino/)

Thanks.

I had the same issue. The stepper would work but not very smooth. I then found a topic of what to do with your old arduino and I went down that route. So I have a nano which runs the stepper code with its microsecond delays ect. and then I use an esp8266 d1 mini which communicates to the nano. I will post the code up tomorrow as it’s a bit hard to do on my phone.

I’m starting to believe that UNO is no good for these things. I’ll be waiting for the sketch. Thank you.

Ok this is how I got my stepper to work smoothly.

ESP8266 D1 Mini Code

 /*
AquArt Blynk StepperMotor_Esp

WRITTEN BY MARIANO PROIETTI
16 CRANE AVENUE
ISLEWORTH
MIDDLESEX, TW7 7JL
TEL. 07771 528 969
*/

/*BLYNK APP
 * V0 TIME TAG
 * V1 DATE TAG
 * V5 TITLE TAG
 * V6 STEP MODE SELECTION
 * V7 SPEED GAUGE
 * V8 TIME INPUT
 * V9 SPEED INPUT
 */

//LIBRARIES//           
#include <ESP8266WiFi.h>         
#include <BlynkSimpleEsp8266.h>
               
#include <ESP8266mDNS.h>  //For OTA w/ ESP8266
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#include <TimeLib.h>
#include <WidgetRTC.h>
BlynkTimer timer;
WidgetRTC rtc;

//PIN ASSIGNMENTS//
#define ACTIVE D1
#define SPEED D2
#define M0 D5
#define M1 D6
#define M2 D7
#define LEDON D4

//WIFI GLOBALS//
char modulename[] = "AquArt IntelliPUMP 1";      //Insert app name
int ReCnctFlag;         //Reconnection Flag
int ReCnctCount = 0;    //Reconnection counter

char auth[] = "";  
char ssid[] = "";                                            
char pass[] = "";                       

char server[] = "blynk-cloud.com";            //URL for Blynk Cloud Server
int port = 8080;

//GLOBALS//
int Mode;
int Speed;
int svSpeed;

//RTC & TIME IMPUT ATTRIBUTES & VARIABLES
char currentTime[9];    //RTC time
char currentDate[11];   //RTC date

//CONNECT TO BLYNK SERVER//
BLYNK_CONNECTED()
{
  Blynk.syncAll();
  rtc.begin();
  ReCnctCount = 0;
  ArduinoOTA.setHostname(modulename);
  ArduinoOTA.begin();
}

void setup() 
{
  Serial.begin(115200);

  WiFi.begin(ssid, pass);
  Blynk.config(auth, server, port);
  Blynk.connect();
  
  setSyncInterval(30);
  Blynk.virtualWrite(V5, "IntelliPUMP 1");

  pinMode(M0, OUTPUT);
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);
  pinMode(DIR, OUTPUT);
  pinMode(ACTIVE, OUTPUT);
  pinMode(SPEED, OUTPUT);
  pinMode(LEDON, OUTPUT);

  timer.setInterval(1000L, ClockDisplay);
}

void loop()
{
  timer.run();
  ArduinoOTA.handle();
  if (Blynk.connected())
  {
    Blynk.run();
    digitalWrite(LEDON, HIGH);
  }
  else if (ReCnctFlag == 0)
  {
    ReCnctFlag = 1;
    timer.setTimeout(5000L, []()
    {
      ReCnctFlag = 0;
      ReCnctCount++;
      Blynk.connect();
      digitalWrite(LEDON, LOW);
    });
  }
}

//BLYNK FUNCTIONS////////////////////////////////////////////////////////////////////

BLYNK_WRITE(V6)
{
  Mode = param.asInt();
  {
    switch (Mode)
    {
      case 1:   //FULL STEP
      {
        digitalWrite(M0, LOW);
        digitalWrite(M1, LOW);
        digitalWrite(M2, LOW);
        break;
      }

      case 2:   //1/2 STEP
      {
        digitalWrite(M0, HIGH);
        digitalWrite(M1, LOW);
        digitalWrite(M2, LOW);
        break;
      }

      case 3:   //1/4 STEP
      {
        digitalWrite(M0, LOW);
        digitalWrite(M1, HIGH);
        digitalWrite(M2, LOW);
        break;
      }

      case 4:   //1/8 STEP
      {
        digitalWrite(M0, HIGH);
        digitalWrite(M1, HIGH);
        digitalWrite(M2, LOW);
        break;
      }

      case 5:   //1/16 STEP
      {
        digitalWrite(M0, LOW);
        digitalWrite(M1, LOW);
        digitalWrite(M2, HIGH);
        break;
      }

      case 6:   //1/32 STEP
      {
        digitalWrite(M0, HIGH);
        digitalWrite(M1, LOW);
        digitalWrite(M2, HIGH);
        break;
      }

      case 7:   //CALIBRATION MODE CODE STILL TO BE WRITTEN
      {
      }
    }
  }
}

BLYNK_WRITE(V8)
{
  TimeInputParam t(param);
  StTime = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
  SpTime = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
  int dayadjustment = -1;  
  if(weekday() == 1)
  {
    dayadjustment = 6;
  }
  if (t.isWeekdaySelected((weekday() + dayadjustment)))
  {
    if(t.hasStartTime())
    {
      digitalWrite(ACTIVE, HIGH);
    }
    else if(t.hasStopTime())
    {
      digitalWrite(ACTIVE, LOW);
    }
  }
  else
  {
    digitalWrite(ACTIVE, LOW);
  }
}

BLYNK_WRITE(V9)
{
  Speed = param.asInt();
  svSpeed = map(Speed, 0, 100, 0, 1023);
  analogWrite(SPEED, svSpeed);
  Blynk.virtualWrite(V7, Speed);
}

//CLOCK FUNCTIONS////////////////////////////////////////////////////////////////////

void ClockDisplay()
{
  sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
  sprintf(currentDate, "%02d/%02d/%04d", day(), month(), year());

  Blynk.virtualWrite(V0, currentTime);    //Send time to TIME widget
  Blynk.virtualWrite(V1, currentDate);    //Send date to DATE widget
}

Arduino Nano Code

/*
AquArt Blynk StepperMotor_Nano

WRITTEN BY MARIANO PROIETTI
16 CRANE AVENUE
ISLEWORTH
MIDDLESEX, TW7 7JL
TEL. 07771 528 969
*/

//LIBRARIES//
#include <AccelStepper.h>

//PIN ASSIGNMENTS//
#define DIR 2 
#define STP 3 
#define M0 4
#define M1 5
#define M2 6
#define ACTIVE 7
#define svM0 9
#define svM1 10
#define svM2 11

//ACCELSTEPPER GLOBALS//
#define motorInterfaceType 1
AccelStepper stepper = AccelStepper(motorInterfaceType, STP, DIR);
 
//GLOBALS//
int Speed;
int SpeedValue;
int ActiveValue;

void setup()
{ 
  stepper.setMaxSpeed(1000);
  pinMode(M0, INPUT);
  pinMode(M1, INPUT);
  pinMode(M2, INPUT);
  pinMode(ACTIVE, INPUT);
  pinMode(svM0, OUTPUT);
  pinMode(svM1, OUTPUT);
  pinMode(svM2, OUTPUT);
}

void loop()
{
  StepResolution();
  ActiveValue = digitalRead(ACTIVE);
  if (ActiveValue == HIGH)
  {
    SpeedValue = analogRead(A0);
    Speed = map(SpeedValue, 0, 1023, 300, 1000);
    stepper.setSpeed(Speed);
    stepper.runSpeed();
  }
  else
  {
    stepper.stop();
  }
}

void StepResolution()
{
  if (digitalRead(M0) == LOW && digitalRead(M1) == LOW && digitalRead(M2) == LOW)
  {
    digitalWrite(svM0, LOW);
    digitalWrite(svM1, LOW);
    digitalWrite(svM2, LOW);
  }
  else if (digitalRead(M0) == HIGH && digitalRead(M1) == LOW && digitalRead(M2) == LOW)
  {
    digitalWrite(svM0, HIGH);
    digitalWrite(svM1, LOW);
    digitalWrite(svM2, LOW);
  }
  else if (digitalRead(M0) == LOW && digitalRead(M1) == HIGH && digitalRead(M2) == LOW)
  {
    digitalWrite(svM0, LOW);
    digitalWrite(svM1, HIGH);
    digitalWrite(svM2, LOW);
  }
  else if (digitalRead(M0) == HIGH && digitalRead(M1) == HIGH && digitalRead(M2) == LOW)
  {
    digitalWrite(svM0, HIGH);
    digitalWrite(svM1, HIGH);
    digitalWrite(svM2, LOW);
  }
  else if (digitalRead(M0) == LOW && digitalRead(M1) == LOW && digitalRead(M2) == HIGH)
  {
    digitalWrite(svM0, LOW);
    digitalWrite(svM1, LOW);
    digitalWrite(svM2, HIGH);
  }
  else if (digitalRead(M0) == HIGH && digitalRead(M1) == LOW && digitalRead(M2) == HIGH)
  {
    digitalWrite(svM0, HIGH);
    digitalWrite(svM1, LOW);
    digitalWrite(svM2, HIGH);
  }
  else
  {
    digitalWrite(svM0, LOW);
    digitalWrite(svM1, LOW);
    digitalWrite(svM2, LOW);
  }
}
    
//END//

Hope this is of some help to you.

1 Like

Today I will not be able to change the sketch. Tomorrow I see. Thank you.

@Gunner Gunner is back Hurray !! :wink:

2 Likes

I only managed to use the ES8266, it seems that the W5500 shield does not work at all. Thank you.