Turning Raspberry Pi into a PLC - Adding a pushbutton

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!

In the first tutorial on using the Raspberry Pi as a PLC the process of compiling, downloading and monitoring the control algorithm using the tools of the REX Control System was illustrated.

This tutorial will follow with modification of the hardware setup and control algorithm to include a simple pushbutton. This pushbutton will control the blinking of the red LED. The red LED will be blinking only when the button is pressed, otherwise it will be in its default state.

  1. The hardware part of the modification is very easy, you just need to wire up the pushbutton and a protective resistor (R=330Ω) between GND and GPIO#22.

    Breadboard scheme with the pushbutton

  2. The software part also needs a minor modification to read the state of the pushbutton. Start with the project files from the previous tutorial and open them in RexDraw. Opening the blink_exec.mdl file will do the trick, the blink_task.mdl will be opened automatically.
  3. Save the exec file into a separate folder buttonblink and name it buttonblink_exec.mdl.
  4. Save the task file into the same folder and name it buttonblink_task.mdl.
  5. In the exec file, double-click the block named blink_task and rename it to buttonblink_task.mdl. By that you are linking the executive configuration with the task.
    Renaming the task in the executive configuration
  6. For reading the pushbutton in the algorithm, open the Block library (View->Block Library) and find the block named From.The function block named From
  7. Now drag&drop it into the task file.
  8. In the task file, double-click the From block and enter RPI__GPIO22U into the Goto tag parameter. By that, you are telling the Raspberry Pi driver to read data from the GPIO#22 pin. The trailing U enables the internal pull-up resistor of the pin to avoid floating of the pin when the button is not pressed.
    Block properties dialog
  9. Switch to Colors tab and select the background color of the From block. It is a good practice to mark the physical inputs and outputs by color.
  10. Your project should now look like the one below.Project files
  11. Now delete the line between the BIS_BLINK_RED block and the output flag of GPIO pin 25 by left-clicking it and hitting the Delete button on your keyboard.
  12. We will be switching between the default and blinking state of the red LED, therefore we add the ISSW block from the library. This block implements a simple switch for integer and Boolean signals.
  13. We will also need to define the default state of the red LED, therefore we add a Boolean constant (the CNB block) and rename  it to CNB_RED_DEFAULT. Also uncheck the YCN checkbox, the red LED will be off by default.
  14. The signal from the button will be switching the signal sent to the GPIO#25, therefore connect the input flag to the SW input of the ISSW block.
  15. Also connect the n output of the ISSW block to the flag for output GPIO#25.
  16. When the button is pressed, the GPIO pin is held LOW and the input value is 0. When released, the pin is held HIGH and the input is 1. Therefore connect the Y output of the BIS block to the i1 input of the ISSW block (active when SW=0) and the CNB_RED_DEFAULT block to the i2 input (active when SW=1).Project files - final state
  17. The last modification is changing the blinking pattern of the red LED. Doble-click the BIS_BLINK_RED block and change the parameters: t2=0.1, t3=0.2 and t4=0. By that we define a binary signal with 5 Hz frequency.Block properties dialog - the BIS block
  18. Now you can compile the project and download it to your Raspberry Pi.
  19. You will probably want to watch the algorithm, namely the ISSW block.
  20. The green LED should be glowing, the yellow LED should blink each 2 seconds and the red LED should be inactive upon the download.
  21. As soon as you press the pushbutton, the ISSW block switches the signals and the red LED should start to blink with frequency of 5 Hz.
  22. Now you can play a bit with the parameters of individual blocks to achieve e.g. blinking patterns of your own. You can change the parameters in online mode or you can change them in the source files of the project and compile&download the algorithm to Raspberry Pi again. Remember that the default parameters which will be used after Raspberry Pi reboot are the ones which were used during compilation of the project.
  23. When changing the blinking patterns, do not forget to take into account the sampling period of the algorithm, which is 20 ms in this example (this is defined by the tick and ntick0 parameters of the EXEC block). Unless you change the EXEC configuration, you cannot generate pulses shorter than 20ms.

What is this good for in real world?

By manipulating GPIO pins you can control relays, which can switch lights, pumps, ventilators, heaters, refrigerators, etc. Reading pushbuttons allows you to create simple user interfaces. Follow our tutorial on pool automation to see how you can benefit from what you have learned.

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.