Log inRegister

Raspberry Pi as a PLC - Pool automation project

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

Disclaimer: We are not responsible for any equipment damage or failure and data loss, and make no claims about the accuracy, completeness, or adequacy of the following procedure. Remember that mains electricity can kill you, therefore consult the project with an approved electrician. Follow the tutorial at your own risk.

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

Upgrade from the DEMO version!

In the first tutorial on using the Raspberry Pi minicomputer as a PLC the handling of Raspberry Pi's GPIO pins in the REX Control System was illustrated. The subsequent tutorial described adding a pushbutton and now you are ready for your first real project. The summer season just started so pool automation is a natural choice.

Pool automation project

Let's say you want to:

  • switch the pool lights on every day at 21:00 (9:00 PM) and switch them off at 1:00 (AM)
  • run the filtration from 2:00 to 4:00 (AM)
  • run the filtration from 13:00 (1:00 PM) to 14:00 (2:00 PM)
  • have the possibility to disable the scheduled filtration if necessary

Because this is the very first project the goals are intentionally simplified. Anyways, let's see how to solve this project using the REX Control System and the Raspberry Pi minicomputer.

Switching heavy loads

You already know how to manipulate the GPIO pins from the algorithm of the REX Control System so the only question is how to switch mains AC voltage with the GPIO pins. There are many tutorials on the web dealing with this topic, but we will use the Sainsmart relay board. Since we want to switch filtration and lights, we will need the 2-relay version.

Connect the relay board with the Raspberry Pi and wire up the lights and filtration according to the following diagram. Use the NO (Normally Open) contact of the screw terminals.

Wiring diagram for pool automation using the Raspberry Pi.

 

Pool automation algorithm

You already have the development tools of the REX Control System installed on your PC and the RexCore runtime module running on your Raspberry Pi therefore we can focus on the algorithm itself.

Note: If you missed our previous tutorials, proceed with the installation as described in the Turning Raspberry Pi into a PLC article.

The algorithm for pool automation in the REX Control System can look like this:

Pool automation project in RexDraw

As usual, you can download the project files from our GitHub repository. You can download only the pool_exec.mdl and pool_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\03_PoolAutomation folder. If you are familiar with Git and GitHub server, you will probably use the direct link to the REX Control System examples repository

Scheduling of the lights and filtration is quite easy, you just use the REL block for comparing the defined switching times with the system time (the TIME block) and combine the individual conditions using the logic function blocks AND and OR. For a detailed description of each function block left-click the block in RexDraw and press F1. Notice that the relay board has inverted logic, therefore you have to negate the commanding signals. Also notice the H at the end of the output flags. This initializes the GPIO pin in output mode and the initialization value is ON (HIGH), which suppresses firing the relays at startup.

The only thing left is processing the pushbutton signal. For disabling of the filtration the RS flip-flop function block can be used. Think of it as a variable, which can be set to TRUE by the S input (Set) and set to FALSE by the R1 input (Reset). The state of the variable is available at the Q output. The state of the RS block will determine the action to perform when the pushbutton is pressed.

When the pushbutton is pressed, the EDGE block detects a rising edge. This event is combined with the state of the RS function block and the RS block is either set or reset. When the Q output of the RS block is TRUE (ON), the ISSW block passes the i2 input to the output and therefore disables the filtration. Moreover, when the filtration is disabled, the LED light will blink because the RS block also controls the ISSW_LED switch. When the Q output of the RS function block is FALSE (OFF) the filtration will run as scheduled and the LED light will indicate whether the filtration is running or not.

The last block which has not yet been mentioned is the MP block which stands for a virtual pushbutton. Changing its BSTATE parameter to ON while in online mode will be equal to pressing the physical button. This will allow you to disable/enable filtration remotely from your PC.

Running the control algorithm

Now you can compile the project and download it to your Raspberry Pi. You already know how to do that, don't you? If not, read the very first tutorial in the Turning Raspberry Pi into a PLC series.

How can I continue in the project?

The possibilities to expand your project are endless. Do you need to engage the filtration more than 2 times a day? Do you want to control the lights also manually? Simply add a few function blocks and you are done. Do you want to measure the water temperature in the pool? That's also possible...

The next tutorial will be focused on creating HMI screen (Human Machine Interface) for the REX control algorithm so you will be able to control your pool with your tablet or smartphone via your home wifi network.

Troubleshooting

Got stuck at any point? We want to hear about it, do let us know.

Additional information

You can also:

 


Raspberry Pi is a trademark of the Raspberry Pi Foundation.