In compliance with high industry standards

Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

How to work with PWM outputs on industrial Raspberry PLC

Raspberry Pi industrial PLC family

  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 >>


 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 introducing 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 behaviour. 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


Odoo image and text block

Have you got questions?

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

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


Contact Us Now >> 


Looking for your ideal PLC?

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

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


Industrial PLC controller comparison >>

Do you want more information?

Just fill the form!

Tell me more!