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

DigitalWrite() not working as expected in Blynk Function


#1

HW: Node MCU esp32s
Phone: Xperia X Compact
Blynk: 0.6.6 I think

I am trying to write a program that would open a PNP transistor connected to pin 0 for .75 seconds every time a button in the app is pressed, no matter how long the button is pressed for.


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

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

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

unsigned long currentTime = 0;
const long PWRPress = 750;
unsigned long timePressed = 0;
int V1Value = 0;
bool finishedness = HIGH;

BLYNK_WRITE(V1)
{
  V1Value = param.asInt();// assigning incoming value from pin V1 to a variable
  Serial.print("V1 value is:");
  Serial.println(V1Value);
  
  if (V1Value == 1 && finishedness == HIGH){
    Serial.println("Turning relay on");
    digitalWrite(PWRpin, LOW);
    timePressed = millis();
    finishedness = LOW;
  }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  digitalWrite(PWRpin, HIGH);
}

void loop()
{
  Blynk.run();
  //Remember to avoid delay() function!
   currentTime = millis();
  
  if( currentTime >= timePressed + PWRPress && finishedness == LOW){
    Serial.println("Turning relay off");
    digitalWrite(PWRpin, HIGH);
    finishedness = HIGH;
  }
}

The board sends the value of V1 every time the value changes just as it should, but it doesn’t open the transistor. I have already concluded that the hardware works, because the basic blynk example where the app controls a pin directly does trigger the transistor. Why is the digitalWrite not working?


#2

You title is misleading… BLYNK_WRITE() functions just fine… it is whatever you are doing once it grabs the widgets state that is your issue.

We don’t really teach programming here, but I would suggest you keep your void loop() clear of extraneous code and handle your “timed” GPIO process differently.

Perhaps using a BlynkTimer process to handle the timing?

BLYNK_WRITE(V1) {
  if (param.asInt()) {  // If receiving a 1
    digitalWrite(PWRpin, HIGH);  // toggle pin HIGH
    timer.setTimeout(750L, []() {  // toggle pin LOW 0.75 seconds later
      digitalWrite(PWRpin, LOW);
    }
  }
}

#3

Thank you for the suggestion. However, my problem is that the digitalWrite inside the BLYNK_WRITE function does not set the pin to LOW. Because I need to control a PNP transistor, the “off” state is actually HIGH on pin 0 and the “on” is LOW on pin 0. Is there anything I could do to fix that?


#4

Umm, just set the GPIO to whatever value you want to turn your transistor “ON”… HIGH or LOW and the inverse for the opposite

BLYNK_WRITE(V1) {
  if (param.asInt()) {  // If receiving a 1
    digitalWrite(PWRpin, LOW);  // toggle pin LOW to activate PNP
    timer.setTimeout(750L, []() {  // toggle pin HIGH to deactivate PNP  0.75 seconds later
      digitalWrite(PWRpin, HIGH);
    }
  }
}

None of this is an issue with the Blynk function… so… :thinking: again your title is misleading.


#5

Yes, but as you can see I already have a digitalWrite(PWRpin, LOW) inside the BLYNK_WRITE function and it does not work. When I press the button in the app, the board sends all the serial messages (“V1 value is:1”, “Turning relay on”, “Turning relay off”), but it does not actually turn the relay on and off, the value on pin 0 always stays on HIGH no matter what.


#6

I tried the exact same HW configuration in some other programs and the same digitalWeite worked. The only difference is that in those other programs I did not use the BLYNK_WRITE function.


#7

Well… I also see that you do not have a pinMode(PWRpin, OUTPUT); command in your void setup()… perhaps you might add that little necessity :stuck_out_tongue:

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

I guess we have to teach some programming here after all :innocent:


#8

Thanks, that might be it! I wrote this thing quite late in the night and it didn’t even cross my mind that I’d be missing something so simple. Consider this solved.