Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PWM ledCWrite(255) or ledcWrite(1023) is not 100% on #5089

Closed
hallard opened this issue Apr 20, 2021 · 6 comments · Fixed by #6045
Closed

PWM ledCWrite(255) or ledcWrite(1023) is not 100% on #5089

hallard opened this issue Apr 20, 2021 · 6 comments · Fixed by #6045
Labels
Status: Stale Issue is stale stage (outdated/stuck) Status: Test needed Issue needs testing

Comments

@hallard
Copy link
Contributor

hallard commented Apr 20, 2021

Hardware:

Board: ESP32

Description:

Unable to light off PWM common anode RGB Led when setting value to latest value of range.

Old known issue of ESP8266 #5957 and now seen is Tasmota and my sample test program

The fix is described in comments of official sample code

But from logical working, having value going from 0 to 255 should means that 255 is full scale so always 1 and not having a little pulse. This does now works on ESP8266 with analogWrite(255) and comments have been updated to code according in this PR

// Per the Arduino docs at https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
// val: the duty cycle: between 0 (always off) and 255 (always on).
// So if val = 0 we have digitalWrite(LOW), if we have val==range we have digitalWrite(HIGH)

I know it's not arduino but I think if PWM could work as is 0 (always off) and max range value (always on) would be less confusing

my ledOff()sample function with 256 does works, but should works with 255, much easier if we use an uint8_t as scale range.

void ledOff() {
  ledcWrite(LED_RED_CHAN, 256);
  ledcWrite(LED_GRN_CHAN, 256);
  ledcWrite(LED_BLU_CHAN, 256);
}
@hallard hallard changed the title PWM ledCWrite(255) or ledcWrite(1023) is not 100% 1 PWM ledCWrite(255) or ledcWrite(1023) is not 100% on Apr 20, 2021
@lbernstone
Copy link
Contributor

lbernstone commented Apr 20, 2021

esp32 does not have a fixed bit width like atmel and esp8266 boards, so max is pow(2,bit_res). You have not included your ledcSetup call, so your issue is not clear.

@hallard
Copy link
Contributor Author

hallard commented Apr 20, 2021

Yeah sorry about that, what I mean is that the bug is there if I call ledcWrite() with the max value of resolution

  ledcAttachPin(LED_RED_PIN, LED_RED_CHAN); // assign RGB led pins to channels
  ledcAttachPin(LED_GRN_PIN, LED_GRN_CHAN);
  ledcAttachPin(LED_BLU_PIN, LED_BLU_CHAN);
  
  // Initialize channels 
  ledcSetup(LED_RED_CHAN, 12000, 8); // 12 kHz PWM, 8-bit resolution
  ledcSetup(LED_GRN_CHAN, 12000, 8);
  ledcSetup(LED_BLU_CHAN, 12000, 8);

So 8 bits max value 255 does not light off the Led, 10bits max value 1023 does not light off led, I need to set to max value + 1 to get it working.
So in case of uint8_t (example) moving to all scope from 0 to 255 led will never go off.
In case of 10bits, same things set all bits to 1 (0x3FF) never light off the led

@me-no-dev
Copy link
Member

255 is max light and 0 is off

@hallard
Copy link
Contributor Author

hallard commented Apr 22, 2021

yeah that is the issue (255) is not full 100% HIGH level, if you connect the LED as follow (3V3 not 5V of course)

image

You won't be able to light off the LED because 255 is not fully 100%

Detailled explanation, same issue than this one
esp8266/Arduino#5957

Of course if you connect the LED other side

image

your eye is unable to see light difference to full light and missing 1/255 pulse.

@stale
Copy link

stale bot commented Jun 22, 2021

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Jun 22, 2021
@stale
Copy link

stale bot commented Jul 8, 2021

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck) Status: Test needed Issue needs testing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants