hi…
i’m trying to control a led brightness with the slider in thinger dashboard
i’m writing the code as shown below , what’s wrong ??
why the slider go back to zero !!
i’m using NodeMCU … v1.0
Hi @Hussam_Farhat, I think that your code tries to change a constant value that is unacceptable ( #define LED D0). Futhermore, in order to control the brightness of an LED you have to use PWM (Pulse Width Modulation). In your code you use D0 pin that, as I am aware, does not support PWM.
So, your code should look like the example below (I’ve used D1 pin):
It worked as long as my slider is set 0 to 1023. If my slider has a value of up to 255 it doesn’t work. Another question, why my slider is at maximum value, my LED turn off and when it is at minimum it turns on?
It should be because, for example, the nodemcu esp8266 has inverted logic for the builtin led (when its pin is LOW it turns ON, when its HIGH turns OFF).
It seems that the ESP32 way of managing PWM signals is a little bit different from the ESP8266, in that you need to set the frequency, the channel and the resolution, as well as using different functions.
With the latest libraries of ESP32 and Thinger this code seems to work:
#include <ThingerESP32.h>
#define USERNAME "*********"
#define DEVICE_ID "*********"
#define DEVICE_CREDENTIAL "*********"
#define SSID "*********"
#define SSID_PASSWORD "*********"
#define LED 16
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void setup() {
thing.add_wifi(SSID, SSID_PASSWORD);
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(LED, ledChannel);
thing["led"] << [](pson& in){
if(in.is_empty()){
in = ledcRead(ledChannel);
}
else {
ledcWrite(ledChannel,in);
}
};
}
void loop() {
thing.handle();
}
I’ll leave a couple of references if you are interested in further investigating: