====== Pulse-width Modulation ====== {{ :files:img_warning-line_b.png?nolink |}} {{:en:hw:08_safety-instructions_pseudo-ISO-7010-info.png?nolink |(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. /*
PWM \\ PWM abbreviation stands for Pulse Width Modulation. This function serves for generating a rectangular signal of a given frequency and duty cycle on digital output and is used for digital transmission of an analog signal (such as 0-100%). PWM is typically used to regulate components powered by direct current in a way not possible with analog regulation. For example, the function can serve as an effective regulation of heating cartridges and LED lighting through an SSR relay. */ /* Něco o tom jak je PWM na PLC a rychle výčet (tři věci) */ 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. /* size of registers: 16 bits (max values: 65535) max. frequency: 200 kHz */ 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 ===== {{ :files:img_warning-line_b.png?nolink |}} {{ :en:hw:09_safety-instructions_pseudo-ISO-7010-attention.png?nolink |(!)}} **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: [[en:sw:01-mervis:pwm-calculations-hidden]] 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. {{ .:pwm:pwm_00-pwm-device-settings.png?direct |}} {{ :files:img_warning-line_b.png?nolink |}} {{ :en:hw:09_safety-instructions_pseudo-ISO-7010-attention.png?nolink |(!)}} **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 [[en:sw:01-mervis:unipi-configure-module-hidden|Unipi - Configure Module]] tutorial. /* The change has been done on the prototype of the device, which means, it is a change in the PLC module's configuration. To upload the configuration, check the [[en:sw:01-mervis:unipi-configure-module-hidden|Configuration of the Unipi 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. {{ .:pwm:pwm_01-set-initial-value.png?direct |}} 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 {{:files:dialog-confirm_button_2.png?nolink|}} 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. {{ .:pwm:pwm_02-osciloscope-output.gif?direct |}} And we can see the frequency of the wave is 4 kHz with 50 % duty cycle.