ledWrite/PWM Dimming - constant on -Arduino IDE/ESP32

Hi community,

i try to get a ESP32 working as a PWM dimming module for LED. i have a SoftPot which is being mapped to the output. The dimming is working put only when im touching the Pot. When im releasing the finger the LED switch of. Im a bit confused with the freq/LED channel/resolution (is it a ESP32 thing or is it Arduino IDE like?).

So i cant find a solution to keep after touching the LED on with the PWM state i touched until i touch another state at the softpot.

Thanks for the help

gnom

const int ledPin = 16;  // 16 corresponds to GPIO16
uint16_t dutyCycle;
uint16_t old_dutyCycle;

// setting PWM properties
const int freq = 9000;
const int ledChannel = 0;
const int resolution = 10;

void setup(){
  
  Serial.begin(115200);
  // configure LED PWM functionalitites
  ledcSetup(ledChannel, freq, resolution);
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(ledPin, ledChannel);

}

void loop(){
   
   dutyCycle =  analogRead(34);
    
   Serial.println(old_dutyCycle);

   if (old_dutyCycle != dutyCycle) {
      old_dutyCycle = dutyCycle;
      // changing the LED brightness with PWM
      ledcWrite(ledChannel, dutyCycle);
   }
   
   delay(100);
  }

The dutyCycle goes to 0 when you release your finger. The question is how does your software know that you are just removing your finger or you really want to turn the LED off? It doesn't.

You could use a double tap to turn the LED on and double tap to turn it off. You would have to time dutyCycle going from 0 to non-zero twice within a certain time period. When the LED is on ignore dutycycle of 0 and always use the last non-zero value.

Alternatively you could add an LED on/off switch. This would make the code more simple.

Another alternative would be to use a rotary pot.

Thanks for the thoughts.

The idea for the double tab is really nice. For the version 2.0 it will be perfect.

Right now I have the idea of dimming the led light. A rotary pot is to big for the wall. Should be the soft pot next to the lamp at the wall.

Is there a possibility to exclude the state of “0”? Then I would still have 4094 states for dimming?!

Thanks for the patience

Gnom

gnom:
Thanks for the thoughts.

The idea for the double tab is really nice. For the version 2.0 it will be perfect.

Right now I have the idea of dimming the led light. A rotary pot is to big for the wall. Should be the soft pot next to the lamp at the wall.

Is there a possibility to exclude the state of “0”? Then I would still have 4094 states for dimming?!

Thanks for the patience

Gnom

I have never actually used a softpot so I'm just throwing out ideas...

Assuming the analog input doesn't bounce you could assume 0 means use the last non-zero value. Also, you could designate any values in a range that correspond to about the bottom "finger width" of the softpot to be interpreted as LED off. You would lose some dimming range but it may not matter. You may need to account for some "settle" time after the value changes. I don't know how fast the value will come down after you remove your finger.

I also just thought that the remainder of the range could be mapped to full range with a slight loss of precision. That way touching the very bottom of the SoftPot would turn the LED on or off. The remainder of the strip would be the dimming range.

Thanks for the thoughts! I used now capacitive sensors for the dimming. was more simple! ... but i think i will do some other projects with the stripes. We ll see.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.