How to Use a Relay with a Raspberry Pi

Raspberry Pi relay
Add an electrically-operated switch to your Pi!
Tyler Tyler (285)
Total time: 20 minutes 

A relay is an electrically-operated switch. These switches can be extremely useful for a variety of Raspberry Pi projects (think turning on a light or opening your garage door. Watch the video below to learn how to drive a relay using your Raspberry Pi.

Here's everything you'll need to complete this guide:

Raspberry PiRaspberry Pi×1
SainSmart 2-channel Relay Module×1

The following video will show you how to wire up the relay to your Raspberry Pi.

Here's a copy of the test script used in the video:

#!/usr/bin/env python

import time

import RPi.GPIO as GPIO

GPIO.setup(17, GPIO.OUT)
GPIO.output(17, GPIO.LOW)


GPIO.output(17, GPIO.HIGH)

To run the script, simply copy the code above to a file on your Raspberry Pi called (or whatever you want). And then run the script using: