Raspberry Pi: Control Relay switch via GPIO

Often you want to control modules with a higher voltage with the Raspberry Pi. For this purpose, relays can be used on the Raspberry Pi: The relay “switch” is utilized by means of a low-voltage pulse. Since the Pi only tolerates a maximum of 5V (the GPIOs even only 3.3V) without relays, there is the risk that the Pi could burn out. However, if you have two separate circuits this can not happen.

In this tutorial, I will show how to control a relay with the Raspberry Pi and what has to be considered.

Required Hardware Parts


The relays are available as 2, 4, 8 and even 16 modules, depending on what you need. To avoid wasting too many GPIOs on the Pi it pays off to purchase a GPIO port expander with more than 4 channels.



The structure is very simple 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 of the relays you want to control, you need to connect a corresponding number of GPIOs to the IN pins. It is recommended to set a small resistor between the Pi and the relay, but it is not absolutely necessary with 3V3.
If you set 5V instead of 3.3V to VCC, you should definitely put one resistor each (~ 1kΩ) between the GPIOs and the IN pins.


On the other side are at each relay 3 connections (see picture below): Depending on whether the IN pin is a LOW (0V) or HIGH (3.3V or 5V) is applied either the switch between the centre and right, or Open centre and left. If you connect all 3 pins, you can use the relay as a kind of switch, leaving it free on the left or right has the effect of an on/off switch. Where VCC or ground are connected (middle or right/left) does not matter.


Raspberry Pi Relay: Either center-left or centre-right is connected / "opened".

Either center-left or center-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! 230V is life-threatening. 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). I assume no liability for damages!



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).

sudo python

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. I would like to know, in which applications have you found the use for the Raspberry Pi relays?

13 Responses

  1. You can fry your Pi hooking your GPIO directly to your relays…
    You need to put in a transistor and resistors in each connection.

    • And then you’ll fry the transistor and your GPIO.

      You can drive the proper relay directly, but you need a snubber circuit to stop the inductive kickback caused by removing energy to the relay. You use a diode and a small capacitor for that.

      • hi Jacobus and Red!
        I’ve had this doubt since I bought those relay boards. They do have a transistor and a diode and I think they are for protection. Isn’t that enought to protect the GPIOs?

      • The relay control boards almost all have a schottky diode placed in reverse and parallel to the relay to act as a flyback protection circuit. They are also made with an optocoupler (optoisolator) to electrically isolate the relay control circuit from the gpio pin. The optoisolator has control pins usually have a resistor in series with them somewhere to reduce the drain on whatever device is controlling it. However, since the relay boards operate at 5v, another resistor between the IN pin on the relay board and GPIO on the Pi is recommended. What I’m curious about is that the relays on most relay boards are 5v relays, meaning they require 5v in order to “switch on.” How can you provide only 3.3v from the pi to the relays and expect that to be enough to switch them them? Also, if you do use the Pi’s 5v as Vcc to the relay board, you probably shouldn’t put the Pi in a situation where the relay board may be “turning on” more than, say, 5 relays at once. If I recall, the coils in those 5v relays can draw around 30mA. The RPi 3 is only capable of supplying about 300mA safely from it’s +5v GPIO Vcc pin. Is it a good idea to drive relays from the Pi’s 5v pin?

    • The relay board shown in the photo above is the same as the one I have here on my bench, (about £5 from Amazon.co.uk) has an Opto isolator on each input to protect the GPIOs and a diode across each relay to protect the driver transistors on the board. You can also power the relays from a separate supply to protect the Pi’s power supply.

  2. I use Remote Desktop to control the RPi. I then start a program I wrote in Return To BASIC to control 16 Relays. One of the relays powers up my old Win XP driven Compaq which still will let me run 16 bit PowerBASIC compiled programs I wrote many years ago to work with my Weather Station.

    • I’m also using an old DOS program (created in VisualBasic for DOS). I’m now moving it to a Raspberry Pi with Linux and Dosbox-x because when the 20 year old PC will be dead, the program won’t run anymore. The program controls my central heating so you can imagine why I’m using the relays 😉

  3. I don’t have a very technical background, can you elaborate on the arrangement of the transistor and resistor? Also what kind of transistor would be used?

  4. Just put a 2kOhm resistor between Pi and relay board and you’ll be fine.
    It’ll limit the drain to 2.5mA on that part of the circuit.

  5. I am using single channel relay . After number of ON OFF switching the relay stops to respond to signal and don’t switch . is there a solution for such a problem

  6. hello,

    I am trying to use a control relay to operate for a certain duration of time. Could I use something like the import time feature and set the sleep.time(duration) as the on time and then turn off once the duration time is met?

    Thanks in advance,


Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!