khoih-prog/ESP8266_PWM

Change Duty Cycle

AnselPeng2019 opened this issue · 7 comments

when I use this lib, after setup the PWM paras, the PWM works well. But I need to change the duty cycle in working, for example, the breathing led.

I have tried to use disable or delete function, but it looks not work, the PWM duty cycle do not change.

So, it would be great if there is a duty cycle set up function.

Hi,

I just add an example ISR_Changing_PWM to show you how to change PWM freq and duty-cycle on-the-fly.

Anyway, I'll add option to make it easier to change the PWM freqs and duty-cycles, without having to delete and reinit the PWM channels in next release.

Hi @AnselPeng2019

The new ESP8266_PWM releases v1.1.0 has just been published

Your contribution has been noted in Contributions and Thanks

Check the new

  1. ISR_Changing_PWM example
  2. ISR_Modify_PWM example

The library is definitely better and more useful now. Looking forward to receiving more enhancement requests from you.

Best Regards,

KH


Changelog

Releases v1.1.0

  1. Add functions to modify PWM settings on-the-fly
  2. Fix bug
  3. Add example to demo how to modify PWM settings on-the-fly

I'm similarly trying to change the duty cycle with a fixed frequency. Since PWM_DutyCycle is a uint32_t , this can only be done in 100 steps. I suggest to have an alternative function where you can specify Period/OnTime to be able to get the full duty cycle resolution. Or change PWM_DutyCycle to a float (I think 0. to 1. would be easier to use than 0-100 if you do this).

@ggmichael

Good idea. I'll rewrite the library to use float / double for PWM freqs, period and duty-cycles to have much better accuracy.

Regards,

Hi @ggmichael

The new ESP8266_PWM releases v1.2.0 has just been published. Your contribution is noted in Contributions and Thanks

Best Regards,


Releases v1.2.0

  1. Fix multiple-definitions linker error. Drop src_cpp and src_h directories
  2. Add feature to select among highest, medium or lowest accuracy for Timers for shortest, medium or longest time
  3. Fix reattachInterrupt() bug. Check bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19
  4. Add example multiFileProject to demo for multiple-file project
  5. Improve accuracy by using double, instead of uint32_t for dutycycle, period. Check Change Duty Cycle #1
  6. Update examples accordingly

Thanks for this! Your library has fixed a problem I was having with inconsistent calibration, which I now think arose from the changing timing of the analogWrite pwm.

One more feature that would be nice would be to be able to command a duty cycle update to occur at the end of the next full period instead of immediately (as I think happens now). If you're trying to create a continuously variable mean output voltage, resetting the channel at a frequency that approaches the PWM frequency causes a bias towards whichever part of the duty cycle comes first. This wouldn't happen if the update occurred at the end of the cycle. It may be that there are other applications (long cycles) where you would prefer the update straight away. So perhaps a flag to set either mode.

Hi @ggmichael

One more feature that would be nice would be to be able to command a duty cycle update to occur at the end of the next full period instead of immediately (as I think happens now)

It's nice-to-have and a little bit harder, but totally possible to address this issue, if there are many more users requesting the similar feature.

Can you start by creating a new enhancement request / issue, and also specify the use-case and how it'll be beneficial to have the new feature.

IMO, a short glitch can't affect anything (such as light, motor control, etc.). I also don't think analogWrite pwm has taken care of this issue.

Thanks,