Introduction: 4CH Relay-board Controlled With Push Buttons

My goal is to upgrade my Anet A8 3D-printer by adding a power supply control via the Octoprint interface. Nevertheless, I also want to be able to start "manually" my 3D-printer, meaning not using the web interface but just pressing a button.

Step 1: The Wiring

The AC-DC PSU is connected with a Smart-plug (TP-Link) which is controllable by means of a smartphone (App Kasa) or a button.

This PSU power the Raspberry Pi 3 B+ by means of a LM2596 DC-DC Buck Converter Module (12V - 5V). The 4-Relay Module 5V with Optocoupler Low-Level-Trigger is directly connected to the RPI 3B+ (no need of 3.3V tuning).

4 Push buttons are connected as "pull-up resistor" to the RPI 3B+.

For the wiring, just have a look to the sketch.

Step 2: The Software

For the control by means of push-buttons, I've used Python to write the program, inspiriting me by reading the following thread:

To insure the interaction with the control via Octoprint, there is a test to check the current status of the relay and change it.

The script is attached.

Comments: As the Relay 1 controls the motherboard, I wanted to add a security by switching off with the push-button. The goal was to maintain the button pressed during 5 seconds to confirm the switch-off. Unfortunately, the script works but not reliably due to rebound effect. If you have a correction to suggest feel free to share it.

To make the script executable: sudo chmod 777 /home/pi/script/
To test the script: ./

To copy the script: sudo cp /usr/local/bin

To run automatically the script at the start:

sudo nano /etc/rc.local

add the path to the script "/home/pi/scripts/ &" between fi and exit 0

For the control by means of Octoprint, it's well documented on the web.

There is two steps:

1- Edit the /etc/init.d/octoprint file by adding in the block "do_start()" just after RETVAL="$?:

gpio export 6 out

gpio -g write 6 1

gpio export 13 out

gpio -g write 13 1

gpio export 19 out

gpio -g write 19 1

gpio export 26 out

gpio -g write 26 1

2- Edit the /home/pi/.octoprint/config.yaml file by adding the block "System":


- action: Printer_ON

command: gpio -g write 6 0

confirm: false

name: Printer_ON

- action: Printer_OFF

command: gpio -g write 6 1

confirm: You are about to turn_OFF the printer.

name: Printer_OFF

- action: LED-String_ON

command: gpio -g write 13 0

confirm: false

name: LED-String_ON

- action: LED-String_OFF

command: gpio -g write 13 1

confirm: false

name: LED-String_OFF

- action: LED-Cam_ON

command: gpio -g write 19 0

confirm: false

name: LED-Cam_ON

- action: LED-Cam_OFF

command: gpio -g write 19 1

confirm: false

name: LED-Cam_OFF

- action: Relay-4_ON

command: gpio -g write 26 0

confirm: false

name: Relay-4_ON

- action: Relay-4_OFF

command: gpio -g write 26 1

confirm: false

name: Relay-4_OFF

Step 3: The Test

It works!

The behavior of the push-buttons is a bit tricky but after a few trials you get it.

Step 4: Finalize the Concept

Now I will solder the push-buttons on a perfboard and add a 5 pins connector.

Finally, I have designed and print 2 cases:

- one for the RPI 3 B+ and the relay-board

- one to cover the wiring at the base of the PSU and to fix the LM2596 DC-DC Buck Converter Module.

You can find the *.stl and *.gcode files on