User Tools

Site Tools


raspberry_pi_external_power_control

Using A Raspberry Pi to Externally Control Power

Materials

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

GPIO Pin Setup

Use GPIO Pin Layout as a reference to what is discussed.

When hooking up the jumper cables to the raspberry pi it is important to note that these wires have specific locations that they must be placed at. The different types of locations are provided in the link attached above.

To properly hook up the power relay to the Raspberry Pi you must connect the corresponding positive wire that is hooked into the power relay to one of the different GPIO labeled pins on the diagram (i.e. GPIO 2 (SDA), GPIO 17, GPIO 23). Note keep track of the small label number that is in the diagram as that is the number that you will use in the code. Once you have hooked up the positive lead to the Raspberry Pi next is the negative. This wire can be placed on any pin labeled ground in the diagram.

Now that you have connected the wires to the proper locations on the Raspberry Pi GPIO board next thing is to go into the python code that was written and change out the # with the small number in the circle of the corresponding GPIO pin that you selected. This will now allow you to properly have control over your power relay and any appliances connected to it.

raspberry_pi_external_power_control.txt · Last modified: 2025/07/28 17:11 by 127.0.0.1