Nodemcu dc motor control with L298N

hello everyone ! i am new blynk user i was made some projects with nodemcu board and some sensors and relays. Now iam trying to finish my new project so i want to drive a dc motor with nodemcu and L298N. when i push the button in blynk app (that is connected at v1 virtual pin) i want to start the motor to open a valve for 3 sec then stops and open again for 3 sec and stops for 4 hours ans then turns off. or when the button state = 0 to turn off the valve. i read a lot to do it, but i didint find a solution. i think it is simple but a cant find where is my error. i made some changes

thank you !
here is the code

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <SimpleTimer.h>
SimpleTimer timer;
#include <BlynkSimpleEsp8266.h>
char auth[] = "54a5a5c2135d49dfa6016165920629e9";
char ssid[] = "CYTA14D6";
char pass[] = "ZTETG8FG9M06103";

int IN1 = D0;
int IN2 = D2;
int ENA = D3;                              //pmw motor

void setup()
   Blynk.begin(auth, ssid, pass);
  //You can also specify server:
  //Blynk.begin(auth, ssid, pass, "", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080); 
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENA, OUTPUT);


BLYNK_WRITE(V1)                            //the motor works with l298n bringe 
  int buttonState = param.asInt();
   if ( param.asInt()  == 1)
    OpenValve;                             //open valve
    delay (3000);
    OpenValve;                             //open again for 3 sec
    delay (3000);
    void OpenValve()                       //first fuction
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(ENA, 255);              //motor pwm adjust at 255
    void CloseValve()                     // secont function
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(ENA, 255);

    void StopValve()                     //third function
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);

void loop()

You’re working on the principal that these timers will block the execution of the next line of code until they’ve completed.
The whole principal behind using SimpleTimer and BlynkTimer it that they are non-blocking.
(BlynkTimer is included in the BlynkSimpleEsp8266.h library, so you don’t actually need to use SimpleTimer).

This means that your code actually does this:

OpenValve; //open valve
timer.setTimeout(3000L,StopValve); // Start a timer that will call StopValve every 3 seconds
OpenValve; //open the valve (executes IMMEDIATLY after the previous line)
timer.setTimeout(3000L,StopValve);  // Start another timer that will call StopValve every 3 seconds

There’s no timer to open the valve, just one to stop it, so it gets opened twice in rapid succession the gets closed every 3 seconds. These timers are never deleted, so they keep closing the valve every 3 seconds all the time the device is powered up.

There’s no simple fix, you need to restructure your code significantly.

I’d suggest you use timeout timers to get the results you need.
Take a look at some of @Gunner’s excellent timer tutorials for inspiration.

BTW, I have my doubts about the logic of opening and closing a valve every 3 seconds for 4 hours - It’s not likely to last very long!
Also, I assume that you’re using some sort of motor shield or changeover relay between the MCU ad the motor?


1 Like

@PeteKnight Hello thanks for the quick respond ! My project is to opening a water valve is high pressure water for orchard watering. I have a dc motor for a handbrill and i adjusted it on the water valve so i want when i press the button the valve start opening for 3sec and then to stop for 10sec then i will start again the same routine and when it is almost full open to stop the motor there for 5 hour to water my plants. And then to close the valve. With arduino uno i used the delay function and it works perfectly! But i try to use it with blynk and i cant make it work. I will read the tutorial that you suggest but the problem is that when i push the button it didnt do anything , everything is wired correct and iam trying to pin D4 and the led is working when i push the button.
Do you habe any idea why is this hapening ;

Thank you very much for your time !

Hello, Teo_Efraimidis
I need to use L298N for esp32 project.
Can you tell me what kind of library you are using in this project.
Thank you

@hasanuz2001 please don’t post in multiple topics, you already opened a topic