Controlling LED brightness with Potentio and Blynk

Need help,
I am a beginner and currently working with ESP8266 to control LED brightness. my circuit scheme is connecting a potentiometer to analog pin A0, and connecting my LED to digital pin D6. My codes configuration seems fine when i controlled the brightness manually only by using potentiometer (hardware), but is it possible to also control the LED brightness using virtual pin? so it will looks like some sort of value interrupt in Blynk. I attach my codes below


int LED = D6;
int POTENSIO = A0;
int pinVal;
int sensorvalue;

BLYNK_WRITE(V0){
  pinVal = param.asInt();
  analogWrite(LED, pinVal);
  Blynk.virtualWrite(V1,pinVal);
}

void setup(){
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(LED, OUTPUT);
}

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

void analog_pot(){
  sensorvalue = analogRead(POTENSIO);
  analogWrite(LED, sensorvalue);
  delay(2);
}
}

First of all, you should read this:

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Calling your 'analog_pot` function from the void loop isn’t going to work with Blynk.

Secondly, are you saying that your BLYNK_WRITE(V0) isn’t working to dim our physical LED?
If so, then are you using Blynk IoT or Legacy?
If you’re using Legacy then how is the V0 widget configured?
If you’re using IoT then how is your V0 datastream configured (especially the Nin/Max values)?

Pete.

Thanks, Pete for the document, it really helps.

I’ve worked on the void loop( ) to make it clean, and here’s the update. It does make the virtual pin works, but when I tried to control it manually via the potentiometer (hardware), it won’t work, because I don’t know where to put the ‘analog_pot’ function.

int LED = D6;
int POTENSIO = A0;
int pinVal;
int sensorvalue;

BLYNK_WRITE(V0){
  pinVal = param.asInt();
  analogWrite(LED, pinVal);
  Blynk.virtualWrite(V1,pinVal);
}

void analog_pot(){
  sensorvalue = analogRead(POTENSIO);
  analogWrite(LED, sensorvalue);
  delay(2);
}

void setup(){
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(LED, OUTPUT);
}

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

To answer your question, my BLYNK_WRITE(V0) does work to dim our physical LED. but the concept of this experiment is, I want to make my potentiometer (hardware, connected to ADC input via A0 analog pin, can be functioning to dim the LED too. so both analog input and virtual input is working. is that seems possible?

But you’ve obviously not understood the principal of using a BlynkTimer and removing the blocking delay from your sketch.
When you fix this, please post your ENTIRE sketch, and answer this question…

It is entirely possible to use both a physical potentiometer and a Slider widget to control the same physical LED, but you need to get your code structure sorted-out first.

Pete.

Ah my bad, should’ve use BlynkTimer to set the interval.

here’s the updated one,

#define BLYNK_TEMPLATE_ID "TMPLWhZDOacJ"
#define BLYNK_DEVICE_NAME "Potensio TEST"
#define BLYNK_AUTH_TOKEN "Vwcm8N_GWb8jdeGqFUct-Ui39xCAeeXq"
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

int LED = D6;
int POTENSIO = A0;
int pinVal;
int sensorvalue;

BlynkTimer timer;

void myTimer(){
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);
}

BLYNK_WRITE(V0){
  pinVal = param.asInt();
  analogWrite(LED, pinVal);
  Blynk.virtualWrite(V1,pinVal);
}

void setup(){
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(LED, OUTPUT);
  timer.setInterval(1000L, myTimer);
}

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

and for this particular question…

, I believe that I used Blynk.console…(?) since I only use the web app, not the mobile app.

You are using Blynk IoT.

So, you’ve abandoned your analog_pot function and are using your timer to call myTimer instead, but myTimer doesn’t read your analog port and do anything with the results.
Strange!

If you fixed this, the you’d want to be reading your analog pin more frequently than once every second.

Maybe you should go an re-read the “keep your void loop clean” document again, so that you actually understand what the timer is meant to achieve and why, instead of randomly copying and pasting pieces of code.

Pete.

Thanks for your brief explanation, Pete.

I’ve finally come to almost understand the code structure. I’m making a function potentio_read to be called inside my timer. and it does read my analog port and printed it in serial monitor.

I’ve set the timer to run every 100ms, and already making the void loop clean with only Blynk.run() and timer.run() only.

however it still doesn’t solve my problem of interrupting the analog value that has been recorded on analog pin, here’s my latest code.

#define BLYNK_DEVICE_NAME "Potensio TEST"
#define BLYNK_AUTH_TOKEN "Vwcm8N_GWb8jdeGqFUct-Ui39xCAeeXq"
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

int LED = D6;
int POTENSIO = A0;
int pinVal;
int sensorvalue;

BlynkTimer timer;

BLYNK_WRITE(V0){
    pinVal = param.asInt();
    analogWrite(LED, pinVal);
    Blynk.virtualWrite(V1,pinVal);
    }

void potentio_read(){
    sensorvalue = analogRead(POTENSIO);
    Blynk.virtualWrite(V2, sensorvalue);
    analogWrite(LED, sensorvalue);
    Serial.print("\n sensor value: ");
    Serial.println(sensorvalue);
}

void setup(){
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(LED, OUTPUT);
  pinMode(POTENSIO, INPUT_PULLUP);
  timer.setInterval(100, potentio_read);
}

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

Thank you in advance, Pete

I’m not sure from your code or your sensor value what this line of code is supposed to do, but the sensible thing to be doing here is to write the potentiometer value to the slider widget, which appears to be V0, not V2.

Doing that will keep your slider synchronised with your potentiometer.

The problem you have is that when the slider value changes it will update the LED, but then this value will be overridden by the reading from the potentiometer.
As you can’t physically move the potentiometer to the same value as the slider (there is no way to do the Blynk.virtualWrite in reverse) then you’ll need to introduce a way of tracking which value changed last (the slider widget or the physical potentiometer), and using that one for your LED brightness. Value drift on your potentiometer may be an issue, but that will depend on the type and quality of the hardware.

The other issue you’ll have is that, when you move your potentiometer you may need to turn/slide it a significant distance before it’s value matches that last value from the slider widget.

Whether that’s an issue will depend on your application.

Personally, I’d use a rotary encoder rather than a potentiometer, but once again it depends on your application - which is something you’ve not shared with us.

Pete.