Software PWM

Hello Community,

we try to use the software PWM on the SCM20260D Dev board.
As soon as we use a PWM frequency below 16Hz an exception is thrown.
We need a frequency of 1 Hz for our project. The minimum frequency shown on the “pwm-controller” is 7.4109846876187E-323.

Code:
SoftPWM = PwmController.FromName(SC20260.Timer.Pwm.Software.Id);
SoftPWM.SetDesiredFrequency(15.0); <- Exception

Does anyone have a solution / idea for this problem?

Best regards,
christian

What GpioPin are you using? Your code should contain something like this

var softwarePwmController = PwmController.FromName(SC20260.Timer.Pwm.Software.Id);
var pwmPinPB3 = softwarePwmController.OpenChannel(SC20260.GpioPin.PB3);

In the original code we want to use the pwm-controller for more then one pin, so the OpenChannel method is used in a different location.

But opening the channel after setting the controller gives also an exception .

Code:
SoftPWM = PwmController.FromName(SC20260.Timer.Pwm.Software.Id);
pwmPin = SoftPWM.OpenChannel(SC20260.GpioPin.PA13);
SoftPWM.SetDesiredFrequency(15.0); <-Exception

The documentation under software PWM ‘TIP’ suggests that the PWM frequency must fall between 15.4Hz to 10KHz.

https://docs.ghielectronics.com/software/tinyclr/tutorials/pwm.html

1 Like

Can you do c# for lower than 15Hz? What duty cyles is? 1ms or higher c# can do perfectly.

If you really need to get 1Hz, set PWM to 16Hz and cascade 4 of below to get 1Hz

One cycle per second? Do you even need the pwm class? If the rate is that slow and if you can tolerate duty-cycle jitter on the scale of a handful of mS, then can’t you just use do this directly in C# code (a loop in a thread for instance that toggles the GPIO line).

The “slow” PWM is used as a puls-packet to control a triac with a maximum on-time of 1 second.
Less puls duration will lower the engergy of an heating element. With this idea we could vary the power between 0…100% every second.
This is a common way in injection molding (Hot runner technology)

Still certainly sounds like something you could do in managed code - doesn’t need the PWM native code which is designed for higher pulse rates and more accurate timing. mS accuracy is still .1% accuracy over the span of a second. Even your temp feedback readings probably aren’t that accurate.

Of course, whether you use the native support or spin your own managed code, hopefully your hardware protects against runaway ‘on’ conditions.

Usually, with PWM, the duty cycle is as important as the period.

Why 1HZ? Why not 20 HZ and vary the duty cycle? Triacs easily run at 60HZ.

Background:
The 1 second is typically started with a zero crossing of the phase. Then the duration is varied and ends again with another zero crossing. (to reduce EMI)
This is done for up to (for this Project/Test) 16 Channels.
Each channel is controlled by its own pid controller reading thermocouple values and so on.

This type of control has been practiced with great success for years (PIC Controller C++).
Now we try to implement this with C# on GHI hardware.

Example for Multi-Channel Controller

So, if the Software PWM could run at 1 Hz our “problem” would be solved.

1 Like

I’ve found a software PWM snippet from the old GHI site code share library. It’s targeting NETMF but with small change you can make it work with TinyCLR.

Download link https://21code.work/temp/591_646_snippet.zip

1 Like

Many thanks for the link :+1: