PWM.png (29348 bytes)

Download package



This is a collection of soft mods for the generic I/Os of a Linux based PLC. It can be used for example with Raspberry Pi, Beagle Bone Black. The Linux Scheduler and some specific circuits are used to implement different things, which would require more complicated hardware, otherwhise.


The function block, called "SoftPWM" is a soft PWM driver that makes use of a standard digital Output Pin. The Interval is constantly changed, to achieve the precise timing which is necessary to fulfill the requirements of the specified duty cycle. It has to be used from a separate, high priority task, which drives the output. But as the interval of this task constantly changes, you should best not add anything else to this task.

A common mistake that's made is, that one uses the same FB instance in multiple tasks to generate the PWM signals. That can't work, because it would mess up the timing calculations in the FB. So best you instantiate the FB in a PRG, which you are used in exactly one task.

Known issues

The I/O driver of the Raspberry Pi seems to have a problem when it is called from multiple tasks.
In this case, the PWM signal in software is generated correctly, but the output is updated in the wrong sequence.
But, as it was the first target, which I personally used, its proven, that one task works pretty well 😉


The function block "SoftAIN" is implementing the sampling of a so called "Poor man's ADC". It just uses a small variation from the widely documented "Poor man's ADC", by using two GPIOs, instead of just one. The advantage of this approach is, that we are more flexible in the selection of I/O drivers, which can be used with that. Virtually every combination of a digital input and output can be used to sample an analog input.
We are using the digital output as the VRef. This way, it is easy to safely pull down the capacitors current, as the output is pulling down only the current, which itself loaded before.