If you would like to try out CircuitPython, you will find it rather easy to use compared to other programming environments. We suggest you read the Adafruit Welcome to CircuitPython Guide to get started.

Adafruit recommends the Mu editor to type your code into and interact with your project. You can find out how to install Mu on mac, PC, and Linux in this guide page on the Adafruit Learning System.

The advantage to using Mu is it has an editor and added features like direct save to Circuit Playground Express and access to the Express' REPL interactive command line and serial print output. Mu can even plot values for you.

The CPX Library

The adafruit_circuitplayground library provides functions for a variety of accelerometer uses including shake detection, tap detection, and reading the accelerometer values.

You can read about the available functions and parameters in this document on read the docs.

Basic Shake Detection

Here is the basic code for shake detection for the Circuit Playground Express in CircuitPython.

import time
from adafruit_circuitplayground import cpx

while True:
    if cpx.shake(shake_threshold=20):
        print("Shake detected!")
        cpx.pixels.fill((150, 0, 0))
        time.sleep(5.0)  # In seconds
    else:
        cpx.pixels.fill((0, 0, 0))

This program will make the NeoPixels go red and print a message if a shake is detected. 

You can adjust the shake sensitivity easily in the cpx.shake detection line. 10 will constantly activate so set it higher than that. The default is 30. The values for this sensitivity are numerically different than those in MakeCode.

Tap Detection

The accelerometer can detect single or double taps. You might use this if you were constructing a mouse or a Star Trek badge, as examples.

from adafruit_circuitplayground import cpx
 
cpx.detect_taps = 2
 
while True:
    if cpx.tapped:
        print("Tapped!")

Change cpx.detect_taps to 1 for single tap, 2 for double tap.

Reading Acceleration

You can read the individual x, y, and z values for the accelerometer. The units are in meters/s2 so gravity will pull on the board at 9.8 m/s2 down towards the ground.

import time
from adafruit_circuitplayground import cpx
 
while True:
    x, y, z = cpx.acceleration
    print((x, y, z))
    time.sleep(0.5)

This code will print the three values to the serial console and if you are using the Mu editor, you can plot the values. Turn the board in space and see the readings change. See where the earth is pulling?

The video below demonstrates viewing the values.

Note: Their code is a tiny bit different but the numbers output are the exact same numbers the code above is outputting, it is just obtaining them via a lower level library.

You can read more about shake in the guide CircuitPython Made Easy on Circuit Playground Express and Bluefruit.

This guide was first published on Sep 12, 2018. It was last updated on Mar 08, 2024.

This page (Use in CircuitPython) was last updated on Mar 08, 2024.

Text editor powered by tinymce.