Pulse-width Modulation

(i)

This function works only for following Inputs & Outputs:

  • digital outputs

The PWM works only on digital outputs, that means it can only output 0 V⎓ or 24 V⎓. To achieve the regulation, the PWM generates a square signal of given frequency. That means it will periodically set the output ON and OFF. The parameter of the regulation is the duty cycle - a proportion of the ON time and OFF time within one period. When the duty cycle is represented in percents and the value is 50 %, the output signal will be 50 % of the period ON and the rest will be OFF. If the duty cycle is 66 %, it will be 66 % of the cycle ON and 34 % OFF.

Let's take a 24 V⎓ motor for example. To achieve 100 % RPMs, you have to supply it with 24 V⎓. Logically, to achieve 10 % RPMs, you have to supply it 24/10 = 2.4 V⎓. But if you apply this voltage, the motor will not even start, because of the friction and its load. And if it starts, its torque will be very low. This is one of the downsides of analogue regulation.

The PWM can be configured with PWM Cycle and PWM Prescale. These parameters are used for all digital outputs, where you can set duty cycle on each of them individually. PWM settings registers are using 16 bit words, meaning only values 0 to 65535 can be written to them. Meaning the PWM have 16 bit resolution. Frequency of the PWM signal can be up to 200kHz.

Setting the PWM in Mervis

(!)

Important:
PWM settings are using terms PWM Cycle and PWM Prescale. If you not sure how to set them, then you can calculate their values in this tutorial: Calculating parameters for PWM

The PWM Cycle and PWM Prescale are configuration variables of the Unipi module and cannot be changed from the program. Select the Unipi module in the Left panel. In the Properties panel, you can find the PWM Cycle and PWM Prescale variables. Set those according to your calculations. We will set it to PWM Cycle = 99 and PWM Prescale = 119. This will set the frequency to 4 kHz with resolution of 101.

(!)

Important:
We just changed the configuration of the PLC modules, which is not applicable by simply uploading the solution. To save the configuration, follow the Unipi - Configure Module tutorial.

Now let's check which variables we have available for working with PWM. Select the Variable browser tab on the Main window. Under the Name column, enter the PWM_ into the search box. You should see similar list. Select …_PWM_1.01_w variable where we will enable PWM by setting initial value in the variable attributes to 50. You will need to deploy the solution and Cold restart the PLC.

Alternatively you can try it by changing the variable value directly from debug mode. Turn on the Debugging and in the column PLC Value, you will see the current PWM value of DO ports 1.01-1.04. If you didn't change it previously, you should see zeros. Change the value of …_PWM_1.01 to 50 and confirm by clicking on or submiting with ENTER. Keep in mind after Cold restart, like power outage, the value will be lost.

We can hook up the oscilloscope to the Unipi's DO 1.01 and check the waveform.

And we can see the frequency of the wave is 4 kHz with 50 % duty cycle.