Wednesday, May 25, 2016

Relay Software

This post will list the python commands needed to control the relay board as well as how to run the commands from the command line.


Every script should have these three lines:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(x,GPIO.OUT) #x is the pin number, this tells the raspberry pi that                                                    the pin is an output

The first line imports the library, the second line tells the program how the pins are numbered and the third line tells the program that the pin is an output.

The next two lines turn the relay on and off:
GPIO.output(x,GPIO.HIGH) # this turns the relay light on
GPIO.output(x,GPIO.LOW)  # this turns the relay light off

Try and except, while loops and/or for loops can be used to with these commands to make the relay do different things, see this video's description for example codes. For multiple relays it is helpful to put the pin numbers in a list. This makes running for loops over multiple pins easier.  

To run the script from the command line type, python filename.py


To control the board from the command line without a script start by typing python, then entering the three lines above (import, setmode, setup), then GPIO.output(x,GPIO.HIGH) and GPIO.output(x,GPIO.LOW). Use the up arrow to switch between the two. To quit python type quit().

Relay Hardware

This post will explain how to connect the relay board and outlet to wall power.


Wire the right screw of the outlet to the middle port of the relay. Wire the left screw of the outlet to the neutral wire of the cord that plugs into the wall. The left port of the relay connects to the hot wire of the cord that plugs into the wall. The ground of the outlet is connected to the green wire of the cord.

If working properly, when the light on the relay board is off then the outlet does not get power. When the light on the relay board is on then the outlet does get power.