# Adafruit's Raspberry Pi Lesson 13. Power Control

## Overview

The Raspberry Pi can make a great home automation controller. But to be really useful, it needs to be able to turn electrical appliances and lights on and off. That means it needs to be able to control 110V safely.

![](https://cdn-learn.adafruit.com/assets/assets/000/004/158/medium800/learn_raspberry_pi_overview.jpg?1396764305)

In this lesson, you will combine the PIR sensor from [Lesson 12](http://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement/) with the Powerswitch Tail 2 module from Adafruit, to automatically switch something on when movement is detected.

# Adafruit's Raspberry Pi Lesson 13. Power Control

## Hardware

Danger: 

## Power Tail 2
This tutorial works with all versions of Raspberry Pi (rev 1, 2, A, B, B+ and Zero) except the compute module which has no headers.&nbsp;

The Powerswitch Tail 2 from Adafruit, looks like a 110V electrical lead with a box in the middle.&nbsp;

The box is actually an opto-isolated solid-state relay. The opto-ilsolation bit means that there is no actual electrical connection between the low voltage switching circuit and the 100V mains. This makes it extremely safe and removes any chance of accidentally frying your Pi.

What is more the control input consumes just 3 mA of current at 3.3V, which means we can control it directly from one of the Pi's output pins.

The PIR sensor is connected to a different pin from lesson 12, so be sure to move the yellow lead.

&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/004/165/medium800/learn_raspberry_pi_powerswitchtail2_MED.jpg?1396809322)

## Controllable Four Outlet Power Relay Module Version 2
The [Controllable Four Outlet Power Relay Module Version 2](https://www.adafruit.com/product/2935) can be used as an alternative for this project.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/074/024/medium800/sensors_power-relay.jpg?1554343878)

## 40-Pin (A, B, B+ and Zero) Cobbler Plus Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/074/025/medium800/sensors_Raspberry_Pi_Power_Control_bb.png?1554344860)

## 20-Pin (Raspberry Pi Rev 1 and Rev 2) Cobbler Schematic
The Powerswitch Tail comes with a little indicator LED that shows when it is on, so you do not need to connect anything high voltage to it yet.

![](https://cdn-learn.adafruit.com/assets/assets/000/004/166/medium800/learn_raspberry_pi_breadboard.png?1396809370)

# Adafruit's Raspberry Pi Lesson 13. Power Control

## Software

## The Setup
We are&nbsp;using the CircuitPython Libraries that are part of adafruit-blinka. See&nbsp;[CircuitPython Libraries on Raspberry Pi&nbsp;](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi)to get a fresh Raspberry Pi setup.

If you have a running Raspberry Pi with an up to date copy of Raspbian you can simply run the following command to install adafruit-blinka.&nbsp;

The software is exactly the same on all 40-pin and 20-pin Raspberry Pi models.&nbsp;

```auto
$ sudo pip3 install adafruit-blinka

```

## The Code
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.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Raspberry_Pi_Power_Control/Raspberry_Pi_Power_Control.py

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.

# Adafruit's Raspberry Pi Lesson 13. Power Control

## Configure and Test

## Get the Code
Let's put this file right in your home directory for simplicity. The wget command makes things easy as this can be run from an internet connected Raspberry Pi.

```auto
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Raspberry_Pi_Power_Control/Raspberry_Pi_Power_Control.py
```

Danger: 

## Running the Code
Confirm that the Pi connected to the Cobbler (or Cobbler Plus) and run the script.

To start with, cover the PIR sensor with something, so that it is not activated until you are ready.

The following command will start the program:

```auto
$ sudo python3 ./Raspberry_Pi_Stepper_Motors.py 
```

Uncover the PIR sensor and you should see a message saying 'POWER ON' and the little LED on the Powerswitch Tail should light, then turn itself off after 20 seconds.

Now this is all working, you can try attaching a lamp or some electrical appliance to the Powerswitch Tail.

![](https://cdn-learn.adafruit.com/assets/assets/000/074/047/medium800/sensors_power-control.png?1554413181)


## Featured Products

### Controllable Four Outlet Power Relay Module version 2

[Controllable Four Outlet Power Relay Module version 2](https://www.adafruit.com/product/2935)
Say goodbye to hazardous high voltage wiring and create the [Internet of Things](https://www.adafruit.com/categories/342) with safe, reliable power control. The **IoT Power Relay** &nbsp;from&nbsp;[Digital...](http://www.digital-loggers.com/iot.html)

In Stock
[Buy Now](https://www.adafruit.com/product/2935)
[Related Guides to the Product](https://learn.adafruit.com/products/2935/guides)
### Powerswitch tail 2

[Powerswitch tail 2](https://www.adafruit.com/product/268)
[**The PST has been discontinued. Please check out the IoT Power Relay for an equivalent alternative. The IoT Power Relay has more outlets but otherwise is functionally the same, and at a similar price!**](https://www.adafruit.com/product/2935)

The Power Switch...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/268)
[Related Guides to the Product](https://learn.adafruit.com/products/268/guides)
### PIR (motion) sensor

[PIR (motion) sensor](https://www.adafruit.com/product/189)
PIR sensors are used to detect motion from pets/humanoids from about 20 feet away (possibly works on zombies, not guaranteed). This one has an adjustable delay before firing (approx 2-4 seconds), adjustable sensitivity **and** we include a 1 foot (30 cm) cable with a socket so you...

In Stock
[Buy Now](https://www.adafruit.com/product/189)
[Related Guides to the Product](https://learn.adafruit.com/products/189/guides)
### Assembled Pi T-Cobbler Plus - GPIO Breakout

[Assembled Pi T-Cobbler Plus - GPIO Breakout](https://www.adafruit.com/product/2028)
 **This is the assembled version of the Pi T-Cobbler Plus. &nbsp;It only works with the Raspberry Pi Model Zero, A+, B+, Pi 2, Pi 3, Pi 4, and Pi 5!** (Any Pi with 2x20 connector)  
  
The Raspberry Pi has landed on the Maker World like a 40-GPIO pinned, quad-USB ported,...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2028)
[Related Guides to the Product](https://learn.adafruit.com/products/2028/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

In Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)
### Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi

[Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi](https://www.adafruit.com/product/914)
Now that you've finally got your hands on a [Raspberry Pi® Model B](http://www.raspberrypi.org/), you're probably itching to make some fun embedded computer projects with it. What you need is an add on prototyping Pi Cobbler from Adafruit, which can break out all those...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/914)
[Related Guides to the Product](https://learn.adafruit.com/products/914/guides)
### Adafruit Assembled Pi T-Cobbler Breakout for Raspberry Pi

[Adafruit Assembled Pi T-Cobbler Breakout for Raspberry Pi](https://www.adafruit.com/product/1754)
Now that you've finally got your hands on a [Raspberry Pi®](http://www.raspberrypi.org/), you're probably itching to make some fun embedded computer projects with it. What you need is an **assembled** add on prototyping Pi T-Cobbler from Adafruit, which can...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1754)
[Related Guides to the Product](https://learn.adafruit.com/products/1754/guides)
### Adafruit Pi Cobbler + Kit- Breakout Cable for Pi B+/A+/Pi 2/Pi 3

[Adafruit Pi Cobbler + Kit- Breakout Cable for Pi B+/A+/Pi 2/Pi 3](https://www.adafruit.com/product/1990)
The Raspberry Pi B+ has landed on the Maker World like a 40-GPIO pinned, quad-USB ported, credit card sized bomb of DIY joy. And while you can use most of our great Model B accessories by hooking up our [downgrade cable,](https://www.adafruit.com/product/1986) its probably a good...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1990)
[Related Guides to the Product](https://learn.adafruit.com/products/1990/guides)

## Related Guides

- [PIR Motion Sensor](https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor.md)
- [Adafruit AMG8833 8x8 Thermal Camera Sensor](https://learn.adafruit.com/adafruit-amg8833-8x8-thermal-camera-sensor.md)
- [Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing.md)
- [Adafruit's Raspberry Pi Lesson 12. Sensing Movement](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement.md)
- [Windows IoT Core Application Development: Headed Blinky](https://learn.adafruit.com/windows-iot-application-development-headed-blinky.md)
- [Capacitive Touch Sensors on the Raspberry Pi](https://learn.adafruit.com/capacitive-touch-sensors-on-the-raspberry-pi.md)
- [Single Channel LoRaWAN Gateway for Raspberry Pi](https://learn.adafruit.com/raspberry-pi-single-channel-lorawan-gateway.md)
- [Node.js Embedded Development on the Raspberry Pi](https://learn.adafruit.com/node-embedded-development.md)
- [IoT Temperature Logger with Analog Devices ADT7410, Raspberry Pi, and Adafruit IO](https://learn.adafruit.com/iot-temperature-logger-with-python-and-adafruit-io.md)
- [Basic Resistor Sensor Reading on Raspberry Pi](https://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi.md)
- [CircuitPython Libraries on Linux and ODROID C2](https://learn.adafruit.com/circuitpython-libaries-linux-odroid-c2.md)
- [Windows IoT Core Application Development: Headless Blinky](https://learn.adafruit.com/windows-iot-application-development-headless-application.md)
- [Adafruit's Raspberry Pi Lesson 4. GPIO Setup](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup.md)
- [Adding Basic Audio Ouput to Raspberry Pi Zero](https://learn.adafruit.com/adding-basic-audio-ouput-to-raspberry-pi-zero.md)
- [Playing sounds and using buttons with Raspberry Pi](https://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi.md)
- [Large Pi-based Thermometer and Clock](https://learn.adafruit.com/large-pi-based-thermometer-and-clock.md)
