LEDC PWM Driver on esp32

Post and discuss new components that you have created.
mnfisher
Valued Contributor
Posts: 953
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Flowcode v10 LEDC PWM Driver on esp32

Post by mnfisher »

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...
ledc_driver.fcfx
(14.65 KiB) Downloaded 71 times
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

Chadb
Posts: 63
Joined: Fri Dec 18, 2020 1:15 pm
Has thanked: 23 times

Re: LEDC PWM Driver on esp32

Post by Chadb »

Hey, this is awesome! I will give it a go. What is the difference between the NO_WAIT and Wait?

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: LEDC PWM Driver on esp32

Post by mnfisher »

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

Chadb
Posts: 63
Joined: Fri Dec 18, 2020 1:15 pm
Has thanked: 23 times

Re: LEDC PWM Driver on esp32

Post by Chadb »

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.

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: LEDC PWM Driver on esp32

Post by mnfisher »

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

Chadb
Posts: 63
Joined: Fri Dec 18, 2020 1:15 pm
Has thanked: 23 times

Re: LEDC PWM Driver on esp32

Post by Chadb »

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.
Main.PNG
Main.PNG (86.56 KiB) Viewed 4497 times

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: LEDC PWM Driver on esp32

Post by mnfisher »

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

Chadb
Posts: 63
Joined: Fri Dec 18, 2020 1:15 pm
Has thanked: 23 times

Re: LEDC PWM Driver on esp32

Post by Chadb »

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.
Attachments
Main1.PNG
Main1.PNG (70.24 KiB) Viewed 4487 times

Chadb
Posts: 63
Joined: Fri Dec 18, 2020 1:15 pm
Has thanked: 23 times

Re: LEDC PWM Driver on esp32

Post by Chadb »

The set fade will work outside a loop.

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: LEDC PWM Driver on esp32

Post by mnfisher »

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....
ledc_driver.fcfx
(14.8 KiB) Downloaded 55 times
Doing a slow cycle (1Hz) for 30s at 50% duty at the start...

Post Reply