The code is simple, like the hardware. That's one reason to use CircuitPython: the resulting code can be far simpler than the equivalent code in C++.  

Are you new to using CircuitPython? No worries, there is a full getting started guide here.

Adafruit suggests using the Mu editor to edit your code and have an interactive REPL in CircuitPython. You can learn about Mu and installation in this tutorial.

The code needs three libraries to be in the CIRCUITPY drive's lib directory (which you may have to create):

  • adafruit_bus_device (a directory)
  • adafruit_vl53l0x.mpy
  • neopixel.mpy

If you haven't installed CircuitPython libraries before, the Welcome to CircuitPython guide has a page that covers it in detail.

First, the distance sensor and Neopixel strip are set up. The loop continuously checks the distance reading, and if it's close enough (half a meter in my code) the Neopixels are turned on, otherwise they are turned off. I put in a 100 milisecond delay so it wasn't running the distance sensor non-stop.

As I said, the code is simple, and there are plenty of ways to extend it. If used for presence aware lighting effects, you can make use of the color capabilities of the Neopixels. You could set color based on how far away someone is, or change an animation, its speed, or any manner of things. 

That's what makes this a fun project: simple hardware, simple code, but plenty of possibilities.

# SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import busio
import neopixel
import adafruit_vl53l0x

TRIGGER_DISTANCE = 500          # number of mm where the light toggles

i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_vl53l0x.VL53L0X(i2c)

strip = neopixel.NeoPixel(NEOPIXEL_PIN, NUMBER_OF_PIXELS, brightness=1.0, auto_write=False)

strip.fill((0, 0, 0))
for i in range(NUMBER_OF_PIXELS):
    strip[i] = (64, 64, 64)
    strip[i] = (0, 0, 0)

while True:
    if sensor.range < TRIGGER_DISTANCE:
        strip.fill((255, 255, 255))
        strip.fill((0, 0, 0))

This guide was first published on Jun 13, 2018. It was last updated on Nov 29, 2023.

This page (Code) was last updated on Nov 29, 2023.

Text editor powered by tinymce.