How to control a power outlet with a raspberry pi

Doing this is actually surprisingly easy, but I couldn’t find a simple guide online on how to do this. In order to do this, you’ll need the following:

#Part
1Raspberry Pi
1IoT Relay

The “IoT relay” is the simplest (and safest) way to do this. Basically, you just need to supply power to the signal connector (green thing with wires plugged into it on the right) and you can do this directly with a GPIO pin (supplies 3.3V) on the raspberry pi:

In this example, I have a fan on the “normally ON” outlet. The raspberry pi can be powered with the “always ON” outlet. I have a wire connected between the GPIO 17 pin on the raspberry pi and the “+” input on the green signal connector on the IoT relay and a wire connected between a ground pin on the raspberry pi and the “-” input on the green signal connector on the IoT relay. Just as a reminder, the raspberry pi 4 pin layout is shown below:

Once you turn on power to the IoT relay, the fan should turn on. If you run the following python code:

import RPi.GPIO as GPIO

PIN_GPIO = 17

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_GPIO, GPIO.OUT)

GPIO.output(PIN_GPIO, True)

The “switch active” light should turn on on the IoT relay and the fan should stop running. Running:

GPIO.output(PIN_GPIO, False)

Will make the “switch active” light turn off and the fan should start running again. If you want the opposite functionality, you can plug the fan into the “normally OFF” outlet.

Leave a Reply

Your email address will not be published. Required fields are marked *