CircuitPython Code

Getting Set Up for CircuitPython

This project is written in CircuitPython. If you're new to CircuitPython, visit the Welcome to CircuitPython guide for everything you need to know to get started!

For in-depth instructions on how to program your Circuit Playground Express with the code shown below, visit this page in the Circuit Playground Express guide.

While you can edit CircuitPython in any text editor of your choice, Adafruit highly suggests using Mu Editor, which contains helpers and an interactive REPL. To install Mu and learn how to use it, visit the Mu guide.

This code uses the Adafruit HC-SR04 library, which you will need to download and add to the lib folder on your Circuit Playground Express, if it's not already there. You can learn about installing the adafruit_HC-SR04 library in the Library guide. It is easiest to install the whole library package.

The Code

In a nutshell: This code sets up the HC-SR04 as a sensor for the Circuit Playground Express to read. The distances sent from the sensor can range from zero to over 400 cm. With a little math (the pitchMultiplier variable), we turn the distance into a frequency for the Circuit Playground Express to play through the connected speaker. We also use the wheel helper function, commonly used in Adafruit code with rainbow animations, to turn this distance measurement into an RGB value for the NeoPixels on-board the Circuit Playground Express.

When the sensor detects an object, your hand for instance, within 3 - 25 cm, it plays a sound and flashes its lights!

To learn more about the super handy wheel function and how it works, see this page in Kattni Rembor's Ikea Lamp Hack guide.

import time
import board
import adafruit_hcsr04
from adafruit_circuitplayground.express import cpx

# This line creates the distance sensor as an object.
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.D9, echo_pin=board.D6, timeout=0.1)
pixels = cpx.pixels
pitchMultiplier = 300   # Change this value to modify the pitch of the theremin.

def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        return (0, 0, 0)
    if pos < 85:
        return (255 - pos * 3, pos * 3, 0)
    if pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)
    pos -= 170
    return (pos * 3, 0, 255 - pos * 3)

while True:
    try:
        handDistance = int(sonar.distance)
        print("Distance:", handDistance)
    except RuntimeError:
        print("retrying!")
    time.sleep(.00001)

    pitch = handDistance*pitchMultiplier

    # Limits on the distances that trigger sound/light to between 3 and 25 cm.
    if (handDistance >= 3) & (handDistance < 25):
        cpx.play_tone(pitch, 0.1)
        pixels.fill(wheel(handDistance*10))
        pixels.show()
        time.sleep(.00001)
        print(pitch)
    else:
        cpx.stop_tone()
        pixels.fill((0, 0, 0))
        pixels.show()
This guide was first published on Oct 28, 2018. It was last updated on Oct 28, 2018. This page (CircuitPython Code) was last updated on Oct 21, 2019.