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.

 

Setup

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?

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

    Reply
    • 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.

      Reply
      • 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?

  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.

    Reply
  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?

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

    Reply

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

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