ChadB mentioned the LEDC peripheral on the esp32.. This looks useful.
So - a first attempt. This doesn't implement all the features (and isn't yet a component either) - it does however do a very nice PWM fade from 0 - 100% and back to 0...
There are a lot of features still to add - for example I have used the NO_WAIT option for the fade - and there is a WAIT version. It's also possible to set interrupts for end of fade - set the fade step size etc.
Martin
LEDC PWM Driver on esp32
-
- Valued Contributor
- Posts: 1179
- http://meble-kuchenne.info.pl
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 115 times
- Been thanked: 604 times
-
- Valued Contributor
- Posts: 1179
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 115 times
- Been thanked: 604 times
Re: LEDC PWM Driver on esp32
No_wait returns immediately - and I just use a delay to wait for the fade to finish. It is possible to cancel a fade - on the other hand wait - 'waits' until the fade has finished, It;s not possible to canncel in this case.. Note that 'Cancel' isn't implemented here anyways.
Also note that it is set to 13bit mode so the maximum duty is 2^13-1 (8191)
Martin
Also note that it is set to 13bit mode so the maximum duty is 2^13-1 (8191)
Martin
Re: LEDC PWM Driver on esp32
Ok, gotcha. Could that also be called blocking? Sorry I admittedly suck at software. I am a hardware guy.
I played with it, and it is working here so far. I really like that you can set the FREQ high. I am super sensitive to led flicker, and it drives me crazy.
I am going to use this to set the backlight on the led display and a nice fade in on boot.
So far so good. I'd be happy to test anything you have...
Thanks,
Chad
*edit I tried the SetDutyCycle() I used 4000 and it didn't work.
I played with it, and it is working here so far. I really like that you can set the FREQ high. I am super sensitive to led flicker, and it drives me crazy.
I am going to use this to set the backlight on the led display and a nice fade in on boot.
So far so good. I'd be happy to test anything you have...
Thanks,
Chad
*edit I tried the SetDutyCycle() I used 4000 and it didn't work.
-
- Valued Contributor
- Posts: 1179
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 115 times
- Been thanked: 604 times
Re: LEDC PWM Driver on esp32
Strange - I can do a SetDutyCycle and output looks good on the 'scope.
One thing to watch - some values aren't allowed and the esp32 will just spit out an error and restart. Open the com port using putty or similar and check if any output (probably a flashing LED on the board is a good sign something is amiss)
Can you post your test code?
Martin
One thing to watch - some values aren't allowed and the esp32 will just spit out an error and restart. Open the com port using putty or similar and check if any output (probably a flashing LED on the board is a good sign something is amiss)
Can you post your test code?
Martin
Re: LEDC PWM Driver on esp32
I just used your demo and disabled the loop, enabled SetDutyCycle(4000) and ran it. I was assuming this would just set a pwm ~50% at the set 5k rate based on what was in the (). Maybe I screwed up (likely). Remember that I am a noob. All I have done with flow code so far is debug components and chips, and spent money. Almost 4 years now.
The ramp up down loop works fine.
The ramp up down loop works fine.
-
- Valued Contributor
- Posts: 1179
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 115 times
- Been thanked: 604 times
Re: LEDC PWM Driver on esp32
If the esp32 falls off the end of a program it will constantly reset with WDT timeouts.
If you add a loop with a delay in (say 500ms) after the SetDutyCycle - then output should stay at the set level.
Martin
If you add a loop with a delay in (say 500ms) after the SetDutyCycle - then output should stay at the set level.
Martin
Re: LEDC PWM Driver on esp32
Ah ok. I'l give that a shot. I didn't realize that you had to have a null loop in there.
I also noticed that you can do basically the same thing if you just set the fade time to 0.
So it works if I put it into a loop but it doesn't work outside of the loop after the initalize.
I also noticed that you can do basically the same thing if you just set the fade time to 0.
So it works if I put it into a loop but it doesn't work outside of the loop after the initalize.
- Attachments
-
- Main1.PNG (70.24 KiB) Viewed 7356 times
-
- Valued Contributor
- Posts: 1179
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 115 times
- Been thanked: 604 times
Re: LEDC PWM Driver on esp32
You shouldn't need the SetDutyCycle inside the loop - unless you want to change the duty
4000 isn't quite 50% (~48%) - the duty cycle is a measure of the percentage 'on' time (duty/8192 * 100) - frequency is in Hz and gives the speed of the wave - if you set a low frequency, say 1Hz - then at 50% duty the LED would be on for 500ms / off for 500ms - assuming the hardware supports this - I tested this and - 4096 gives a pretty good approximation of this Obviously to drive an LED you'd use a much faster frequency - unless you want a blinkie....
Doing a slow cycle (1Hz) for 30s at 50% duty at the start...
4000 isn't quite 50% (~48%) - the duty cycle is a measure of the percentage 'on' time (duty/8192 * 100) - frequency is in Hz and gives the speed of the wave - if you set a low frequency, say 1Hz - then at 50% duty the LED would be on for 500ms / off for 500ms - assuming the hardware supports this - I tested this and - 4096 gives a pretty good approximation of this Obviously to drive an LED you'd use a much faster frequency - unless you want a blinkie....
Doing a slow cycle (1Hz) for 30s at 50% duty at the start...