# Your First Power Switch Relay Project - Circuit Playground Smart Plug

## Overview

https://youtu.be/xzCllfr2dRk

Control All the Things in your house with a Circuit Playground Express and a Power Switch Relay. This little power supply is a toggle-able "Smart Plug" that you can use for all kinds of home automation applications.

This relay controller has one plug that's always on (for powering your Circuit Playground).

The other plugs are where it gets interesting. There are two plugs that are normally OFF and one that is normally ON. Activating the relay with the Circuit Playground turns the "on" plug to OFF, and the two "off" plugs to ON. Simple and elegant, yes?

![sensors_2935-17.jpg](https://cdn-learn.adafruit.com/assets/assets/000/090/397/medium640/sensors_2935-17.jpg?1586978141)

Where it gets really interesting is when you combine this functionality with the inputs and triggers on with the Circuit Playground Express. You can toggle the switches so many ways:&nbsp;

- Move, tilt, or shake the Circuit Playground
- Make a loud sound (like a DIY version of "The Clapper!")
- A change in ambient light
- A change in ambient temperature
- Capacitive touch pads, and things attached to them
- Circuit Playground's onboard buttons

You can also create timers or combinations of triggers to toggle the outlets. It's safe to use with higher voltage devices like lamps or heaters, too. The sky is the limit.

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/090/262/medium800/makecode_powerswitch.jpg?1586559551)

