Skip to content

Controlling a relay board from your RPi

M4T edited this page Apr 18, 2018 · 34 revisions

Note: You are playing around with mains voltage here. Errors can be fatal, so if you do not know exactly (!) what you are doing, you shouldn't be doing it! There are much easier and safer ways, for example sockets which are controllable over TCP/IP or 433MHz radio!


Nearly all printers come with no power switch included. In any case getting up from your desk just to turn your printer on is a hassle, so lets make a button on the webfrontend do the work for you:

What you need:

  • Raspberry Pi with Octoprint already installed
  • a relay board (Amazon or DealExtreme for example) - make sure it supports the currents you are planning to switch with it! It will need a small modification with a 1kohm resistor to work with 3.3v, described below.
  • a 3D-printer (of course...)

The rest of this tutorial assumes that you bought the one from Amazon, as this is the one I have...

Wiring

Modify the 5V relay board to run off of RPI 3.3V GPIO pins (sainsmart 5V relay from amazon)

You can drive the Opto-coupled boards with the 3v3 I/O of the RPi. In most cases they have a 1000ohm resistor and LED in series with the LED part of the opto-coupler.

You simply add another 1k resistor across the existing one, and they'll happily drive from the 3v3 RPi i/o pins. You'll need both 5v (for the relays) and 3v3 for the channel drive as these boards are active low (ie you make IN1 LOW to turn on the relay)

relay board modification

(in the picture you can see 2 resistors added, the board has 4 relays, you need to add 1 resistor in each relay)

Always double-check your wiring. Never do wiring with the machine plugged in.

Connect to RPi

Lets first take a look at the GPIO-Pins:

RPi GPIO

If you need more info on the GPIO pinout, check here: RPI-3 B GPIO pinout article

For this project we will need the 8 top pins(physical location pin 1-8).

Remove the 2 pin (JD-VCC to VCC) jumper from the relay board, then connect two pins as follows:

  • connect JD-VCC to one of the 5V pins on the Pi (pin 2 or 4 on GPIO header)(this powers the relays)
  • connect VCC to RPI 3.3V (pin 1 on GPIO header)(this powers the optocouplers)

The 6-pin-header is to be connected as follows:

  • connect GND to Ground (surprise ;D)
  • connect IN1-IN4 to the remaining pins
  • leave VCC unconnected or connect it to the 2 pin header's VCC(pin 1 on GPIO header)(they are the same trace)

Printer power supply

Cut one of the two wires coming into your printer's power supply. Bare the resulting ends a little, twist and screw into the designated relay (look this up if it's not obvious).

(As an aside I would suggest to cut the live/hot wire (normally Brown in the EU/UK and Black in the US) and switch that with the relay. I was always told that would be safer and is how a wall switch works.)

Configuring OctoPrint

Easiest way

Install the PSU Control Plugin via OctoPrint's built-in Plugin Manager (Settings > Plugin Manager > Get More...) and configure it according to your setup.

Manual way

This RPi-Wiki side describes all about using the GPIO. From my experience it is the easiest to install WiringPi (instructions are here) and use its commands.

In our case we only need "gpio -g mode xx out/in". For example (in your config.yaml):

system:
  actions:
  - action: printer off
    command: gpio -g mode 7 in
    confirm: You are about to turn off the printer.
    name: Turn off printer

Hint: You can also copy/paste the following string into the YamlPatcher Plugin for the same effect (Screenshot for comparison):

[["append", "system.actions", {"action": "printer off", "command": "gpio mode 7 in", "name": "Turn off printer", "confirm": "You are about to turn off the printer."}]]

Alternative solution

If you have a computer PSU. A cheaper solution (probably) is to have one of the GPIO pins pull PS_ON (green wire on the ATX connector) low. I do this with an NPN transistor: The base is connected to the GPIO pin on the Raspberry Pi in series with a resistor (in my case 5kΩ, but it of course depends on what kind of transistor you are using). The collector is connected to PS_ON, and the emitter is connected to ground on the ATX (any black wire). The RPI and PSU need to have a common ground reference. By having a USB cable connect to the printer electronics, in my case Printrboard, they automatically share ground (maybe not an optimal solution, but it works fine for me!).

Using a Grove Relay with the OctoPi image

The SeeedStudio Grove Relay Board is readily available, and very easy to get working.

You will need to install WiringPi as above.

Connect the Relay board to the Raspberry Pi as follows:

  • VCC -> 5v
  • GND -> GND
  • Signal -> any GPIO pin, I used Pin 18

In /etc/init.d/octoprint, add the following at line 77, before RETVAL="$?"

gpio export 18 out

Where "18" is the GPIO pin you're using.

Next, use the PSU Control Plugin mentioned above.

Alternatively add to ~/.octoprint/config.yaml, in the system>actions section:

system:
  actions:
  - action: printer on
    command: gpio -g write 18 1
    name: Turn on the printer
  - action: printer off
    command: gpio -g write 18 0
    confirm: You are about to turn off the printer.
    name: Turn off the printer

Hint: You can also copy/paste the following string into the YamlPatcher Plugin for the same effect (Screenshot for comparison):

[["append", "system.actions", {"action": "printer on", "command": "gpio -g write 18 1", "name": "Turn on the printer"}], ["append", "system.actions", {"action": "printer off", "command": "gpio -g write 18 0", "name": "Turn off the printer", "confirm": "You are about to turn off the printer."}]]

Next, restart your Raspberry Pi. You should now have two options in the System menu. Test them by clicking the options - the relay should turn on and off, and the LED on the Grove Relay Board will light up. Once you have confirmed the relay is working as intended, you can connect the power supply.

You should be connecting the "hot" side of the power supply - so connect the +V to one screw terminal, and a length of appropriate wire to the other one. Then connect the wire to the +V on the printer. Now the menu options should control the printer power!

Clone this wiki locally
You can’t perform that action at this time.