As I continue to learn more Python code I’ve reached a point where I want to have the ability to control multiple electronics with the use of a relay switch boar I bought back in December 2018. Originally I always going to build a musical light display for my two younger cousins at my uncle’s. Unfortunately with the planning and beginning of Sudo Grizzly I just didnt have the time. So I’m dusting off the old relay board and giving it a shot.
Raspberry Pi: Control Relay switch via GPIO
Eventually you will reach a point where you may want to control multiple modules via the Raspberry Pi’s GPIO Pins. If this is you then the best way to accomplish this task will be with a relay switch board: The relay switch works via a small voltage pulse. REMEMBER the Pi only withstand up to 5V (the GPIOs even less at 3.3V) and this is without a relay, so you run the risk of burning out the Rpi. So the best way around this issue is to instead build two circuits rather than one.
In this tutorial, I will walk you guys and gals through how to both set this up and provide you with some basic Python code to test the set up
Required Hardware Parts
5V relay module*
Female – Female jumper cable*
an external circuit (e.g., batteries*) and an application (eg, motors)
The relays are sold as 2*, 4*, 8* and even 16* modules, depending on what you need. We will be using a 4 relay switch board from Velleman.
The physical set up is very basic since all pins are labelled. Left (GND) comes to pin 6 of the Pi (GND), the right pin (VCC) comes to 3V3 (pin 1) of the Pis. Depending on how many relays you’re looking to control just connect them to the corresponding Raspberry Pi “IN” pins. If you want you can add a resistor between the Pi and the relay but it’s not totally necessary given the voltage of 3V3.
However if you set 5V instead of 3.3V to VCC, definitely add one resistor each (~ 1kΩ) between the GPIOs and the IN pins.
Now on the other side are the 3 pin connections (check out the pic below): Depending if the IN pin is a LOW (0V) or HIGH (3.3V or 5V) is added to either the switch between the center and right, or Open center and left. If you connect all 3 pins, you can use the relay more so like a switch, this leaves it free on the left or right and has the effect of an on and off switch. Finally the VCC or ground are connected (middle or right/left) does not matter.
Either center-left or centrr-right is connected/”opened”.
If you want to connect devices with high voltages, you should either know exactly what you are doing or ask an electrician! PLEASE BE CAREFUL AS 230V COULD LEAVE YOU LOOKING LIKE GARY COLEMAN (SHRIVELED UP DEAD AND BLACK). Pay extra attention to the specifications of the relay and take, if possible, no dodgy parts from China (which doesn’t matter in the low-power range but at higher voltages you should spend a bit more and take proven products). SUDO GRIZZLY GENTS LLC ASSUMES NO LIABITY FOR DAMAGES OR LOSS OF LIFE. This tutorial is for educational purposes only and you should do this only with an experienced professional or for the younger ones with adult supervision.
Raspberry Pi Relay Control
Also, the control is not very difficult, since only GPIOs have to be switched. You can use C++ (wiringPi) or Python for it. I am using Python and have used GPIO 17 (pin 11).
Here is the Python script we will be using
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) # GPIO Numbers instead of board numbers
RELAIS_1_GPIO = 17
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT) # GPIO Assign mode
GPIO.output(RELAIS_1_GPIO, GPIO.LOW) # out
GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # on
If 0V is present at the relay pin, the corresponding LED lights up, at a HIGH level the LED goes out. So if you want the relay to open at a HIGH level, you need to connect the middle and left pins to the circuit. The LED is off there. If the relay is to open, if the LED is also on, middle and right OUT pins are connected.
That’s it. Nothing to it and now we have added another piece of the puzzle to whatever it is we are building.
Let us know in the comments if this worked for you and what sorts of devices your controlling with the relay switches. If you have any questions of input on how we can improve be sure to let us know in the comments section.
As always keep on building, learning, and creating.
– Billie T. Grizzly
Remember purchasing from our store and any links we provide help support us so we can continue bringing you more Tutorials and fun builds. Even a simple share of our blog or articles help us reach other Grizzlys for us to help and encourage on their builds.