This is an old revision of the document!
Table of Contents
Using A Raspberry Pi to Externally Control Power
Materials
- Raspberry Pi Model 4 Raspberry Pi 4
- x2 Jumper Wires (Length will vary based on needs) Jumper Wires
- Enclosed High-power Power Relay Power Relay
Assembly
- Cut the male end of the wire jumper off and then strip the wire to leave around 1/8“ inch wire exposed
- Pull out green power input module from power relay box
- Unscrew wire clamp screws on the power input module
- Place striped end of jumper wire into the power input on the module (one to negative, one to positive)
- Screw down wire clamp screws
- Connect the wires to appropriate locations on the Raspberry Pi GPIO pin board (more explanation given below)
- Plug power supply for the raspberry pi into the Always On outlet on the power relay box
- Plug appliances that you want to control into the Normally Off outlet on the power relay box
- Plug power relay into an appropriate outlet
Code to Control Power Relay
In order to control the power relay box it is necessary that code is written on the Raspberry Pi to make these changes to the power relay box. The example that is provided is in Python which requires that the library rpi.gpio be installed in order to gain control over the GPIO pins on the Raspberry Pi. Step by step examples will be given below to download the library, write code for controlling relay box, and knowing what pins to connect jumper cables to.
Downloading RPi.GPIO Library
- Open terminal
- type sudo apt-get install RPi.GPIO
- installation complete
Writing Code
Example of code written in python
Below is both a full example of the code and a break down of what everything does
Full Example
import RPi.GPIO as GPIO import time import sys GPIO.setmode(GPIO.BOARD) power_choice = str(sys.argv[1]) pin_number = # if power_choice == 'on': GPIO.setup(pin_number, GPIO.OUT) GPIO.output(pin_number, GPIO.HIGH) While True: time.sleep(1) exit() elif power_choice == 'off': GPIO.setup(pin_number, GPIO.OUT) GPIO.output(pin_number, GPIO.LOW) while True: time.sleep(1) exit() GPIO.cleanup()
Breakdown
Importing of appropriate libraries into python
import RPi.GPIO as GPIO import time import sys
This sets the way that the GPIO Pins will be recognized by the code
GPIO.setmode(GPIO.BOARD)
This allows for user input to tell the code what to do (turn power on/off)
power_choice = str(sys.argv[1])
The # should be replaced by an appropriate integer corresponding to the gpio pin picked
pin_number = #
If the input from user specifies on then the code performs as follows
if power_choice == 'on':
This specifies that for a given pin number it will change the output of that pin
GPIO.setup(pin_number, GPIO.OUT)
This sets that pin to be powered on which when power is provided to the relay it switches to on
GPIO.output(pin_number, GPIO.HIGH)
This is code that needs to be added in order for the code to stop after the command is performed
While True: time.sleep(1) exit()
Same thing happens here but this is now just for the turning power off command
elif power_choice == 'off': GPIO.setup(pin_number, GPIO.OUT) GPIO.output(pin_number, GPIO.LOW) while True: time.sleep(1) exit()
This must be added in order for the pins to not take on multiple arguments from later commands that might be sent
GPIO.cleanup()