Raspberry Pi 5 Relay Controller 16-Channel
This project combines a simple 16-channel relay switch with a raspberry pi model 5 to make a network connected relay switch at a significant savings over commercial offerings.
This project provides a library you can use in your Python applications plus a Python web application (written using Flask) to interact with the board from a web browser.
The board looks something like the following (image from amazon.com):
To use this project, you'll need at a minimum the following hardware components:
- Raspberry Pi
- ModMyPi PiOT Relay Board
- 5V, 2.5A Micro USB power source (basically, a smartphone charger)
Attach the VCC and GND pins of the board to the respective pins on the GPIO header of the raspberry pi. The remaining 8 pins control the relays, and are attached to the GPIO pins on the raspberry pi. Consult the pinout diagram of the version of the raspberry pi you're using for the correct numbering. I'm using a raspberry pi Model 5. The pinout diagram of this version is here pinout
Download the latest version of the Raspbian OS from the Raspberry Pi web site and follow the instructions for writing the OS image to a Micro SD card for the Pi. Insert the SD card in the Pi, connect Ethernet, keyboard, mouse, and a monitor to the Pi and finally power it up using a smartphone charger or some suitable power source.
Raspbian comes configured with its keyboard, timezone, and other locale settings configured for the United Kingdom (UK), so if you're in the US, or elsewhere that's not the UK, you'll want to switch over to the localisation tab and adjust the settings there as well.
When the Pi comes back up, open a terminal window and execute the following command:
sudo apt-get update
This updates the local catalog of applications. Next, execute the following command:
sudo apt-get upgrade
This command will update the Raspbian OS with all updates released after the latest image was published. The update process will take a long time, so pay attention, answer any prompts, and expect this process to take a few minutes or more (the last time I did this, it took about 15 minutes or more to complete).
The controller's Flask application uses Flask and the Flask Bootstrap plugin to serve Bootstrap applications, so in the terminal window, install the plugin by executing the following command:
sudo apt install python3-full
sudo pip install flask flask_bootstrap
sudo apt-get install gpiod
sudo apt install python3-venv
python3 -m venv my_venv
source my_venv/bin/activate
pip install flask flask_bootstrap
sudo apt install python3-pip
sudo rm /usr/lib/python3.*/EXTERNALLY-MANAGED
RUN python3 -m venv /opt/venv
sudo apt install python3-lzo
sudo apt install liblzo2-dev
sudo apt install -y gpiod libgpiod-dev python3-libgpiod
sudo apt install python3-lgpio
Finally, clone the controller application to your local system by executing the following commands:
git clone https://github.com/baalwy/pi5-relay-controller-16-channel
cd pi5-relay-controller-16-channel
Since the relay's GPIO port assignments can be easily changed using the buttons on the board, before you can run the project, you must make one change to the project's server code. Open the project's server.py file using your editor of choice. Near the top of the file, you should see the following lines of code:
# Update the following list/tuple with the port numbers assigned to your relay board
PORTS = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
This ports list refers to the GPIO port configuration for the board referenced in the pinout diagram. Change the values here based on your board's configuration. I don't know what the default configuration is.
Open a terminal window and execute the following commands:
cd pi5-relay-controller-16-channel
ls
You should see the project's files listed. The project includes a script file to automate launching the server process. To use this file, you'll first have to make the file executable. In the terminal window, execute the following command:
chmod +x start-server.sh
Once that's done, execute the script file using the following command:
./start-server.sh
The server process will launch and update the terminal window as shown in the following figure:
If you open the web browser on the Pi and point it to http://localhost:5000 you should see the web application load as shown in the following figure:
Click any of the buttons to interact with the relay board. Alternatively, turn on/off or toggle a channel with a GET request to
- Channel 3 on: http://relay-host/on/3
- Channel 5 off: http://relay-host/off/5
- Channel 7 toggle: http://relay-host/toggle/7
To make the server process start every time your boot the Raspberry Pi, you'll need to open the pi user's session autostart file using the following command:
sudo nano ~/.config/lxsession/LXDE-pi/autostart
Add the following lines to the end (bottom) of the file:
@lxterminal -e /home/pi/pi5-relay-controller-16-channel/start-server.sh
To save your changes, press ctrl-o then press the Enter key. Next, press ctrl-x to exit the nano application.
Reboot the Raspberry Pi; when it restarts, the python server process should execute in its own terminal window automatically.
or by Step 1: Create a Service File
Open a terminal and create a new service file:
sudo nano /etc/systemd/system/pi-relay.service
add
[Unit] Description=Pi Relay Controller Service After=network.target
[Service] ExecStart=/bin/bash /home/pi/pi-relay-controller-modmypi/start-server.sh WorkingDirectory=/home/pi/pi-relay-controller-modmypi StandardOutput=inherit StandardError=inherit Restart=always User=pi
[Install] WantedBy=multi-user.target
Save and close the file: Save the service file in nano by pressing CTRL + O and then CTRL + X to exit.
Reload the systemd daemon: After creating or editing the service file, you need to reload the systemd daemon so it recognizes the new service:
bash
sudo systemctl daemon-reload
Enable the service to run on boot: To make sure the service starts automatically on boot, enable it:
bash
sudo systemctl enable pi-relay.service
Start the service: To start the service immediately, use:
bash
sudo systemctl start pi-relay.service
Check the service status: You can check the status of your service to ensure it's running correctly:
bash
sudo systemctl status pi-relay.service
This command will show you the current status and any error messages if something went wrong.
Verifying the Service:
After completing these steps:
Your start-server.sh script will automatically start in the background on boot.
You can manage the service with the following commands:
Start the service: sudo systemctl start pi-relay.service
Stop the service: sudo systemctl stop pi-relay.service
Restart the service: sudo systemctl restart pi-relay.service
Check service status: sudo systemctl status pi-relay.service
This setup will ensure your script runs as a background service every time the Raspberry Pi starts.



