# FunHouse Motion Detecting Lights with LIFX Bulbs

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/101/924/medium800thumb/leds_funLIFX.jpg?1619632370)

You can build your own motion detection system to control your room lighting, using the FunHouse, LIFX WiFi light bulbs, and CircuitPython.

Set up a high tech night light. Or a darkroom warning light. Or a psychedelic chill-out zone. All of these things are possible with the mini PIR sensor that detects the presence of a person (or other large IR heat source) and flips the bulb to the color and brightness you decide.

Also uses the FunHouse buttons and display to pause and arm the system as well as to adjust the active timer.

## Parts
### Adafruit FunHouse - WiFi Home Automation Development Board

[Adafruit FunHouse - WiFi Home Automation Development Board](https://www.adafruit.com/product/4985)
Home is where the heart is...it's also where we keep all our electronic bits. So why not wire it up with sensors and actuators to turn our house into an electronic wonderland. Whether it's tracking the environmental temperature and humidity in your laundry room, or notifying you when...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4985)
[Related Guides to the Product](https://learn.adafruit.com/products/4985/guides)
![Top-down video of Adafruit Funhouse PCB. The TFT display shows a data readout, and the NeoPixel LEDs glow rainbow colors.](https://cdn-shop.adafruit.com/product-videos/640x480/4985-00.jpg)

### Breadboard-friendly Mini PIR Motion Sensor with 3 Pin Header

[Breadboard-friendly Mini PIR Motion Sensor with 3 Pin Header](https://www.adafruit.com/product/4871)
PIR sensors are used to detect motion from pets/humanoids from about 5 meters away (possibly works on zombies, not guaranteed). This sensor is much smaller than most PIR modules, which makes it great for unobtrusive projects. It's also fully-contained - only needs power and ground, and...

In Stock
[Buy Now](https://www.adafruit.com/product/4871)
[Related Guides to the Product](https://learn.adafruit.com/products/4871/guides)
![Topdown video of 3-pin PIR sensor assembled onto a breadboard. A hand passes over the sensor, and a blue LED to light up.](https://cdn-shop.adafruit.com/product-videos/640x480/4871-04.jpg)

## LIFX WiFi Light Bulb

WiFi enabled, hub-less, color/color-temperature changing [smart bulb by LIFX](https://www.lifx.com/collections/lamps-and-pendants/products/lifx-color) (there are other WiFi bulbs out there, but this project is specifically coded for LIFX brand bulbs).

Any of their bulbs will work, and you can modify the code to control multiple bulbs if you like.

![leds_00_1PACK_LIFX_Color_Image_Hero_US_500px_540x.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/889/medium640/leds_00_1PACK_LIFX_Color_Image_Hero_US_500px_540x.jpg?1619621067)

### USB Type A to Type C Cable - approx 1 meter / 3 ft long

[USB Type A to Type C Cable - approx 1 meter / 3 ft long](https://www.adafruit.com/product/4474)
As technology changes and adapts, so does Adafruit. This&nbsp;&nbsp; **USB Type A to Type C** cable will help you with the transition to USB C, even if you're still totin' around a USB Type A hub, computer or laptop.

USB C is the latest industry-standard connector for...

In Stock
[Buy Now](https://www.adafruit.com/product/4474)
[Related Guides to the Product](https://learn.adafruit.com/products/4474/guides)
![Angled shot of a coiled black, USB-C to USB-A cable.](https://cdn-shop.adafruit.com/640x480/4474-02.jpg)

### 5V 2A Switching Power Supply w/ USB-A Connector

[5V 2A Switching Power Supply w/ USB-A Connector](https://www.adafruit.com/product/1994)
Our 5V 2A USB 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 adapter. We did this on purpose to...

In Stock
[Buy Now](https://www.adafruit.com/product/1994)
[Related Guides to the Product](https://learn.adafruit.com/products/1994/guides)
![5V 2A Switching Power Supply with  USB-A Connector](https://cdn-shop.adafruit.com/640x480/1994-01.jpg)

# FunHouse Motion Detecting Lights with LIFX Bulbs

## LIFX Bulb Setup

The LIFX bulbs have a robust API and developer support. In order to use them in your own projects, follow these steps.

## Set up bulb with LIFX app

Screw the bulb into a light socket and power it on.

Follow the instructions that came with the bulb for using with the LIFX mobile app for iOS and Android. This process will get your bulb connected to your WiFi, and allow you to control the bulb directly from your mobile device.

During the setup, name your bulb **Lamp** so that it will be the same name used in our CircuitPython code.

![leds_IMG_1164.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/899/medium640/leds_IMG_1164.jpg?1619624296)

![leds_default.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/901/medium640/leds_default.jpg?1619624486)

## LIFX Cloud API Access Token

Next, you'll sign in to the [LIFX Cloud](https://cloud.lifx.com/sign_in). (You'll use the same account credentials you set up with the mobile app.)

In the account settings page, click on **Generate New Token** to create your HTTP API access token, which will be used in the **secrets.py** file on your FunHouse in order to communicate with the bulb. You can read about it [here in the LIFX Developer Zone](https://api.developer.lifx.com/docs/authentication).

![proximity_token.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/932/medium640/proximity_token.jpg?1619635215)

Write down this token in a secure place, you'll need it later.

Warning: 

## Test the Connection

Now that you have your token, you can try out a quick test from a command line terminal. Copy and paste the following, substituting the `YOUR_APP_TOKEN` for your unique token instead:

`curl -H 'Authorization: Bearer YOUR_APP_TOKEN' -X POST 'https://api.lifx.com/v1/lights/label:Lamp/toggle'`

![](https://cdn-learn.adafruit.com/assets/assets/000/101/933/medium800/proximity_curl.jpg?1619635450)

Press enter and your light will toggle off or on each time you repeat the command, and the light will report its status.

![proximity_curlresult.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/934/medium640/proximity_curlresult.jpg?1619635651)

# FunHouse Motion Detecting Lights with LIFX Bulbs

## Add the Mini PIR Sensor

## Align the Polarity

Adding the mini PIR sensor to the FunHouse is pretty simple, you just need to check the polarity.

Look for the + symbol on the PIR sensor's PCB and match it up with the + symbol on the front side of the FunHouse board's PIR row.

![leds_IMG_1165.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/902/medium640/leds_IMG_1165.jpg?1619628094)

![leds_IMG_1166.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/903/medium640/leds_IMG_1166.jpg?1619628111)

## Insert Sensor

Align the pins with the socket holes on the front side of the board.

The first time you insert the sensor the fit is tight, but just apply firm down ward pressure while wiggling the board a little bit and it will go in just fine.

![leds_IMG_1167.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/904/medium640/leds_IMG_1167.jpg?1619628259)

![leds_IMG_1168.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/905/medium640/leds_IMG_1168.jpg?1619628347)

![](https://cdn-learn.adafruit.com/assets/assets/000/101/906/medium800/leds_IMG_1169.jpg?1619628377)

# FunHouse Motion Detecting Lights with LIFX Bulbs

## Code the FunHouse Lighting for LIFX

## Install CircuitPython

The first think to do is install CircuitPython on your Funhouse. [Follow this guide](https://learn.adafruit.com/adafruit-funhouse/circuitpython) to get it set up with the latest release version.

## Shhhh... Secrets

In order for the Funhouse to connect to the internet, you'll need to include a **settings.toml** file on the board that contains your WiFi access point ssid and password, as well as your AIO key.

[Follow this guide page](https://learn.adafruit.com/adafruit-funhouse/circuitpython-internet-test) to get your **settings.toml** file set up.

## Add LIFX Token to settings.toml

Add your LIFX token to the **settings.toml** file on it's own line. It should look something like this:

```python
# This file is where you keep private settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

CIRCUITPY_WIFI_SSID="your-wifi-ssid"
CIRCUITPY_WIFI_PASSWORD="your-wifi-password"
lifx_token="YOUR_APP_TOKEN"
```

## Text Editor

Adafruit recommends using the Mu editor for using your CircuitPython code with the Funhouse. You can get more info in [this guide](https://learn.adafruit.com/welcome-to-circuitpython/installing-mu-editor).

## Download the Project Bundle

Your project will use a specific set of CircuitPython libraries and the **code.py** file. In order to get the libraries you need, click on the **Download Project Bundle** link below, and uncompress the .zip file.

Next, drag the contents of the uncompressed bundle directory onto you microcontroller board **CIRCUITPY** drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.

![leds_bundle_drag.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/890/medium640/leds_bundle_drag.jpg?1619623170)

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

# FunHouse Motion Detecting Lights with LIFX Bulbs

## Code Walkthrough

## How It Works

Here's how the code works.

### Libraries

First, you'll import the libraries needed including `time`, `ssl`, `socketpool`, `wifi`, and `adafruit_requests` for creating the connection to the bulb, `adafruit_funhouse` for all of the convenient functions for using the sensors, display, NeoPixels, and buttons on the FunHouse easily, and the `adafruit_lifx` library for specific bulb functions.

```python
import time
import ssl
import socketpool
import wifi
import adafruit_requests
from adafruit_funhouse import FunHouse
import adafruit_lifx
```

### WiFi Setup

Next, the code checks the **settings.toml** file for the WiFi credentials.

```python
# Get WiFi details, ensure these are setup in settings.toml
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")

if None in [ssid, password]:
    raise RuntimeError(
        "WiFi settings are kept in settings.toml, "
        "please add them there. The settings file must contain "
        "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
        "at a minimum."
    )
```

### Color Variables

You can pick your standby and active (sensor has been "tripped") colors here. Note that these are hexidecimal color values formatted two ways for the bulb, which uses HTML style hex formatting, and the FunHouse DotStars, which use more standard hex code formatting.

```python
default_bulb_color = "#002010"
default_led_color = 0x002010
tripped_bulb_color = "#440044"
tripped_led_color = 0x440044
```

### Requests Session

Here the ESP32-S2 sets up a WiFi HTTP socket session.

```python
wifi.radio.connect(ssid, password)
pool = socketpool.SocketPool(wifi.radio)
http_session = adafruit_requests.Session(pool, ssl.create_default_context())
```

### LIFX Setup

Next you'll set up the LIFX bulb using your token from the **settings.toml** file, the bulb name, and the http session. The `lights` variable is set by querying the LIFX light list.

```python
lifx_token = getenv("lifx_token")
lifx_light = "label:Lamp"
lifx = adafruit_lifx.LIFX(http_session, lifx_token)
lights = lifx.list_lights()
```

### FunHouse Setup

The FunHouse object is created, setting a background color for the screen and a text scale of 3 and the DotStars are turned on to their default color.

Three variables are created to store the PIR sensor state, the running/paused state, and the initial on timer value.

```python
funhouse = FunHouse(default_bg=0x000F20, scale=3)

pir_state = 0
running_state = False
trip_time = 30  # seconds to stay tripped, adjust this with buttons while running

funhouse.peripherals.dotstars.fill(default_led_color)
```

### Text Display

Next the screen labels are set up, including a function for toggling label colors when buttons are pressed.

```python
def set_label_color(conditional, index, on_color):
    if conditional:
        funhouse.set_text_color(on_color, index)
    else:
        funhouse.set_text_color(0x606060, index)


# Create the labels
funhouse.display.root_group = CIRCUITPYTHON_TERMINAL
up_label = funhouse.add_text(text="+", text_position=(3, 6), text_color=0x606060)
down_label = funhouse.add_text(text="-", text_position=(3, 40), text_color=0x606060)
running_label = funhouse.add_text(
    text="paused", text_position=(2, 68), text_color=0x606060
)
time_label = funhouse.add_text(
    text=trip_time, text_scale=2, text_position=(30, 25), text_color=0x606060
)

funhouse.display.root_group = funhouse.graphics.root_group
```

### Turn on the Light

The final step of setup is to turn on the LIFX bulb with a brightness of 65% and the default teal color.

```python
lifx.toggle_light(lifx_light)
light_brightness = 0.65
lifx.set_brightness(lifx_light, light_brightness)
lifx.set_color(
    lifx_light, power="on", color=default_bulb_color, brightness=light_brightness
)
```

### Main Loop

The main loop of the program does the following:

- When the top button is pressed, increase the timer variable and highlight the text
- When the middle button is pressed, decrease the timer variable and highlight the text
- When the bottom button is pressed toggle the `running_state` value and swap the text
- Watch for the PIR sensor value to change -- if the sensor is tripped, set the LIFX `set_color` and the DotStars to the "tripped" color, and then countdown the timer. When the timer runs down, return to the default color.

```python
while True:

    if funhouse.peripherals.button_up:
        trip_time = trip_time + 1
        funhouse.set_text(trip_time, time_label)
        funhouse.set_text_color(0xFFFFFF, up_label)
        time.sleep(0.2)
    else:
        funhouse.set_text_color(0x606060, up_label)

    if funhouse.peripherals.button_sel:
        trip_time = abs(trip_time - 1)
        funhouse.set_text(trip_time, time_label)
        funhouse.set_text_color(0xFFFFFF, down_label)
        time.sleep(0.2)
    else:
        funhouse.set_text_color(0x606060, down_label)

    if funhouse.peripherals.button_down:
        if running_state is False:  # it's currently paused, so unpause it
            running_state = True  # flip the state
            funhouse.set_text("..prepping..", running_label)
            time.sleep(6)  # pause to get out of range
            funhouse.set_text("sensing...", running_label)

        else:  # it's currently running, so pause it
            running_state = False
            funhouse.set_text("paused", running_label)
            time.sleep(0.5)

    # when sensor is tripped, set the color x amount of time
    if running_state is True and funhouse.peripherals.pir_sensor and pir_state is 0:
        funhouse.peripherals.dotstars.fill(tripped_led_color)
        funhouse.set_text("tripped", running_label)
        lifx.set_color(
            lifx_light,
            power="on",
            color=tripped_bulb_color,
            brightness=light_brightness,
        )
        prior_trip_time = trip_time  # store the state of the trip time value
        for _ in range(trip_time):
            time.sleep(1)
            trip_time = trip_time - 1
            funhouse.set_text(trip_time, time_label)
        pir_state = 1
        trip_time = prior_trip_time  # restore the trip time value

    # return to default color
    elif (
            running_state is True and not funhouse.peripherals.pir_sensor and pir_state is 1
    ):
        funhouse.peripherals.dotstars.fill(default_led_color)
        funhouse.set_text("sensing...", running_label)
        lifx.set_color(
            lifx_light,
            power="on",
            color=default_bulb_color,
            brightness=light_brightness,
        )
        funhouse.set_text(trip_time, time_label)
        pir_state = 0
```

# FunHouse Motion Detecting Lights with LIFX Bulbs

## Use the FunHouse Lighting for LIFX Sensor

![](https://cdn-learn.adafruit.com/assets/assets/000/101/911/medium800thumb/leds_funLIFX.jpg?1619631816)

Here's how to use the FunHouse Lighting for LIFX controller.

## Mount the FunHouse

Mount the FunHouse to your wall using the mounting plate, or rest it on a desk or other surface. Make sure the mini PIR sensor has a clear view of the area you want to motion detect for people entering the space.

Turn on the LIFX bulb switch.

![leds_funLIFX-1170.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/912/medium640/leds_funLIFX-1170.jpg?1619631887)

## Startup

The FunHouse will start up and turn on the LIFX bulb to the default color set in code.

![leds_funLIFX-1171.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/913/medium640/leds_funLIFX-1171.jpg?1619631985)

## Adjust Timer and Arm Sensor

You can use the top and middle buttons on the FunHouse to adjust the timer.

When ready to use the sensor, press the bottom button, which will change the display from **paused** to **..prepping..** and then to **sensing...**

It is now sensing for big IR sources, such as, well, you!

![leds_funLIFX-1172.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/914/medium640/leds_funLIFX-1172.jpg?1619632020)

![leds_funLIFX-1173.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/915/medium640/leds_funLIFX-1173.jpg?1619632035)

![leds_funLIFX-1174.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/916/medium640/leds_funLIFX-1174.jpg?1619632042)

![leds_funLIFX-1175.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/917/medium640/leds_funLIFX-1175.jpg?1619632048)

## Motion Detected

When a person is detected the Dotstars will change to the tripped color and the command will be sent over WiFi to the LIFX bulb (via the Internet).

The tripped color will remain while the timer counts down.

![leds_funLIFX-1176.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/918/medium640/leds_funLIFX-1176.jpg?1619632171)

![leds_funLIFX-1178.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/919/medium640/leds_funLIFX-1178.jpg?1619632186)

![leds_funLIFX-1180.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/920/medium640/leds_funLIFX-1180.jpg?1619632202)

![leds_funLIFX-1185.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/921/medium640/leds_funLIFX-1185.jpg?1619632217)

## No Motion Detected

Once the timer has run down and no motion is detected, the bulb will return to the default color.

![leds_funLIFX-1186.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/922/medium640/leds_funLIFX-1186.jpg?1619632315)

![leds_funLIFX-1187.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/923/medium640/leds_funLIFX-1187.jpg?1619632321)


## Featured Products

### Adafruit FunHouse - WiFi Home Automation Development Board

[Adafruit FunHouse - WiFi Home Automation Development Board](https://www.adafruit.com/product/4985)
Home is where the heart is...it's also where we keep all our electronic bits. So why not wire it up with sensors and actuators to turn our house into an electronic wonderland. Whether it's tracking the environmental temperature and humidity in your laundry room, or notifying you when...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4985)
[Related Guides to the Product](https://learn.adafruit.com/products/4985/guides)
### Breadboard-friendly Mini PIR Motion Sensor with 3 Pin Header

[Breadboard-friendly Mini PIR Motion Sensor with 3 Pin Header](https://www.adafruit.com/product/4871)
PIR sensors are used to detect motion from pets/humanoids from about 5 meters away (possibly works on zombies, not guaranteed). This sensor is much smaller than most PIR modules, which makes it great for unobtrusive projects. It's also fully-contained - only needs power and ground, and...

In Stock
[Buy Now](https://www.adafruit.com/product/4871)
[Related Guides to the Product](https://learn.adafruit.com/products/4871/guides)
### USB Type A to Type C Cable - approx 1 meter / 3 ft long

[USB Type A to Type C Cable - approx 1 meter / 3 ft long](https://www.adafruit.com/product/4474)
As technology changes and adapts, so does Adafruit. This&nbsp;&nbsp; **USB Type A to Type C** cable will help you with the transition to USB C, even if you're still totin' around a USB Type A hub, computer or laptop.

USB C is the latest industry-standard connector for...

In Stock
[Buy Now](https://www.adafruit.com/product/4474)
[Related Guides to the Product](https://learn.adafruit.com/products/4474/guides)
### 5V 2A Switching Power Supply w/ USB-A Connector

[5V 2A Switching Power Supply w/ USB-A Connector](https://www.adafruit.com/product/1994)
Our 5V 2A USB 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 adapter. We did this on purpose to...

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

## Related Guides

- [Adafruit FunHouse](https://learn.adafruit.com/adafruit-funhouse.md)
- [FunHouse 3D Printed Stand](https://learn.adafruit.com/funhouse-3d-printed-stand.md)
- [Funhouse Door Alert with Email Notification](https://learn.adafruit.com/funhouse-door-alert-email-notification.md)
- [Quickstart: Adafruit IO WipperSnapper ](https://learn.adafruit.com/quickstart-adafruit-io-wippersnapper.md)
- [Creating FunHouse Projects with CircuitPython](https://learn.adafruit.com/creating-funhouse-projects-with-circuitpython.md)
- [Motion Activated Outlet with the Adafruit FunHouse](https://learn.adafruit.com/motion-activated-outlet-with-the-adafruit-funhouse.md)
- [Hang out your washing reminder - Adafruit IO + Vibration switch](https://learn.adafruit.com/hang-out-your-washing-reminder-adafruit-io-vibration-switch.md)
- [Pet Bowl Water Level Sensing](https://learn.adafruit.com/pet-bowl-water-level-sensing-with-the-funhouse-and-home-assistant.md)
- [FunHouse IoT Fume Extractor and Air Quality Sensor](https://learn.adafruit.com/funhouse-fume-extractor-iot-air-quality-sensor.md)
- [No-Code WipperSnapper Water Detection](https://learn.adafruit.com/water-detection-with-wippersnapper.md)
- [Track a Turtle with WipperSnapper](https://learn.adafruit.com/track-a-turtle-with-wippersnapper.md)
- [Karel The Robot In CircuitPython](https://learn.adafruit.com/karel-the-robot-in-circuitpython.md)
- [Adafruit IO IOT Hub with the Adafruit FunHouse](https://learn.adafruit.com/adafruit-io-hub-with-the-adafruit-funhouse.md)
- [No-Code WipperSnapper IoT Power Switch Outlet](https://learn.adafruit.com/no-code-wippersnapper-iot-power-switch-outlet.md)
- [Using the Adafruit FunHouse with Home Assistant](https://learn.adafruit.com/using-the-adafruit-funhouse-with-home-assistant.md)
- [CircuitPython Web Workflow Code Editor Quick Start](https://learn.adafruit.com/getting-started-with-web-workflow-using-the-code-editor.md)
