Analog input + ESP32 + LED ( Trouble setting LED GPIO to High )

• Hardware model + communication type : ESP32
• Smartphone OS (iOS or Android) + version: iOS, 14.8
• Blynk server or local server : I am not sure what this means
• Blynk Library version : 0.1.0

Hello everyone, I am new to Blynk so please be nice :smile:
My current goal is to basically read the value from a Potentiometer and send it to virtual PIN V6,
which will then turn on the physical LED which is connected to GPIO 18 of the ESP32 if the reading of my Potentiometer is less than 2000 and the LED will turn off if the reading is more than or equals to 2000.

In my Blynk Datastreams, I have set up one of the stream to have the Virtual PIN to V6, Data type to Integer and the minimum value to be 0, while the maximum value to be 2500.

In my Arduino “setup()”, I have set my Potentiometer to be “pinMode(A5,INPUT);” and my LED to be “pinMode(18,OUTPUT);”.

In my “loop()”, I have stored the analog value from Potentiometer into the variable pinAnalogValue “int pinAnalogValue = analogRead(A5);”. After storing the analog value in that variable, I used the Blynk function to change the value of virtual pin V6 by executing the following code “Blynk.virtualWrite(V6, pinAnalogValue);”.

In my Blynk App, I have a Gauge widget, which the datastream of the widget is connected to V6. As I upload the code, my Gauge seems to work however it does not execute the if statement in my “BLYNK_WRITE(V6)” function.

Thank you for reading below is the full code in my arduino.


#define BLYNK_TEMPLATE_ID "TMPLwWBMCbXm"
#define BLYNK_DEVICE_NAME "MDTE"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG
#include "BlynkEdgent.h"
void setup()
{
  pinMode(18, OUTPUT); //LED
  pinMode(A5, INPUT);  //Potentiometer
  Serial.begin(115200);
  delay(100);
  BlynkEdgent.begin();
}

void loop() {
  BlynkEdgent.run();
  int pinAnalogValue = analogRead(A5); //Reads Potentiometer value and store it in pinAnalogValue
  Blynk.virtualWrite(V6, pinAnalogValue); //Send Potentiometer value to virtual pin V6
}

BLYNK_WRITE(V6)
{
  int AnalogValue = param.asInt();
  if (AnalogValue < 2000)
  {
    // execute this code if the Gauge widget value is less than 2000
    digitalWrite(18, HIGH); // Set digital pin 18 HIGH
  }
  else if (AnalogValue >= 2000)
  {
    // execute this code if the Gauge widget value is More than or equal to 2000
    digitalWrite(18, LOW); // Set digital pin 18 LOW
  }
}

Hey there,

Keep BlynkEdgent.run(); and remove the other lines

Now try this

int Potentiometer = A5;
int LED = D18;

void setup()
{
  pinMode(LED, OUTPUT); //LED
  pinMode(Potentiometer, INPUT);  //Potentiometer
  Serial.begin(115200);
  delay(100);
  BlynkEdgent.begin();
}

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

void sensor ()
{
int value = analogRead(Potentiometer);
Blynk.virtualWrite(V6, value);
if ( value >= 2000 ) {
 digitalWrite(LED, HIGH);
}
else if ( value < 2000 ) {
digitalWrite(LEF, LOW);
}
}