Software

This project is possible the most over-engineered automatic light switch ever created. You really do not need a Raspberry Pi to turn on the Powerswitch, but the example can easily be adapted for other purposes. For example, you could use a combination of temperature, humidity, light, and perhaps some internet data on weather forecasts to turn on or off a heater, fan or humidifier.

Download: file
import time 
import RPi.GPIO as io 
io.setmode(io.BCM) 

pir_pin = 24 
power_pin = 23

io.setup(pir_pin, io.IN) 
io.setup(power_pin, io.OUT)
io.output(power_pin, False)

while True:
    if io.input(pir_pin):
        print("POWER ON")
        io.output(power_pin, True)
        time.sleep(20);
        print("POWER OFF")
        io.output(power_pin, False)
        time.sleep(5)
    time.sleep(1)

The program first sets up the two GPIO pins that are used, one as an input to the PIR sensor and one as an output for the Powerswitch Tail.

The main loop then just waits for the PIR sensor to sense movement and then prints a message and turns on the Powerswitch tail, waits 20 seconds then turns it off again.

It the output was turned on, it waits for 5 seconds to prevent immediate retriggering.

There is also always a 1 seconds delay each time around the loop.

PIR sensors and the Pi 3 sometimes don't get along - if you're having false trigger reports, make sure the PIR Sensor is far away from the Pi 3
This guide was first published on Feb 18, 2013. It was last updated on Feb 18, 2013. This page (Software) was last updated on Jun 23, 2016.