I was questioning how software PWM was implemented in Klipper.
It is pretty straightforward.
Klipper uses cycle time to determine how often PWM is evaluated.
So, by default, it has a cycle time of 0.1s.
Ten times in a second, a digital pin has been enabled, and the timer will be set to a percentage of 0.1s, the same percentage as with PWM.
So, as an example with 70% PWM, the timer will be set to disable in 0.07s.
Real duty cycle.
In recent years, main-powered heating beds become common in the 3D printing community, and SSR as the switch control unit for them is also widespread.
I will talk about Zero Crossing SSR here, the other 2 types are ignored here (SVR with its own duty cycle and stupid SSR, which acts like switches).
Straight to the topic.
I saw something like this and used it myself because it feels right.
pwm_cycle_time: 0.02 # 50hz for european AC, to avoid flickering lights.
Basically, there is an attempt to synchronize SSR switching with the main power frequency.
Zero Crossing SSR is trying to be smart and only switch power on zero input voltage.
So, at 0(360) and 180 degrees, 2 times per cycle.
With the above config that leads to the next situation.
Klipper schedules PWM with 30% duty cycle, with 0.02 cycle time, for 0.3s duration. Actual on-time is 0.006s, per cycle.
SSR receives signal and opens after 0.006s MCU tries to disable it.
SSR waits for another 0.004s and closes.
In fact, we have 50% duty cycle as long as PWM is below 50%, and 100% duty cycle, as long as PWM > 50%.
As a matter of fact, PWM does not work here with those settings.
I’m not a professional electrician, so I can not suggest correct values here.
As I’m aware everything except Asynchronous AC Induction Motors does not care about the quality of electricity and YMMV.
But from the control point default 0.1s or slightly more like 0.2s will work better.
Because:
0.1 provides 1/10 * 50 = 5Hz so 10 zero crossings.
0.2 provides 1/5 * 50 = 10Hz so 20 zero crossings.
Which with PWM duty cycle can represent PWM with AC.
Who am I to not show nice graphs? Everyone likes graphs
I preheated my large 400x400 bed with a 1000W heater and chose a high enough temp to allow PWM to catch some average point here.
So, now my personal thoughts here, with a less serious tone.
For some time I just assumed cycle time is just an exact regulation.
Ike 0.1s means it is either enabled or disabled for this amount of time.
So, 0.02s was very convincing. I just can’t get why PWM looks so broken and why it can not settle somewhere.
Now I know why. I will be glad if others with the same setup now know it too.
P.S.
I initially reread the PWM code 5 times because willing to understand the restrictions here , 5 times was enough to understand how PWM is actually done here.
UPD: