# Bluefruit Playground Hide and Seek

## Overview

https://youtu.be/Nh5yv8EkcQ8

Stage a creative gift hunt all around your home with Bluetooth LE tracking ornaments! Circuit Playground Bluefruit boards can hone in on the proximity of other boards broadcasting their own signal including color coding! The stronger the signal, the more NeoPixels light up. All coded with CircuitPython.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/277/medium800/circuitpython_IMG_9832.jpg?1575409076)

## Parts

The more the merrier here! Get together with some friends or a whole classroom full of Circuit Playground Bluefruits. You'll need at least two CPBs to test the signal strength measurment aspect of this project, and three or more to grab the hidden colors.

Featured
### Circuit Playground Bluefruit - Bluetooth® Low Energy

[Circuit Playground Bluefruit - Bluetooth® Low Energy](https://www.adafruit.com/product/4333)
 **Circuit Playground Bluefruit** is our third board in the Circuit Playground series, another step towards a perfect introduction to electronics and programming. We've taken the popular Circuit Playground Express and made it even better! Now the main chip is an nRF52840...

In Stock
[Buy Now](https://www.adafruit.com/product/4333)
[Related Guides to the Product](https://learn.adafruit.com/products/4333/guides)
![shot of a Black woman's neon-green manicured hand holding up a Circuit Playground Bluefruit glowing rainbow LEDs.](https://cdn-shop.adafruit.com/640x480/4333-11.jpg)

### Circuit Playground Bluefruit - Bluetooth® Low Energy

quantity: 3
[Circuit Playground Bluefruit - Bluetooth® Low Energy](https://www.adafruit.com/product/4333)
 **Circuit Playground Bluefruit** is our third board in the Circuit Playground series, another step towards a perfect introduction to electronics and programming. We've taken the popular Circuit Playground Express and made it even better! Now the main chip is an nRF52840...

In Stock
[Buy Now](https://www.adafruit.com/product/4333)
[Related Guides to the Product](https://learn.adafruit.com/products/4333/guides)
![shot of a Black woman's neon-green manicured hand holding up a Circuit Playground Bluefruit glowing rainbow LEDs.](https://cdn-shop.adafruit.com/640x480/4333-11.jpg)

### Lithium Ion Polymer Battery with Short Cable - 3.7V 350mAh

quantity: 3
[Lithium Ion Polymer Battery with Short Cable - 3.7V 350mAh](https://www.adafruit.com/product/4237)
Lithium-ion polymer (also known as 'lipo' or 'lipoly') batteries are thin, light, and powerful. The output ranges from 4.2V when completely charged to 3.7V. This battery has a capacity of 350mAh for a total of about 1.3 Wh. If you need a larger (or smaller!) battery, <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/4237)
[Related Guides to the Product](https://learn.adafruit.com/products/4237/guides)
![Lithium Ion Polymer Battery 3.7v 350mAh with JST 2-PH connector and short cable](https://cdn-shop.adafruit.com/640x480/4237-04.jpg)

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

quantity: 3
[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)

### Adafruit Circuit Playground Express or Bluefruit Enclosure

quantity: 3
[Adafruit Circuit Playground Express or Bluefruit Enclosure](https://www.adafruit.com/product/3915)
We've got nice cases for many of our beloved boards, but the [Circuit Playground Express](https://www.adafruit.com/product/3333)&nbsp;and [Circuit Playground&nbsp;Bluefruit](https://www.adafruit.com/product/4333) has been left out of couture enclosure...

In Stock
[Buy Now](https://www.adafruit.com/product/3915)
[Related Guides to the Product](https://learn.adafruit.com/products/3915/guides)
![Top down view of a clear acrylic Adafruit Circuit Playground Express or Bluefruit Enclosure.](https://cdn-shop.adafruit.com/product-videos/640x480/3915-06.jpg)

# Bluefruit Playground Hide and Seek

## CircuitPython on Circuit Playground Bluefruit

# Install or Update CircuitPython

Follow this quick step-by-step to install or update CircuitPython on your Circuit Playground Bluefruit.

[Download the latest version of CircuitPython for this board via circuitpython.org](https://circuitpython.org/board/circuitplayground_bluefruit/)
 **Click the link above and download the latest UF2 file**

Download and save it to your Desktop (or wherever is handy)

![adafruit_products_CPB_Download_UF2.png](https://cdn-learn.adafruit.com/assets/assets/000/080/530/medium640/adafruit_products_CPB_Download_UF2.png?1567715178)

Plug your Circuit Playground Bluefruit into your computer using a known-good data-capable USB cable.

**A lot of people end up using charge-only USB cables and it is very frustrating! So make sure you have a USB cable you know is good for data sync.**

Double-click the small **Reset** button in the middle of the CPB (indicated by the red arrow in the image). The ten NeoPixel LEDs will all turn red, and then will all turn green. If they turn all red and stay red, check the USB cable, try another USB port, etc. The little red LED next to the USB connector will pulse red - this is ok!

If double-clicking doesn't work the first time, try again. Sometimes it can take a few tries to get the rhythm right!

(If double-clicking doesn't do it, try a single-click!)

![adafruit_products_CPB_Front_Reset_Button_Arrow.jpg](https://cdn-learn.adafruit.com/assets/assets/000/080/532/medium640/adafruit_products_CPB_Front_Reset_Button_Arrow.jpg?1567715535)

You will see a new disk drive appear called **CPLAYBTBOOT**.

&nbsp;

&nbsp;

&nbsp;

Drag the **adafruit\_circuitpython\_etc.uf2** file to **CPLAYBTBOOT.**

![adafruit_products_CPB_CPLAYBTBOOT.png](https://cdn-learn.adafruit.com/assets/assets/000/080/533/medium640/adafruit_products_CPB_CPLAYBTBOOT.png?1567715858)

![adafruit_products_CBP_drag_UF2.png](https://cdn-learn.adafruit.com/assets/assets/000/080/534/medium640/adafruit_products_CBP_drag_UF2.png?1567715871)

The LEDs will turn red. Then, the **CPLAYBTBOOT** drive will disappear and a new disk drive called **CIRCUITPY** will appear.

That's it, you're done! :)

![adafruit_products_CBP_CIRCUITPY.png](https://cdn-learn.adafruit.com/assets/assets/000/080/535/medium640/adafruit_products_CBP_CIRCUITPY.png?1567716034)

# Bluefruit Playground Hide and Seek

## Code with CircuitPython

## The Mu Editor

Adafruit recommends using the free program Mu to edit your CircuitPython programs and save them on your Circuit Playground Bluefruit. You can use any text editor, but Mu has some handy features.

[See this page on the Circuit Playground Bluefruit guide](https://learn.adafruit.com/adafruit-circuit-playground-bluefruit/installing-mu-editor)&nbsp;on the steps used to install Mu.

## Installing Project Code

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your **CIRCUITPY** drive. Then you need to update **code.py** with the example script.

Thankfully, we can do this in one go. In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CPB\_Ornament\_Proximity/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

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

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

Here's how the code works.

## Library Import

First, we import the libraries we'll be using:

```python
import time
from adafruit_circuitplayground.bluefruit import cpb

from adafruit_ble import BLERadio
from adafruit_ble.advertising.adafruit import AdafruitColor
```

We'll be able to call on Circuit Playground Bluefruit board functions with the **cpb** command, including simplified ways to access the buttons and switch, as well as the on board red LED and the NeoPixels.

We're also setting up the Bluetooth LE radio and the package needed to advertise color values.

## Color List

Next, we create a list of color values (in hex):

```python
# The color pickers will cycle through this list with buttons A and B.
color_options = [0x110000,
                 0x111100,
                 0x001100,
                 0x001111,
                 0x000011,
                 0x110011,
                 0x111111,
                 0x221111,
                 0x112211,
                 0x111122]
```

## Bluetooth and NeoPixel Setup

The BLE radio is instantiated next, as well as the `AdafruitColor()` object to advertise the color value of the board.

Also, we'll use the `cpb.pixels.auto_write` command to set the pixel auto write to `False` this helps avoid flickering NeoPixels (thanks Roy!) and the NeoPixel fill color.

```python
ble = BLERadio()

i = 0
advertisement = AdafruitColor()
advertisement.color = color_options[i]
cpb.pixels.auto_write = False
cpb.pixels.fill(color_options[i])
```

## Main Loop

The main loop of the code happens in the `while True:` section.

### Switch Left to Broadcast

Here, we check the position of the switch by asking `if cpb.switch:` and if it is `True`, this means the switch is positioned to the left and the board will be in broadcast mode (the "hidden" ornaments are going to be the ones in broadcast mode).

Then, button presses are used to increment or decrement through the color list, and set the NeoPixels to that color.

Every half second, the board will advertise this color information over Bluetooth with the `ble.start_advertising(advertisement)` command.

```python
if cpb.switch:
        print("Broadcasting color")
        ble.start_advertising(advertisement)
        while cpb.switch:
            last_i = i
            if cpb.button_a:
                i += 1
            if cpb.button_b:
                i -= 1
            i %= len(color_options)
            if last_i != i:
                color = color_options[i]
                cpb.pixels.fill(color)
                cpb.pixels.show()
                print("New color {:06x}".format(color))
                advertisement.color = color
                ble.stop_advertising()
                ble.start_advertising(advertisement)
                time.sleep(0.5)
        ble.stop_advertising()
```

### Switch Right to Detect

In the case that the `cpb.switch` value is `False` (in the code, `not True`), this means it's flipped to the right and the board will be in listening/detect mode.

Now, we'll used the Bluetooth LE radio to scan the airwaves and use its received signal strength indicator (RSSI) capability to single out the board with the strongest signal.&nbsp;

The color entry being broadcast by the strongest (usually nearest) board is used in setting the NeoPixel color, while the number of pixels being lit is based on the signal strength.

```python
else:
        closest = None
        closest_rssi = -80
        closest_last_time = 0
        print("Scanning for colors")
        while not cpb.switch:
            for entry in ble.start_scan(AdafruitColor, minimum_rssi=-100, timeout=1):
                if cpb.switch:
                    break
                now = time.monotonic()
                new = False
                if entry.address == closest:
                    pass
                elif entry.rssi > closest_rssi or now - closest_last_time > 0.4:
                    closest = entry.address
                else:
                    continue
                closest_rssi = entry.rssi
                closest_last_time = now
                discrete_strength = min((100 + entry.rssi) // 5, 10)
                cpb.pixels.fill(0x000000)
                for i in range(0, discrete_strength):
                    cpb.pixels[i] = entry.color
                cpb.pixels.show()

            # Clear the pixels if we haven't heard from anything recently.
            now = time.monotonic()
            if now - closest_last_time > 1:
                cpb.pixels.fill(0x000000)
                cpb.pixels.show()
        ble.stop_scan()
```

Next, we'll put together the ornaments and use them in a scavenger hunt!

# Bluefruit Playground Hide and Seek

## Build and Use the Ornaments

## Assembly

This is the simplest build ever!

First place the CPB boards into their enclosures.

Plug in the batteries into the JST battery ports.

Once they turn on, move the selector switch to the left on three of them (these are the ones you'll hide) and to the right on one (this is the detector).

On the ones that will broadcast their colors, press the **A** button to cycle through and pick a unique color for each of the ornaments. (You can press **B** to go backwards though the color list).

The detector will indicate the color and signal strength of the ornament with the strongest signal.

Fold the battery over gently and place each board/case/battery bundle into an ornament.

![circuitpython_IMG_9828.jpg](https://cdn-learn.adafruit.com/assets/assets/000/085/281/medium640/circuitpython_IMG_9828.jpg?1575416420)

![circuitpython_IMG_9830.jpg](https://cdn-learn.adafruit.com/assets/assets/000/085/282/medium640/circuitpython_IMG_9830.jpg?1575416431)

![circuitpython_IMG_9831.jpg](https://cdn-learn.adafruit.com/assets/assets/000/085/283/medium640/circuitpython_IMG_9831.jpg?1575416441)

## Usage

Here's how to use the ornaments. First, place the three ornaments to hide a foot or two away from each other, the farther the better!

Then, move the detector around the space. You'll see the color change as well as the number of NeoPixels that light up indicating the nearest ornament.

![circuitpython_IMG_9842.jpg](https://cdn-learn.adafruit.com/assets/assets/000/085/284/medium640/circuitpython_IMG_9842.jpg?1575417077)

![circuitpython_IMG_9839.jpg](https://cdn-learn.adafruit.com/assets/assets/000/085/285/medium640/circuitpython_IMG_9839.jpg?1575417088)

![circuitpython_IMG_9840.jpg](https://cdn-learn.adafruit.com/assets/assets/000/085/286/medium640/circuitpython_IMG_9840.jpg?1575417095)

Now, you can hide the ornaments throughout your home, perhaps next to wrapped gifts. Then, send your scavenger hunter out on a mission to find them based on signal strength and color.

Hint, you can have multiple detector CPBs, just by flipping the switches to the right, in case you have more than one present hunter who wants to play. Maybe they are each assigned a color!

https://youtu.be/XnzKIlq0h3A

![](https://cdn-learn.adafruit.com/assets/assets/000/085/287/medium800thumb/circuitpython_hideornament.jpg?1575417777)


## Guide Products

### Circuit Playground Bluefruit - Bluetooth® Low Energy

[Circuit Playground Bluefruit - Bluetooth® Low Energy](https://www.adafruit.com/product/4333)
 **Circuit Playground Bluefruit** is our third board in the Circuit Playground series, another step towards a perfect introduction to electronics and programming. We've taken the popular Circuit Playground Express and made it even better! Now the main chip is an nRF52840...

In Stock
[Buy Now](https://www.adafruit.com/product/4333)
[Related Guides to the Product](https://learn.adafruit.com/products/4333/guides)
### Lithium Ion Polymer Battery with Short Cable - 3.7V 350mAh

[Lithium Ion Polymer Battery with Short Cable - 3.7V 350mAh](https://www.adafruit.com/product/4237)
Lithium-ion polymer (also known as 'lipo' or 'lipoly') batteries are thin, light, and powerful. The output ranges from 4.2V when completely charged to 3.7V. This battery has a capacity of 350mAh for a total of about 1.3 Wh. If you need a larger (or smaller!) battery, <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/4237)
[Related Guides to the Product](https://learn.adafruit.com/products/4237/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)
### Adafruit Circuit Playground Express or Bluefruit Enclosure

[Adafruit Circuit Playground Express or Bluefruit Enclosure](https://www.adafruit.com/product/3915)
We've got nice cases for many of our beloved boards, but the [Circuit Playground Express](https://www.adafruit.com/product/3333)&nbsp;and [Circuit Playground&nbsp;Bluefruit](https://www.adafruit.com/product/4333) has been left out of couture enclosure...

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

## Related Guides

- [Adafruit Circuit Playground Bluefruit](https://learn.adafruit.com/adafruit-circuit-playground-bluefruit.md)
- [LED Reactive Light-Up Hockey Puck in MakeCode](https://learn.adafruit.com/led-hockey-puck.md)
- [TFT Gizmo Turtle](https://learn.adafruit.com/tft-gizmo-turtle.md)
- [Bluefruit TFT Gizmo ANCS Notifier for iOS](https://learn.adafruit.com/ancs-gizmo.md)
- [MagicLight Bulb Color Mixer with Circuit Playground Bluefruit](https://learn.adafruit.com/magiclight-bulb-mixer.md)
- [Wirelessly Code your Bluetooth Device with CircuitPython](https://learn.adafruit.com/wirelessly-code-your-bluetooth-device-with-circuitpython.md)
- [CLUE Sensor Plotter in CircuitPython](https://learn.adafruit.com/clue-sensor-plotter-circuitpython.md)
- [Sound Reactive Rapunzel Hair](https://learn.adafruit.com/sound-reactive-rapunzel-hair.md)
- [CircuitPython TFT Candy Hearts](https://learn.adafruit.com/circuit-python-tft-gizmo-candy-hearts.md)
- [Kitty's Flowers - Bluetooth Wearable Brooches - Art by Physicist](https://learn.adafruit.com/flowers-bluetooth-wearable-brooches-art-by-physicist.md)
- [Wakanda Forever Game](https://learn.adafruit.com/wakanda-forever-game.md)
- [Mini Neon Sign Prop & n00ds Booster Case](https://learn.adafruit.com/nood-booster-case.md)
- [Magical Cardboard Craft Obsidian Sword](https://learn.adafruit.com/cardboard-obsidian-sword.md)
- [Meeting Time Keeper Stick](https://learn.adafruit.com/meeting-time-keeper-stick-with-cpx.md)
- [Circuit Playground TFT Gizmo Snow Globe](https://learn.adafruit.com/circuit-playground-tft-gizmo-snow-globe.md)
- [Turtle Graphics in CircuitPython on TFT Gizmo](https://learn.adafruit.com/turtle-graphics-gizmo.md)
