# Remote Control Tree Ornament with Circuit Playground Express

## Overview

Danger: 

Have you put up with non-interactive, regular, old, low-tech tree ornaments for too long? Yes you have! But, this is where things change! It's time to build your own Remote Control Tree Ornament with Circuit Playground Express!!

The Circuit Playground Express (CPX) has a built in infrared (IR) sensor, and it's very simple to decode the signals using CircuitPython, thanks to the Adafruit IR Remote library.

We'll program the CPX to read IR blasts from the mini remote control, and use these commands to change the bright, beautiful NeoPixels. Plus, we'll defuse the light and dress it up with an inexpensive hobby store ornament and festive puff balls!

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

Out of 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)

### 3 x AAA Battery Holder with On/Off Switch, JST, and Belt Clip

[3 x AAA Battery Holder with On/Off Switch, JST, and Belt Clip](https://www.adafruit.com/product/3286)
This battery holder connects 3 AAA batteries together in series for powering all kinds of projects. We spec'd these out because the box is slim, and 3 AAA's add up to about 3.3-4.5V, a very similar range to Lithium Ion/polymer (Li-Ion) batteries, plus it has&nbsp;a nifty&nbsp;on-off...

In Stock
[Buy Now](https://www.adafruit.com/product/3286)
[Related Guides to the Product](https://learn.adafruit.com/products/3286/guides)
![Angled shot of 3 x AAA battery holder with on-off switch, JST PH connector, and belt clip.](https://cdn-shop.adafruit.com/640x480/3286-00.jpg)

### Alkaline AAA batteries - 3 pack

[Alkaline AAA batteries - 3 pack](https://www.adafruit.com/product/3520)
Battery power for your portable project! These batteries are good quality at a good price, and work fantastic with any of the kits or projects in the shop that use AAA's. This is a pack of **3 AAA batteries**.  
  
These batteries are Alkaline (MnO2) chemistry, with a...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3520)
[Related Guides to the Product](https://learn.adafruit.com/products/3520/guides)
![Angled shot of 3 PKcell AAA batteries. ](https://cdn-shop.adafruit.com/640x480/3520-00.jpg)

### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

In Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)
![USB cable - USB A to Micro-B - 3 foot long](https://cdn-shop.adafruit.com/640x480/592-01.jpg)

Or, optionally, to get most of those parts in a single pack:

### Circuit Playground Express - Base Kit

[Circuit Playground Express - Base Kit](https://www.adafruit.com/product/3517)
It's the **Circuit Playground Express Base Kit!** &nbsp;It provides&nbsp;the few things you'll need to get started with the new [Circuit Playground Express](https://www.adafruit.com/product/3333).&nbsp;This version of Circuit Playground is super powered, and will...

In Stock
[Buy Now](https://www.adafruit.com/product/3517)
[Related Guides to the Product](https://learn.adafruit.com/products/3517/guides)
![Rectangular case for Circuit Playground Express - Base Kit.](https://cdn-shop.adafruit.com/640x480/3517-04.jpg)

And these two items:

### Mini Remote Control

[Mini Remote Control](https://www.adafruit.com/product/389)
This little remote control would be handy for controlling a robot or other project from across the room. It has 21 buttons and a layout we thought was handy: directional buttons and number entry buttons. The remote uses the NEC encoding type and sends data codes 0 thru 26 (it skips #3, #7,...

In Stock
[Buy Now](https://www.adafruit.com/product/389)
[Related Guides to the Product](https://learn.adafruit.com/products/389/guides)
![Mini Remote Control with 21 buttons](https://cdn-shop.adafruit.com/640x480/389-03.jpg)

### DIY Ornament Kit - 6cm Diameter - Perfect for Circuit Playground

[DIY Ornament Kit - 6cm Diameter - Perfect for Circuit Playground](https://www.adafruit.com/product/4036)
Have you put up with mainstream, uninspiring, low-tech tree ornaments for too long? This season why not deck the halls with _codes_ of holly?

This **DIY Ornament Kit** is just the right size (6 cm diameter) for a Circuit Playground Express, 500mAh LiPoly battery, and...

In Stock
[Buy Now](https://www.adafruit.com/product/4036)
[Related Guides to the Product](https://learn.adafruit.com/products/4036/guides)
![6cm Diameter DIY Ornament Kit with Circuit board inside](https://cdn-shop.adafruit.com/640x480/4036-00.jpg)

Warning: 

## Materials

In addition to the materials above, you'll need:

- White iridescent pom poms from a craft store
- Ribbon or tree hanger hook

![](https://cdn-learn.adafruit.com/assets/assets/000/049/624/medium800/circuitpython_IMG_7001.jpg?1514071751)

# Remote Control Tree Ornament with Circuit Playground Express

## Code the Circuit Playground Express

Danger: 

You'll program your ornament to change colors based on the numbered buttons on the IR mini remote control by using CircuitPython.&nbsp;

## Get Ready!

First, make sure you're up and running by following these steps:

- First, make sure you're familiar with the basics of using CircuitPython on the Circuit Playground Express.&nbsp;[Follow this guide](https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-quickstart)&nbsp;to familiarize yourself.
- Then,&nbsp;[install CircuitPython on your board by](../../../../adafruit-circuit-playground-express/circuitpython-quick-install)&nbsp;[following these instructions](../../../../adafruit-circuit-playground-express/circuitpython-quick-install)
- Next,&nbsp;[install the library bundle onto your board as shown here](../../../../adafruit-circuit-playground-express/circuitpython-libraries). The libraries give us what we need to code easily with high level commands!

[Library Bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/latest)
Lighting up the NeoPixel ring in different colors is simple to do on the Circuit Playground Express using CircuitPython. In fact, it only takes a single line of code to make them all bright red:&nbsp;`pixels.fill(255, 0, 0) `will do the trick in the case of our program!&nbsp;

To control which colors we see, we can use can use the IR remote control along with IR library that makes it easy to decode the IR commands.

IR commands are actually messages that are sent by the remote when it blinks its infrared light rapidly with very precise timing and duration. It's a little bit like Morse code in that sense.&nbsp;

Using its IR sensor, the Circuit Playground Express can decode those messages sent by the remote. For more details on how this works, check out this great&nbsp;[IR sensor guide](../../../../ir-sensor/circuitpython).

So that you don't need to deal with the low level complexities of the decoding process, the **Adafruit IRRemote** library makes it simple!

## Download the Remote Tree Ornament Code

Download the code below by clicking 'Download Project Bundle.' Then, copy `code.py` over to the **CIRCUITPY** drive.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Remote_Tree_Ornament/code.py

After you've copied everything over, your **CIRCUITPY** drive should look something like this:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/Remote_Tree_Ornament.png)

The board will restart once the code has been saved. Now, point the remote at the board and press any number button 1-9. See how the lights change color depending on the button!

## Code Tour

Let's have a look at how the code works.&nbsp;

First, we import a few libraries that give us easy commands to use for more complex tasks.

Danger: 

```auto
import adafruit_irremote
import board
import digitalio
import neopixel
import pulseio

```

Next, we'll do a bit of setup, creating the NeoPixel object named `pixels`, and the on-board indicator LED called `red_led`.

```auto
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10)

red_led = digitalio.DigitalInOut(board.D13)
red_led.direction = digitalio.Direction.OUTPUT

```

To prepare the IR pulse detection and decoding, we'll initialize these objects:

```auto
pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=120, idle_state=True)
decoder = adafruit_irremote.GenericDecode()

```

Next, we'll create a variable to store the decoded message number list:

```auto
received_code = bytearray(4)
```

## Messages
The messages will be sets of four numbers. Here's a list of which buttons correlate to these messages. We'll use this later in the code to know which button has been pressed.

```auto
# IR Remote Mapping
'''
 1: [255, 2, 247, 8]
 2: [255, 2, 119, 136]
 3: [255, 2, 183, 72]
 4: [255, 2, 215, 40]
 5: [255, 2, 87, 168]
 6: [255, 2, 151, 104]
 7: [255, 2, 231, 24]
 8: [255, 2, 103, 152]
 9: [255, 2, 167, 88]
 0: [255, 2, 207, 48]

^ : [255, 2, 95, 160]
v : [255, 2, 79, 176]
> : [255, 2, 175, 80]
< : [255, 2, 239, 16]

Enter: [255, 2, 111, 144]
Setup: [255, 2, 223, 32]
Stop/Mode: [255, 2, 159, 96]
Back: [255, 2, 143, 112]

Vol - : [255, 2, 255, 0]
Vol + : [255, 2, 191, 64]

Play/Pause: [255, 2, 127, 128]
'''
```

Info: 

## Color Names
Next, we'll create a few variables that make it easier to call out colors by name. The colors of the NeoPixels are described with red, green, and blue values from 0-255.

```auto
RED = (255, 0, 0)
GREEN = (0, 255, 0)
WHITE = (85, 85, 85)
BLUE = (0, 0, 255)
PINK = (128, 0, 128)
YELLOW = (148, 108, 0)
PURPLE = (200, 0, 55)
TEAL = (0, 200, 100)
ORANGE = (100, 45, 0)
BLACK = (0, 0, 0)

```

We'll also create a variable to store the value of the last command decoded from the remote, so that we can compare commands to the previous one to know if anything has to change.

```auto
last_command = None

```

## Main Loop

Now we get to the main loop that runs over and over after the line:

`while True:`

The first thing we do is turn the red LED off. This way, when an IR command is received later, the LED can be turned on to indicate this. In the absence of that, the LED will turn back off.

`red_led.value = False`

This next section is where the IR pulses are detected, and then tested to see if they conform to the expected protocol. If so, the decoded pulses are turned into a message we can use.

```auto
    try:
        pulses = decoder.read_pulses(pulsein)
    except MemoryError as e:
        print("Memory error: ", e)
        continue
    red_led.value = True
    print("Heard", len(pulses), "Pulses:", pulses)
    command = None
    try:
        code = decoder.decode_bits(pulses, debug=False)
        if len(code) > 3:
            command = code[2]
        print("Decoded:", code)
    except adafruit_irremote.IRNECRepeatException:  # unusual short code!
        print("NEC repeat!")
        command = last_command
    except adafruit_irremote.IRDecodeException as e:     # failed to decode
        print("Failed to decode:", e)
    except MemoryError as e:
        print("Memory error: ", e)

    if not command:
        continue
    last_command = command

    print("----------------------------")
    red_led.value = False
```

## Color Commands

Lastly, we'll check the received commands against a list -- if the command corresponds to one of them, the code to change the NeoPixel colors is run.

```auto
    if command == 247:  # IR button 1
        pixels.fill(RED)
    elif command == 119:  # 2
        pixels.fill(GREEN)
    elif command == 183:  # 3
        pixels.fill(WHITE)
    elif command == 215:  # 4
        pixels.fill(BLUE)
    elif command == 87:  # 5
        pixels.fill(PINK)
    elif command == 151:  # 6
        pixels.fill(YELLOW)
    elif command == 231:  # 7
        pixels.fill(PURPLE)
    elif command == 103:  # 8
        pixels.fill(TEAL)
    elif command == 167:  # 9
        pixels.fill(ORANGE)
    elif command == 207:
        pixels.fill(BLACK)  # 0/10+

```

Next, we'll build the ornament and use it!

# Remote Control Tree Ornament with Circuit Playground Express

## Assemble and Use the Ornament

## Fill the Ornament

First, you'll want to tie on the hanging ribbon or hook, and separate the two halves of the clear ornament ball.

![](https://cdn-learn.adafruit.com/assets/assets/000/049/625/medium800/circuitpython_IMG_6997.jpg?1514071882)

Next, fill one half with the pom poms. They will look festive, and help to diffuse the strong glow of the NeoPixels on the Circuit Playground Express.

![](https://cdn-learn.adafruit.com/assets/assets/000/049/626/medium800/circuitpython_IMG_6996.jpg?1514071932)

Plug the battery pack's JST cable into the Circuit Playground Express, and then turn on the on switch.

![](https://cdn-learn.adafruit.com/assets/assets/000/049/627/medium800/circuitpython_IMG_6993.jpg?1514072005)

Place the CPX and battery pack on top of the pom poms. Looks comfy!

![](https://cdn-learn.adafruit.com/assets/assets/000/049/628/medium800/circuitpython_IMG_6992.jpg?1514072106)

Now, close up the ornament. It's ready for testing with the remote, and then for hanging on the tree!

![](https://cdn-learn.adafruit.com/assets/assets/000/049/630/medium800/circuitpython_IMG_6990.jpg?1514072159)

![](https://cdn-learn.adafruit.com/assets/assets/000/049/631/medium800/circuitpython_IMG_7006.jpg?1514072194)

## Use the Remote

The IR signal needs to make it to the IR sensor on the CPX, so you may need to try it from different angles. It has the benefit of bouncing off of lots of surfaces, so it isn't hard to make it work from nearby. Without a direct line of sight, it's harder to control it from great distances, but definitely possible!

![](https://cdn-learn.adafruit.com/assets/assets/000/049/632/medium800/circuitpython_IMG_6987.jpg?1514072349)

![](https://cdn-learn.adafruit.com/assets/assets/000/049/633/medium800/circuitpython_IMG_6989.jpg?1514072362)

Try pressing the different number keys on the remote to see the different colors!

![](https://cdn-learn.adafruit.com/assets/assets/000/049/634/medium800/circuitpython_IMG_7008.jpg?1514072371)

![](https://cdn-learn.adafruit.com/assets/assets/000/049/635/medium800/circuitpython_IMG_7010.jpg?1514072379)

Now, you may hang it on the tree with care! When it's not in use you can press 0 to turn off the NeoPixels which will preserve battery life. You can also open the ornament to flip off the switch for total energy savings.

Enjoy your festive ornament!

![](https://cdn-learn.adafruit.com/assets/assets/000/049/636/medium800thumb/circuitpython_IMG_6985.jpg?1514072476)


## Guide 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...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### 3 x AAA Battery Holder with On/Off Switch, JST, and Belt Clip

[3 x AAA Battery Holder with On/Off Switch, JST, and Belt Clip](https://www.adafruit.com/product/3286)
This battery holder connects 3 AAA batteries together in series for powering all kinds of projects. We spec'd these out because the box is slim, and 3 AAA's add up to about 3.3-4.5V, a very similar range to Lithium Ion/polymer (Li-Ion) batteries, plus it has&nbsp;a nifty&nbsp;on-off...

In Stock
[Buy Now](https://www.adafruit.com/product/3286)
[Related Guides to the Product](https://learn.adafruit.com/products/3286/guides)
### Alkaline AAA batteries - 3 pack

[Alkaline AAA batteries - 3 pack](https://www.adafruit.com/product/3520)
Battery power for your portable project! These batteries are good quality at a good price, and work fantastic with any of the kits or projects in the shop that use AAA's. This is a pack of **3 AAA batteries**.  
  
These batteries are Alkaline (MnO2) chemistry, with a...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3520)
[Related Guides to the Product](https://learn.adafruit.com/products/3520/guides)
### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

In Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)
### Circuit Playground Express - Base Kit

[Circuit Playground Express - Base Kit](https://www.adafruit.com/product/3517)
It's the **Circuit Playground Express Base Kit!** &nbsp;It provides&nbsp;the few things you'll need to get started with the new [Circuit Playground Express](https://www.adafruit.com/product/3333).&nbsp;This version of Circuit Playground is super powered, and will...

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

[Mini Remote Control](https://www.adafruit.com/product/389)
This little remote control would be handy for controlling a robot or other project from across the room. It has 21 buttons and a layout we thought was handy: directional buttons and number entry buttons. The remote uses the NEC encoding type and sends data codes 0 thru 26 (it skips #3, #7,...

In Stock
[Buy Now](https://www.adafruit.com/product/389)
[Related Guides to the Product](https://learn.adafruit.com/products/389/guides)
### DIY Ornament Kit - 6cm Diameter - Perfect for Circuit Playground

[DIY Ornament Kit - 6cm Diameter - Perfect for Circuit Playground](https://www.adafruit.com/product/4036)
Have you put up with mainstream, uninspiring, low-tech tree ornaments for too long? This season why not deck the halls with _codes_ of holly?

This **DIY Ornament Kit** is just the right size (6 cm diameter) for a Circuit Playground Express, 500mAh LiPoly battery, and...

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

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [CLUE Metal Detector in CircuitPython](https://learn.adafruit.com/clue-metal-detector-circuitpython.md)
- [LED Shoji Lamp](https://learn.adafruit.com/led-shoji-lamp.md)
- [CircuitPython Audio FX](https://learn.adafruit.com/circuitpython-audio-fx.md)
- [How to Hack NES ROMs to Add Your Own Sprites](https://learn.adafruit.com/how-to-hack-roms-to-add-your-own-sprites.md)
- [Adafruit IO Basics: Schedule Actions](https://learn.adafruit.com/adafruit-io-basics-scheduled-triggers.md)
- [How to Choose a Microcontroller](https://learn.adafruit.com/how-to-choose-a-microcontroller.md)
- [Grand Central Soundboard in Ten Minutes](https://learn.adafruit.com/grand-central-soundboard-in-ten-minutes.md)
- [Using EduBlocks with Circuit Playground Express](https://learn.adafruit.com/using-edublocks-with-circuit-playground-express.md)
- [Raspberry Pi SelfieBlock](https://learn.adafruit.com/selfieblock.md)
- [Illuminated City Skyline with MakeCode](https://learn.adafruit.com/city-skyline-with-makecode-for-cpx.md)
- [Making oscilloscope images with DACs](https://learn.adafruit.com/dac-oscilloscope-images.md)
- [Steven Universe Cosplay Shirt & Gem](https://learn.adafruit.com/steven-universe-cosplay-shirt-gem.md)
- [Infinity Mirror Valentine's Candy Box](https://learn.adafruit.com/infinity-mirror-candy-box.md)
- [PyPortal Wake-Up Light Alarm Clock](https://learn.adafruit.com/pyportal-wake-up-light.md)
- [Lucky Cat with Circuit Playground Express](https://learn.adafruit.com/lucky-cat-with-circuit-playground-express.md)
