Turning Raspberry Pi into a PLC - Blinking LEDs example

This is a tutorial for an outdated version of the REX Control System. Please follow the up-to-date Getting started guides listed below:

What is the REX Control System?
I want to know!

Upgrade from the DEMO version!

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.

Basic topology of the REX Control System

Prerequisities

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

  1. Login to your Raspberry Pi (default user is pi, password raspberry).
  2. Install GIT
    sudo apt-get install git
  3. Download the latest revision of installation scripts
    git clone https://github.com/rexcontrols/rex-install-rpi.git

    Cloning installation scripts
  4. Change the working directory
    cd rex-install-rpi
  5. Run the following installation script
    sudo bash install-rex.sh
    Note: 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.
    Running installation script for REX on Raspberry Pi
  6. The script is made for a clean Raspbian. Confirm by y and wait for the installation to finish.

Installation of the development tools

  1. Download the installation package of the development tools for Windows. The installation process requires the administrator rights on your PC.
  2. Run the downloaded executable file.
  3. Select the language and follow the installation wizard.
  4. The disk space usage is negligible therefore Full install option is recommended.
  5. 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.

  1. Connect 3 LEDs to your Raspberry Pi as shown in the diagram below. For the resistors, you can use anything from 270Ω to 330Ω.
    Breadboard scheme for the blinking LEDs example
  2. 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.
  3. 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.
    Blinking LEDs example project in RexDraw
    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.
  4. From the menu, select Compiler->Compile and download.
  5. In the dialog which appears, enter the IP address of your Raspberry Pi and click Download. Do not enter any login or password.
    Connection to target device
  6. If you are running the 2-hour demoversion, confirm the evaluation use.
    Confirmation of 2-hour demo mode
  7. 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.
  8. In the dialog which appeared, you may choose to watch the algorithm, in other words, switch to Online mode.
    Watching the algorithm in the target device
  9. 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.
    Online mode of the RexDraw development environment
  10. 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.
    Changing the constant in online mode
  11. You may now disconnect from the Raspberry Pi (Target->Disconnect). The blinking algorithm will keep running.
  12. Do not transfer the change in the CNB_GREEN constant to the project source drawing at this moment. Click No.
    List of parameter changes in RexDraw
  13. 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.

Troubleshooting

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.

Additional information

You can also:

 


Raspberry Pi is a trademark of the Raspberry Pi Foundation.