Sometimes failed to control the component via Virtual pin & button widget

My project is sample. Connect 3 small DC motors into GPIO 4, 5 and 6 of ESP32 dev kit.
There are 3 buttons widget using D4, D5 & D6 on the Blynk app, and I can control the motor directly without any coding.

Besides, I created 2 buttons using V0 and V1 to control different motors with some simple codes, however, sometimes it works, sometimes the motors were not working (the button is activated).
Meanwhile, I pressed D4/D5/D6 button, the motor works & V0 and V1 buttons still not work.

After that, I reset the ESP32 board (by press reset button or reconnect power) and try again, it works again.

Is there any problem for my codes? I am using strong strength wifi in the room with > 100mbps.


int motor1 = 4;
int motor2 = 5;
int motor3 = 6;

BLYNK_WRITE(V0)
{
  int manualCold = param.asInt();
  if (manualCold == 1)
  {
    digitalWrite(motor1, HIGH);
    digitalWrite(motor2, HIGH);
  }
  else 
  { digitalWrite(motor1, LOW);
    digitalWrite(motor2, LOW);
}

BLYNK_WRITE(V1)
{
  int manualHot = param.asInt();
  if (manualHot == 1)
  {
    digitalWrite(motor2, HIGH);
    digitalWrite(motor3, HIGH);
  }
  else 
  { digitalWrite(motor2, LOW);
    digitalWrite(motor3, LOW);
}


void setup()
{
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(motor3, OUTPUT);
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
digitalWrite(motor3, LOW);
Blynk.begin(auth, wifi, pass);
}

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

Are your motors connected directly to the ESP32?
If so, you’d better order a large supply of ESP32 boards, as you’ll be getting through them quite quickly.
Alternatively, you could order some H-Bridge controllers.

Pete.

Hi Pete

No, they are not connected directly to ESP32 as they are 12V motors.

ESP32 --> 3.3V output signal --> PNP transistor board with optocoupler and drive with external 12VDC power --> 12V motors

I have always had issues when combining Blynk’s built-in GPIO control with normal digitalwrite() control via virtual pins and functions.

Try removing all direct GPIO widgets… there is also a command to disable that functionality, but I don’t have it offhand… and only use virtual pins.

I removed all GPIO widgets and use virtual pins only, sometimes problem still occurs

You may be confusing things a bit with motor2 being controlled in both virtual pin functions.

Yes, it is my logic
Actually motor2 is a water valve, motor 1 is hot water pump, motor 3 is cold water pump.

So, when V0 pressed, hot water will out, when V1 pressed, cold water will out.
And V0 and V1 will ONLY activate either one.

Without proper feedback, it might be possible for one button to LOOK activated/deactivated, when the other button is actually the one controlling the true motor2 GPIO state… so perhaps your perception of what is happening differs with your codes actual logic?

I think you will find that virtual pins work very effectively and reliably… as long as you follow the codes logic correctly.

#define BLYNK_NO_BUILTIN   // Disable built-in analog & digital pin operations

Pete.

1 Like

Hi GTT

Would you mean I should add below codes (bold font) to get feedbacks?
e.g.
BLYNK_WRITE(V0)
{
int manualCold = param.asInt();
boolean motor1State = digitalRead(motor1);
boolean motor2State = digitalRead(motor2);
if ((manualCold == 1) && (motor1State == false) && (motor2State == false))
{
digitalWrite(motor1, HIGH);
digitalWrite(motor2, HIGH);
}
else
{ digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
}

Perhaps add a Virtual LED for each motor, then right after each individual motor control, send a virtual write to the appropriate LED to indicate ON or OFF. Then you can see what the code path is, not just how the motor runs.

You could also send virtual writes back to the buttons/switches as needed to have the code turn them ON or OFF, if applicable based on action of another button/switch.