TRAFEX TRAFEX Consultancy Consultancy
Connect a relay board to your Raspberry Pi

Connect a relay board to your Raspberry Pi

August 25, 2014

If you want to switch things like lights with your Raspberry Pi, you need a relay board. A relay is an electronic switch that can be switched with a low-voltage signal. Exactly what the Raspberry Pi offers as GPIO pins. The relay board I’ve found is ideal for the Raspberry Pi.

It has 8 relays and every relay can separately be switched with the Raspberry Pi because it has 8 GPIO pins. Also, the voltage needed to switch the relays is 5V, this can be taken directly from the RPi. This is why you don’t need any additional electronics.

Hardware

You need the following things. I’ve bought these on banggood.com, but they’re widely available.

Pin scheme

WiringPi uses its own pin numbering system. The scheme below connects relay 1 to WiringPi number 0, relay 2 to number 1, etc. See this page to find the right pins on the Raspberry Pi.

Pin on RPI Pin on relay board WiringPi number
5V power VCC
GND GND
GPIO17 INS1 0
GPIO18 INS2 1
GPIO27 INS3 2
GPIO22 INS4 3
GPIO23 INS5 4
GPIO24 INS6 5
GPIO25 INS7 6
GPIO4 INS8 7

(This scheme is based on a revision 2 Raspberry Pi)

Software

I’m using WiringPi as library to control the connected relay board. WiringPi is widely supported for popular languages like NodeJS , PHP, Python, etc.

  1. Install WiringPi
  2. Set every pin on OUT mode
gpio mode 0 out
gpio mode 1 out
gpio mode 2 out
gpio mode 3 out
gpio mode 4 out
gpio mode 5 out
gpio mode 6 out
gpio mode 7 out
  1. Write 0/1 to the ‘WiringPi number’ from the pin scheme to test the relays
gpio write 0 0
gpio write 0 1
gpio write 1 0
gpio write 1 1

Now startup your IDE and build something awesome to control the electronics connected to the relay board!

Go back

Recent articles

Articles

Checklist: Prepare your website for Go Live

When you’re ready to launch your website or web application, you can use this checklist to be fully prepared for the Go Live and have a smooth transition!

Read More

Articles

The key components of Kubernetes autoscaling

Autoscaling is an important feature of Kubernetes. With this feature you always have enough resources for the workload, and when a node becomes unhealthy it gets replaced without effecting the workload. But you won’t get it automatically by just deploying your Pods on Kubernetes.

Read More

Articles

Create a DB backup from a pod running MySQL on Kubernetes

When using the MySQL docker image you can easily create a DB dump with this one-line CLI command.

Read More