Turning Raspberry Pi into a PLC - Blinking LEDs example
This tutorial will show you how to include Raspberry Pi in your hobby or DIY project in the role of a standalone central control unit. In fact, you will turn your Raspberry Pi into a Programmable Logic Controller (PLC), which is how such control units are called in the industry.
At the end, you will be able to manipulate Raspberry Pi GPIO pins remotely, without writing a single line of code. And since the REX Control System is device-independent, you will also know how to program a real industrial PLC, because the workflow and tools are completely the same.
Workflow and tools
A typical PLC is programmed using a standard desktop computer running the development environment. The PLC itself is running the so-called runtime core, which cyclically executes the program, which has been copied to it (therefore the PLC is also referred to as a target device, because it is the target in the copy operation).
In the REX Control System, the main development tools are the RexDraw and RexView programs and the runtime core is called RexCore.
It is necessary to have a correctly configured Internet connection on your Raspberry Pi running the so-called Raspbian distribution of GNU/Linux.
Installation of runtime modules on your Raspberry Pi
- Login to your Raspberry Pi (default user is pi, password raspberry).
- Install GIT
sudo apt-get install git
- Download the latest revision of installation scripts
git clone https://github.com/rexcontrols/rex-install-rpi.git
- Change the working directory
- Run the following installation script
sudo bash install-rex.shNote: If you are using add-on boards like UniPi or PiFace Digital, use the corresponding installation scripts from the repository. Find the instructions directly in our installation repository on GitHub.
- The script is made for a clean Raspbian. Confirm by y and wait for the installation to finish.
Installation of the development tools
- Download the installation package of the development tools for Windows. The installation process requires the administrator rights on your PC.
- Run the downloaded executable file.
- Select the language and follow the installation wizard.
- The disk space usage is negligible therefore Full install option is recommended.
- There is no need to restart the system after installation.
The blinking LEDs example
The workflow and tools of the REX Control System will be demonstrated on a 'Hello world' example of the GPIO world - blinking LEDs.
- Connect 3 LEDs to your Raspberry Pi as shown in the diagram below. For the resistors, you can use anything from 270Ω to 330Ω.
- Download the project files from GitHub. You can download only the blink_exec.mdl and blink_task.mdl files, however, it is strongly recommended to download the whole repository of examples in the form of a standard .zip file. The files are located in the RaspberryPi_examples\01_Blink folder. If you are familiar with Git and GitHub server, you will probably use the direct link to the REX Control System examples repository.
- Open the blink_exec.mdl file in RexDraw. The blink_task.mdl is opened automatically. These two files are the source files of the project.
The algorithm is created from the so-called function blocks. In this simple example only the CNB (Boolean constant) and BIS (Binary signal generator) function blocks are used.
- From the menu, select Compiler->Compile and download.
- In the dialog which appears, enter the IP address of your Raspberry Pi and click Download. Do not enter any login or password.
- If you are running the 2-hour demoversion, confirm the evaluation use.
- Congratulations, your first project has been successfully completed! The algorithm is running in your Raspberry Pi and you should see the green LED illuminated, the yellow LED should blink in 1 second intervals and the red LED should be doing a triple blink each 2 seconds.
- In the dialog which appeared, you may choose to watch the algorithm, in other words, switch to Online mode.
- The project background will turn gray and you will be able to observe the algorithm in action. But first, you have to left-click the CNB_GREEN block and choose Target->Monitor selection from the menu.
- A small on will appear at the block output. This signal controls the GPIO#23 so that's why the green LED is illuminated. Now double click the block and change the YCN parameter to off (un-tick the checkbox) and click OK. The output of the block will turn to off and the green LED will be switched off.
- You may now disconnect from the Raspberry Pi (Target->Disconnect). The blinking algorithm will keep running.
- Do not transfer the change in the CNB_GREEN constant to the project source drawing at this moment. Click No.
- Now you are experienced in compiling, downloading and monitoring control algorithms in the REX Control System. The next tutorial will add a push button and explain the algorithm and project setup in more detail.
Got stuck at any point? We want to hear about it, do let us know.
How can I use this in real world?
By manipulating GPIO pins you can control relays, which can switch lights, pumps, ventilators, heaters, refrigerators, etc. Read the sequel to this tutorial and follow us, a tutorial on pool automation will be available soon.
You can also:
- Read the Getting started guide and watch our Getting started video which both explain a PID control loop example.
- Explore the rich function block library of the REX Control System.
- Visit the Raspberry Pi Foundation webpage, where you can find more information about the Raspberry Pi minicomputer.
Raspberry Pi is a trademark of the Raspberry Pi Foundation.