## Parts
### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
![A Black woman's manicured hand holds a round microcontroller with lit up LEDs.](https://cdn-shop.adafruit.com/640x480/3333-05.jpg)

### 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)
![Controllable Four Outlet Power Relay Module](https://cdn-shop.adafruit.com/640x480/2935-13.jpg)

### 5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable

[5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable](https://www.adafruit.com/product/1995)
Our all-in-one 5V 2.5 Amp + MicroUSB cable power adapter is the perfect choice for powering single-board computers like Raspberry Pi, BeagleBone, or anything else that's power-hungry!

This adapter was specifically designed to provide 5.25V, not 5V, but we still call it a 5V USB...

In Stock
[Buy Now](https://www.adafruit.com/product/1995)
[Related Guides to the Product](https://learn.adafruit.com/products/1995/guides)
![MicroUSB power supply with bundled cable and U.S. plugs.](https://cdn-shop.adafruit.com/640x480/1995-02.jpg)

### Bolt-On Kit for Circuit Playground, micro:bit, Flora or Gemma

[Bolt-On Kit for Circuit Playground, micro:bit, Flora or Gemma](https://www.adafruit.com/product/4103)
You have a [Circuit Playground Express](https://www.adafruit.com/product/3333), and want to connect some wires to it for adding LEDs or sensors or speakers? You can use our nice alligator clips, but they are a little bulky. If you aren't quite ready to solder to your Playground,...

In Stock
[Buy Now](https://www.adafruit.com/product/4103)
[Related Guides to the Product](https://learn.adafruit.com/products/4103/guides)
![Close up angled shot of a LED strip attached and powered by a circuit playground. ](https://cdn-shop.adafruit.com/640x480/4103-06.jpg)

### You'll Also Need

- A small flat-head screwdriver
- A small phillips-head screwdriver OR soldering iron (if you're not using the bolt-on kit)

### A Note about On/Off Switched Devices

This power relay turns its outlets on and off -- that's all it does. We were trying this with various things around the house, and discovered that a lot of modern electronic devices don't come on automatically when their power plug is activated. If it's got an on/off switch, it often requires a button press to come to life, and this relay won't press the "on" button for you, it'll simply feed power. So keep that in mind when you're planning your project.

### Power Switch Relay Features:

- A single input signal to switch four outlets:&nbsp; **1 always on + 1 normally on + 2 normally off.**
- Universal control voltage 3-60VDC or 12-120VAC, or connect directly to a 12V, 48VDC, or even 120VAC circuit.
- Optical isolation, relay, hysteresis and de-bounce protection for added safety.
- A large MOV used to clamp surges for clean 90-140 AVC power.
- Durable SPDT control relay rated at 30/40A. More than 400k operations at 12A or 2million+ at 5A.
- A 12A thermal safety circuit breaker switch prevents overloads.
- Standard 19" C13 detachable cord included.
- Mounting tabs
- This power relay is professionally built and tested.

# Your First Power Switch Relay Project - Circuit Playground Smart Plug

## Assembly

You'll need a small flat-head screwdriver, and a small phillips head screwdriver (if you're using the bolt-on kit) or a soldering iron (if you're not).

On the side of the power strip is a small green wire connector. Pull that little guy out. Pliers can be your friend if it doesn't slide out easily.

Notice the markings on the box, above the connector. This little diagram tells you which wire is which.

![makecode_00_powerswitch.jpeg](https://cdn-learn.adafruit.com/assets/assets/000/090/229/medium640/makecode_00_powerswitch.jpeg?1586479599)

![makecode_01_removeplug.jpeg](https://cdn-learn.adafruit.com/assets/assets/000/090/231/medium640/makecode_01_removeplug.jpeg?1586479971)

Cut a white and a black wire to length. The length depends on your project -- if you're planning to mount the Circuit Playground someplace far away from the power switch, or use the motion triggers, you'll want to use pretty long wires. If you are planning to use a sound trigger, you can use shorter wires. I made mine the same length as my power cord (about a meter) so I'd have options.

Unscrew both of the little screws all the way. Stick the white wire into the + side and the black wire into the - side. (Labels are printed on the relay box, don't get them mixed up!). Tighten the screws down so the wires stay put.

![makecode_02_wireplug.jpeg](https://cdn-learn.adafruit.com/assets/assets/000/090/230/medium640/makecode_02_wireplug.jpeg?1586479658)

Stick the connector back in, and double check you've got white on the left, and black on the right. Give the wires a tug so you're sure they're secure.

![makecode_03_plugin.jpeg](https://cdn-learn.adafruit.com/assets/assets/000/090/232/medium640/makecode_03_plugin.jpeg?1586480030)

Strip a bit of shielding from the other end of the wires. Attach the white wire to pin **A1** and the black wire to **GND** on the Circuit Playground. Secure the wires with the bolt-on kit, or solder them in place.

If you're using the bolt-on kit, it's best to thread the wire through the hole first and then insert the screw, rather than winding the wires around the screw head. The wires will stay attached better if you do it this way, rather than the "traditional" around-the-head method.

Note: Do not use pin **A0**. It is connected to the onboard speaker so it's not so good for switching things. The rest of the analog pins, including **A1** , work very well.

![makecode_04_wireCPX.jpeg](https://cdn-learn.adafruit.com/assets/assets/000/090/233/medium640/makecode_04_wireCPX.jpeg?1586480104)

Finally, connect your USB power cord to the Circuit Playground's USB port and plug it into the unmarked outlet -- this one is always on so works great for powering your Circuit Playground.

![makecode_06_pluginCPX.jpeg](https://cdn-learn.adafruit.com/assets/assets/000/090/234/medium640/makecode_06_pluginCPX.jpeg?1586480185)

# Your First Power Switch Relay Project - Circuit Playground Smart Plug

## Code with MakeCode

**[Microsoft MakeCode for Adafruit](http://adafruit.makecode.com)&nbsp;**is a web-based code editor&nbsp;for physical computing. It provides a block editor, similar to Scratch or Code.org, and also a JavaScript editor for more advanced users.&nbsp;

This means you can drag and drop functionality that triggers the Circuit Playground Express' onboard inputs without ever writing a single line of code.&nbsp;

We've provided three different sample projects: one using the light sensor to turn a light on at dusk, one using the motion sensor to activate a light and then turn it off a few seconds later, and the sound sensor to make a "clapper" style toggle switch.

[Go Deeper: Check out our MakeCode intro guide here.](https://learn.adafruit.com/makecode/what-is-makecode)
Once you decide what you'd like your project to do, you can edit the code to customize it with your own lights, sounds, and sensitivities. Then, click the Download button on the MakeCode site to download your customized code.

Plug the Circuit Playground into your computer and drag the downloaded file onto the **CPLAYBOOT** drive that appears to make the Circuit Playground run the code.&nbsp;

![sensors_makecode_upload.jpg.jpg](https://cdn-learn.adafruit.com/assets/assets/000/090/508/medium640/sensors_makecode_upload.jpg.jpg?1587417409)

# Your First Power Switch Relay Project - Circuit Playground Smart Plug

## Light Activation

Here's a code sample that triggers the relay when the ambient light in the room changes. You can set a threshold for how dark or bright you want it to get, so it's easy to customize for a lot of different applications.

![](https://cdn-learn.adafruit.com/assets/assets/000/090/496/medium800thumb/sensors_light_activation.jpg?1587415422)

The one gotcha with using light sensors to turn lights on and off is that when it's dark enough to trigger the sensor, and you use the sensor to turn on a light, then it's no longer dark.. and so the light turns off again. It's sort of a chicken-and-egg problem. So in practice, you may want to use the light trigger for turning lights ON, then use a different trigger for turning them OFF.

[Edit and Download This Code at MakeCode.com](https://makecode.com/_9D60kF4iA698)
## How it Works

The magic happens with the `digital write pin A0 to low` block, located under **ADVANCED \> PINS**.&nbsp;

What it's doing is sending a signal through the white wire you hooked up to pin **A1** and either connecting or disconnecting it to **GND**. The power switch relay can read the difference, and that's how it knows to switch the outlets on or off.

![sensors_mc_intro_digitalwrite.jpg](https://cdn-learn.adafruit.com/assets/assets/000/090/505/medium640/sensors_mc_intro_digitalwrite.jpg?1587416893)

Once you've customized your code, click the Download button at the bottom of the screen. Plug your Circuit Playground into your computer via the USB port. The lights on the face will turn green (if they don't, click the tiny "reset" button in the center of the board) and a drive will appear on your computer called **CPLAYBOOT**.&nbsp;

To program your Circuit Playground, drag the file you downloaded onto this drive and the code will start running.

If you're having trouble, head over to the [MakeCode Beginner Guide](https://learn.adafruit.com/makecode) for more tips and troubleshooting.

# Your First Power Switch Relay Project - Circuit Playground Smart Plug

## Sound Activation

This code sample toggles the switch on or off whenever a loud sound is heard. It also lights up the Circuit Playground's onboard lights for just a moment, to give more feedback when the switch is triggered.

![](https://cdn-learn.adafruit.com/assets/assets/000/090/497/medium800thumb/sensors_snaps.jpg?1587415573)

[Edit &amp; Download Code This Code at MakeCode.com](https://makecode.com/_7mKHxaeFF6cd)
This is a great example of using MakeCode to create a toggle on/off switch with just a few blocks and an `if/then` statement. We've created a variable called `switchOn` which allows the Circuit Playground to "remember" whether the light is on or off, and toggle the power outlets accordingly.

![sensors_mc_soundsensor.jpg](https://cdn-learn.adafruit.com/assets/assets/000/090/506/medium640/sensors_mc_soundsensor.jpg?1587417027)

Once you've customized your code, click the Download button at the bottom of the screen. Plug your Circuit Playground into your computer via the USB port. The lights on the face will turn green (if they don't, click the tiny "reset" button in the center of the board) and a drive will appear on your computer called **CPLAYBOOT**.&nbsp;

To program your Circuit Playground, drag the file you downloaded onto this drive and the code will start running.

If you're having trouble, head over to the [MakeCode Beginner Guide](https://learn.adafruit.com/makecode) for more tips and troubleshooting.

# Your First Power Switch Relay Project - Circuit Playground Smart Plug

## Motion Activation

This code turns the power relay ON when the Circuit Playground is moved or shaken. It then starts a countdown timer, and when the time runs out, it switches the power relay to OFF again.

![](https://cdn-learn.adafruit.com/assets/assets/000/090/498/medium800thumb/sensors_motion.jpg?1587415603)

We used this as a closet light. The Circuit Playground is mounted to the door, and when the door is opened the light in the closet comes on, then turns itself off a few seconds later.

[Edit &amp; Download This Code at MakeCode.com](https://makecode.com/_9D60kF4iA698)
The timer code is based on the Game Clock code, so if you want to delve into how it works, [check that guide out here](https://learn.adafruit.com/game-clock-with-circuit-playground-makecode/code-with-makecode).&nbsp;

You can change the duration of the countdown timer by changing the `timer` variable.&nbsp; And you can change the countdown light color by choosing your favorite in the `set pixel color` block.

![sensors_variables.jpg](https://cdn-learn.adafruit.com/assets/assets/000/090/501/medium640/sensors_variables.jpg?1587416233)

If you want to change the sensitivity or the type of motion being sensed, there are lots of options available in the input block. Test a few out and see what works best for you.

![sensors_makecode_motions.jpg](https://cdn-learn.adafruit.com/assets/assets/000/090/500/medium640/sensors_makecode_motions.jpg?1587416003)

Once you've customized your code, click the Download button at the bottom of the screen. Plug your Circuit Playground into your computer via the USB port. The lights on the face will turn green (if they don't, click the tiny "reset" button in the center of the board) and a drive will appear on your computer called **CPLAYBOOT**.&nbsp;

To program your Circuit Playground, drag the file you downloaded onto this drive and the code will start running.

If you're having trouble, head over to the [MakeCode Beginner Guide](https://learn.adafruit.com/makecode) for more tips and troubleshooting.


## Featured Products

### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### Bolt-On Kit for Circuit Playground, micro:bit, Flora or Gemma

[Bolt-On Kit for Circuit Playground, micro:bit, Flora or Gemma](https://www.adafruit.com/product/4103)
You have a [Circuit Playground Express](https://www.adafruit.com/product/3333), and want to connect some wires to it for adding LEDs or sensors or speakers? You can use our nice alligator clips, but they are a little bulky. If you aren't quite ready to solder to your Playground,...

In Stock
[Buy Now](https://www.adafruit.com/product/4103)
[Related Guides to the Product](https://learn.adafruit.com/products/4103/guides)
### 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)
### Pink and Purple Braided USB A to Micro B Cable - 2 meter long

[Pink and Purple Braided USB A to Micro B Cable - 2 meter long](https://www.adafruit.com/product/4148)
This cable is&nbsp;super-fashionable&nbsp;with a woven pink and purple Blinka-like pattern!

First let's talk about the cover and over-molding. We got these in custom colors, and if you&nbsp;_have_&nbsp;to have visible cables, then you might as well have the nicest fabric-bound...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/4148)
[Related Guides to the Product](https://learn.adafruit.com/products/4148/guides)
### 5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable

[5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable](https://www.adafruit.com/product/1995)
Our all-in-one 5V 2.5 Amp + MicroUSB cable power adapter is the perfect choice for powering single-board computers like Raspberry Pi, BeagleBone, or anything else that's power-hungry!

This adapter was specifically designed to provide 5.25V, not 5V, but we still call it a 5V USB...

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

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Rose's Shield - Steven Universe](https://learn.adafruit.com/rose-s-shield-steven-universe.md)
- [Circuit Playground Express Sugar Glider](https://learn.adafruit.com/cpx-sugar-glider.md)
- [Zombie Tag Game with Circuit Playground Express](https://learn.adafruit.com/circuit-playground-express-ir-zombie-game.md)
- [Makecode para la Circuit Playground Express](https://learn.adafruit.com/makecode-es.md)
- [Sparky the Blue Smoke Monster Automaton](https://learn.adafruit.com/sparky-automaton.md)
- [FruitBox Sequencer: Musically Delicious Step Pattern Generator ](https://learn.adafruit.com/circuitpython-fruitbox-sequencer-musically-delicious-step-pattern-generator.md)
- [Robotic Creatures ](https://learn.adafruit.com/robotic-creatures.md)
- [UART Communication Between Two CircuitPython Boards](https://learn.adafruit.com/uart-communication-between-two-circuitpython-boards.md)
- [Circuit Playground Hourglass](https://learn.adafruit.com/circuit-playground-hourglass.md)
- [The Tilt Trumpet](https://learn.adafruit.com/the-tilt-trumpet.md)
- [Remote Control Tree Ornament with Circuit Playground Express](https://learn.adafruit.com/remote-control-tree-ornament-with-circuit-playground-express.md)
- [Desktop Dumpster Fire](https://learn.adafruit.com/desktop-dumpster-fire.md)
- [CircuitPython Sound Box](https://learn.adafruit.com/circuitpython-sound-box.md)
- [Dear Diary Alarm](https://learn.adafruit.com/dear-diary-alarm.md)
- [Easy Sparkle Pocket T-Shirt](https://learn.adafruit.com/easy-sparkle-pocket-t-shirt.md)
