Basics 04: OpenPLC on Raspberry Pi with Modbus

3 min read 4 months ago
Published on Aug 16, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you'll learn how to set up and program a Raspberry Pi using OpenPLC Editor, enabling you to control GPIO pins remotely via Modbus TCP. This guide is ideal for beginners who want to explore automation with Raspberry Pi and OpenPLC.

Step 1: Starting the Runtime

  1. Install OpenPLC if you haven't already.
  2. Launch the OpenPLC runtime on your Raspberry Pi.
    • Use the terminal and run the command:
      sudo openplc
      
  3. Verify the runtime is active by checking the status in the terminal.

Step 2: Creating a Start-Stop Circuit

  1. Understand the circuit: A start-stop circuit allows you to control a device using a momentary switch.
  2. Wiring:
    • Connect your momentary switch and load (like a light or motor) to the GPIO pins on the Raspberry Pi.
    • Use physical components like relays if necessary to control higher voltages.

Step 3: Wiring the Components

  1. Gather materials:
    • Raspberry Pi
    • Momentary switch
    • GPIO-compatible relay or load
  2. Wire the circuit:
    • Connect the GPIO pin for the switch to a ground pin.
    • Connect the other side of the switch to a GPIO pin for input.
    • Connect the relay to the output GPIO pin.

Step 4: Creating a New Project in OpenPLC

  1. Open OpenPLC Editor on your computer.
  2. Create a new project:
    • Go to File > New Project.
    • Name your project and select the appropriate settings for Raspberry Pi.
  3. Design the logic:
    • Use the graphical interface to create your logic for the start-stop circuit.
    • Ensure you define your input and output properly.

Step 5: Uploading to Raspberry Pi

  1. Check the connection:
    • Ensure your Raspberry Pi is connected to the same network as your computer.
  2. Upload the project:
    • In OpenPLC Editor, go to File > Upload Project.
    • Input the Raspberry Pi's IP address and initiate the upload.
  3. Monitor the upload process for any errors.

Step 6: Testing the Setup

  1. Access the Raspberry Pi via SSH or directly.
  2. Check the OpenPLC status:
    • Run the command:
      sudo systemctl status openplc
      
  3. Test the circuit:
    • Press the momentary switch and observe if the load activates as expected.

Step 7: Remote Control Over Modbus

  1. Configure Modbus TCP settings:
    • Set up your Modbus parameters in the OpenPLC Editor.
    • Define the registers that correspond to your GPIO pins.
  2. Test remote control:
    • Use a Modbus client software to connect to the Raspberry Pi.
    • Send commands to control the GPIO pins and verify the operation.

Conclusion

You have successfully set up a Raspberry Pi to control GPIO pins using OpenPLC and Modbus TCP. By following these steps, you can create a basic automation control system. Next, consider exploring more complex projects or expanding your setup with additional sensors and outputs. For further resources, visit the OpenPLC project website or consider supporting OpenPLC on their Patreon.