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

No comments:

Post a Comment