The Proximity Trinkey comes with the APDS9960 proximity sensor and two NeoPixel LEDs. You can easily use the proximity sensor to control the brightness of the LEDs.

NeoPixel and APDS9960 Location

Both the two NeoPixel LEDs (highlighted in green in the image) and the APDS9960 proximity sensor (highlighted in red in the image) towards the opposite end of the board from the USB connector, with the proximity sensor nestled between the two NeoPixel LEDs.

Controlling NeoPixel Brightness

The NeoPixel library is already built into CircuitPython for the Proximity Trinkey, as is the APDS9960 library. However, one dependency for the APDS9960 library is not built in, and therefore must be loaded manually onto your board.

First, you must install the library dependency.

Then you need to update code.py.

Click the Download Project Bundle button below to download the necessary library dependency and the code.py file in a zip file. Extract the contents of the zip file. From inside the lib/ folder in the Project Bundle, copy only the entire adafruit_register/ folder to the /lib folder on your CIRCUITPY drive. Then, copy the code.py file to your CIRCUITPY drive.

If you try to copy the entire Project Bundle lib folder, you will run out of space on your board. Copy ONLY the entire adafruit_register/ folder to the /lib folder on your CIRCUITPY drive.

Your CIRCUITPY drive contents should resemble the image.

You should have in / of the CIRCUITPY drive:

  • code.py

And in the lib folder on your CIRCUITPY drive:

  • adafruit_register/
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
NeoPixel brightness proximity example. Increases the brightness of the NeoPixels as you move closer
to the proximity sensor.
"""
import time
import board
import neopixel
from adafruit_apds9960.apds9960 import APDS9960

apds = APDS9960(board.I2C())
pixels = neopixel.NeoPixel(board.NEOPIXEL, 2)

apds.enable_proximity = True


def proximity_to_brightness(value):
    """Maps the proximity values (0 - 255) to the brightness values (0.0 - 1.0)"""
    return value / 255 * 1.0


pixels.fill((255, 0, 0))

while True:
    print(apds.proximity)
    pixels.brightness = proximity_to_brightness(apds.proximity)
    time.sleep(0.2)

Now, move your hand closer to the Proximity Trinkey to see the NeoPixel brightness increase!

Note that the NeoPixels may not be on at all when you save your code. This is because the proximity value is 0 when there's nothing near the sensor, which means the brightness value is also 0. So the LEDs are off!

First, you import the necessary modules and libraries. Then you setup the APDS9960 and the NeoPixel LEDs, and enable the proximity feature on the APDS9960.

Next, there is a helper function that maps the proximity values (0 to 255) to the brightness values (0.0 to 1.0).

Right before the loop, you turn the pixels on to red.

Inside the loop, you first print the proximity values to the serial console. Then, you set pixel brightness equal to the proximity value mapped through the helper function. And finally, you have a 0.2 second delay to keep the serial console values readable.

That's all there is to using the proximity sensor to control NeoPixel brightness!

This guide was first published on Sep 29, 2021. It was last updated on 2021-09-29 11:19:56 -0400.

This page (NeoPixel Brightness) was last updated on Jul 03, 2022.

Text editor powered by tinymce.