How to work with PWM outputs on industrial Raspberry PLC

Raspberry Pi industrial PLC family
February 24, 2021 by
How to work with PWM outputs on industrial Raspberry PLC
Boot & Work Corp. S.L., Quesada Dani Salvans

Introduction

The devices of the Raspberry Pi based PLC family have a defined number of digital outputs. All of them can be programmed as PWM outputs, if required. As we know, the PWM (Pulse Width Modulation) is a kind of voltage signal used to send information or to modify the amount of power sent for each charge. So, in this blog, you are going to see how to configure PWM outputs. 


Requirements

The key points you should consider are the following: 

  • Industrial Raspberry Pi PLC family

  • PLC access: shh.  A tutorial on how to access the device via Linux or Windows can be found in the Raspberry Pi PLC controller User Guide 


Related links 

Basics about digital outputs of a Raspberry Pi automation PLC
Read >>

Basics about Raspberry Pi PLC analog outputs
Read >>

How to find your perfect industrial PLC
Read >>

How to program open source PLC Raspberry Pi interrupt inputs with Python

Read >>

Raspberry PLC family products
Read >>


RPI PLC Version 3: Explanation and Usage of Bash Script

First of all, the Bash Script that we have to execute to manage the PWM outputs is the one called "set-analog-output" located in the path "/home/pi/test/analog/". You have to make sure that the output that you want to configure as PWM is not set as analog or digital so, to ensure that, you can execute the "stop" function (to stop it from being analog or digital output: 

./set-analog-output A0.5 stop

Or :

./set-digital-output Q0.5 stop


To execute the script, you have to call the "set-analog-output" script but introduce as parameters a digital output and the width of the pulse. The width of the pulse is the high time period of the duty cycle and it has a range from 0 to 4095 (12 bits). For example, if you want a high time period of 25%, you must put 1024 and, if you want a high time period of 100%, you must put 4095. 

./set-analog-output Q0.5 4095


Note: See the User's Guide to know which outputs are PWM supported. 

The PWM parameters of the script do not have to be modified to ensure correct PWM behavior. Here we can see the script: 

#!/bin/bash
# PWM period in nanoseconds PERIOD="2000000" case ${1} in A0.5) ADDR=40; INDEX=10 ;; A0.6) ADDR=40; INDEX=1 ;; A0.7) ADDR=40; INDEX=0 ;; A1.5) ADDR=40; INDEX=3 ;; A1.6) ADDR=40; INDEX=5 ;; A1.7) ADDR=40; INDEX=8 ;; A2.5) ADDR=41; INDEX=2 ;; A2.6) ADDR=41; INDEX=1 ;; A2.7) ADDR=41; INDEX=0 ;; Q0.0) ADDR=40; INDEX=15 ;; Q0.1) ADDR=40; INDEX=14 ;; Q0.2) ADDR=40; INDEX=13 ;; Q0.3) ADDR=40; INDEX=12 ;; Q0.4) ADDR=40; INDEX=11 ;; Q0.5) ADDR=40; INDEX=10 ;; Q0.6) ADDR=40; INDEX=1 ;; Q0.7) ADDR=40; INDEX=0 ;; Q1.0) ADDR=40; INDEX=2 ;; Q1.1) ADDR=40; INDEX=9 ;; Q1.2) ADDR=40; INDEX=6 ;; Q1.3) ADDR=40; INDEX=4 ;; Q1.4) ADDR=40; INDEX=7 ;; Q1.5) ADDR=40; INDEX=3 ;; Q1.6) ADDR=40; INDEX=5 ;; Q1.7) ADDR=40; INDEX=8 ;; Q2.0) ADDR=41; INDEX=6 ;; Q2.1) ADDR=41; INDEX=7 ;; Q2.2) ADDR=41; INDEX=5 ;; Q2.3) ADDR=41; INDEX=4 ;; Q2.4) ADDR=41; INDEX=3 ;; Q2.5) ADDR=41; INDEX=2 ;; Q2.6) ADDR=41; INDEX=1 ;; Q2.7) ADDR=41; INDEX=0 ;; *) echo "Output not defined" >&2 exit 1 ;; esac VALUE="${2:-50}" if [ -z "${PWM}" ]; then CHIP_BASE_DIR="/sys/bus/i2c/devices/1-00${ADDR}/pwm" CHIP_NAME="$(ls ${CHIP_BASE_DIR})" CHIP_DIR="${CHIP_BASE_DIR}/${CHIP_NAME}" CHIP="${CHIP_NAME#pwmchip}" PWM="${INDEX}" fi if [ "${VALUE}" = "stop" ]; then echo "${PWM}" > ${CHIP_DIR}/unexport exit 0 fi if [ ! -d ${CHIP_DIR}/pwm${PWM} ]; then echo "${PWM}" > ${CHIP_DIR}/export fi echo "${PERIOD}" > ${CHIP_DIR}/pwm${PWM}/period DUTY_CYCLE="$((${2} * ${PERIOD} / 4095))" echo "${DUTY_CYCLE}" > ${CHIP_DIR}/pwm${PWM}/duty_cycle


RPI PLC Version 4: Explanation and Usage of C++

The C++ function sets a PWM output to the specified value using the set-analog-output function with a digital output. All the digital outputs of the PLC can be used for the PWM function.

The main function initializes the microcontrollers with the initPins() function. Then, like in Arduino programming, it sets the output to the output mode, and it writes to the pin the specified value in the parameter.

So, in the ~/librpiplc/test directory, execute the following command to create an executable file called set-analog-output:

g++ -o set-analog-output set-analog-output.cpp -l rpiplc -I /usr/local/include/rpiplc -DRPIPLC_19R (or any other Raspberry PLC model)


Execute the compiled file named set-analog-output with two parameters:

1: the digital output to control

2: the PWM (analog) value to set (from 0 to 4095)

./set-analog-output Q0.0 4095


And see how the Q0.0 output has been given the specified resolution.

Have you got any questions?

Contact our tech-sales team and solve your doubts about programming or technical issues.

We will help you to find the best solution to automate your facilities.

Contact Us Now >> 


​Search in our Blog

How to work with PWM outputs on industrial Raspberry PLC
Boot & Work Corp. S.L., Quesada Dani Salvans February 24, 2021
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>