Skip to main content

PYTHON – Let the "Monty-language” enter Automation: Part 1

These 11 lines of Python code is all you need to build a PID controlled heating system with the Revolution Pi platform:

from simple_pid import PID
import revpimodio2
import time

TempController = PID(5.0, 0.13, 10.5, setpoint=400, output_limits=(0,100))
rpi = revpimodio2.RevPiModIO(autorefresh=True)
while True:
    Temp = rpi.io.Temp10.value
    PWMvalue = TempController(Temp)
    rpi.io.PWM_heater1.value = int(PWMvalue)
    rpi.io.PWM_heater2.value = int(PWMvalue)
    time.sleep(0.05)

What are we going to control? It’s a mixed application with classical PID control and message-based IoT. I will use an analogue temperature input (with a PT100 sensor connected), and a digital output with two PWM controlled 10 W lightbulbs to simulate a heater. The current temperature is reported to an MQTT broker which provides the data for a Node-Red GUI. Set values can be adjusted using this GUI and are transmitted by MQTT to the Python PID control software. If this thrills you, then go on reading a step by step explanation of how to efficiently use Python for automation tasks.

Python is a commonly used programming language in the IT world. But although it is nearly 30 years old, it has been rarely used in the automation industry and for embedded systems. C and C++ still asserted their dominant position in 2017. But things began to change with Raspberry P, and IoT oriented systems like MicroPython. In 2018, for the second year, IEEE Spectrum’s ranking listed Python as number 1 programming language before C++ and C. But while the 2017 list for only embedded programming did not even list Python under the top ten languages, it has become the number one embedded language in 2018.

image0041_85675cad528a4372c99c8f45fc1ab5d0b2ad39a1.jpg

Source: IEEE Spectrum ranking list 2018 for embedded programming

Python (the name was chosen as a homage, not to a serpent but Monty Python) is a shoot-up star since several years now, and this reflects its enormous popularity. In this article, I don’t want to discuss the reasons, nor will I compare the advantages of using Python against C in embedded systems. If you would like such a discussion, please leave your comment as a starter.

In the automation industry, many engineers do not even use C but often use IEC61131 languages to program PLCs. I will try to whet your appetite for Python by demonstrating how it can be easily used to control industrial IOs with a Revolution Pi. Don’t miss the excellent articles of Peter Oakes and Andrew Back about the Revolution Pi; Dave Ives has shown how to use EN61131 “Structured Text” language with a Revolution Pi.

Here is the equipment I use for the demonstration:

  • Revolution Pi Core 3 (181-1142)
  • RevPi DIO (181-1143)
  • RevPi AIO (181-1146)
  • A laboratory power supply of 24 V delivering at least 1 A of current (you could also use a DIN-rail power supply like this 36 W Mean Well device: (145-7868)
  • Two 24 V 10 W automotive light bulb (e.g. (017-0913)
  • A PT100 sensor (I use a 4-wire type like (123-5602) , but you could well use a 3-wire type like (896-8395)
  • A Windows PC with an internet connection and switch with an additional network cable for the RevPi Core 3
  • (optional) an oscilloscope to check the PWM output

Please refer to Andrew’s and Peter’s articles if you are new to Revolution Pi to get a solid introduction to this Hard- and Software platform. You will learn how to connect the Core 3 module with the IO module, how to hook up the power supply, and how to use the central process image to access all IOs. On the manufacturer’s web pages, you can additionally find many written and videotaped tutorials. This is how my setup looks like:

image006_3da4f6c3bc631840ea50925283ed6b097126ba2d.jpgimage007_41380d5ea97dcdd12a057cf23cdd68b3fced28dc.jpgimage008_d762aebe3410888152275353b3c3ba7e172b2c6e.jpgimage0111_b059147a00ca2458cb86af8239255666cdae0d76.jpg

Please note two details:

  • I’ve seen many people mounting a DIN rail on a plate lying on their table. This causes the RevPi Core to be operated in a position, where the laminar airflow, which is absolutely necessary for cooling the Raspberry Comput Module is not possible. Please do not do so! Use the RevPi always upright so that enough air can flow inside into the bottom slits and out of the top slits!
  • I’ve often seen people using the RevPi Modules without connecting the FE (functional earth) terminals. This causes the complete surge and ESD protection to fail. Please do not do so! Even on your office desktop, you should at least connect the FE terminals to the 0 V terminal. Do not mix up PE (protective earth) concepts with FE concepts! Whereas PE is not needed for a RevPi module (there is no hazard of touching high voltages) the FE terminal is the only way out of the modules for any destructive energy caused by surges, bursts or ESD. Not connecting it results in not protecting your system against such destructive environmental energies.

Please refer to the diagram printed on the side of the AIO module to see how you correctly connect a 4-wire or 3-wire RTD sensor. I use the RTD1 input terminals for this setup and Out1 and Out2 to connect the lightbulbs. Please be aware that two 10 W lightbulbs will nearly draw 1 A of 24 V power. Use thick wires for the IO power supply connection and only use the outputs in high-side mode (which gives you a maximum of 500 mA per output while push-pull would only deliver 100 mA).

In a few days, we will get part 2 online in which I describe step by step how to install the libraries and tools we are going to use. Don’t miss it, subscribe to get a message when it is online.

Volker de Haas started electronics and computing with a KIM1 and machine language in the 70s. Then FORTRAN, PASCAL, BASIC, C, MUMPS. Developed complex digital circuits and analogue electronics for neuroscience labs (and his MD grade). Later: database engineering, C++, C#, industrial hard- and software developer (transport, automotive, automation). Designed and constructed the open-source PLC / IPC "Revolution Pi". Now offering advanced development and exceptional exhibits.
DesignSpark Electrical Logolinkedin