We're going to use CircuitPython, Mu and the accelerometer built into the Circuit Playground Express to plot motion. We'll run this code on our Circuit Playground Express and use Mu to plot the motion data that CircuitPython prints out.
Save the following as code.py on your Circuit Playground Express board, using the Mu editor:
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board import adafruit_lis3dh import busio i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA) lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19) lis3dh.range = adafruit_lis3dh.RANGE_8_G while True: x, y, z = lis3dh.acceleration print((x, y, z)) time.sleep(0.1)
Our code is really simple. We import time
, board
, adafruit_lis3dh
, and busio
. Then we setup the accelerometer. Inside our while loop, we assign x, y, z
to be the motion values. Then we print
our motion values. The time.sleep(0.1)
slows down the readings a little - without it they're too fast to read!
Note that the Mu plotter looks for tuple values to print. Tuples in Python come in parentheses () with comma separators. If you have two values, a tuple would look like (1.0, 3.14)
. We have three values, (x, y, z)
, and note that the tuple itself has its own parentheses. Thus the extra parentheses in print((x, y, z))
.
Once you have everything up and running, try moving the board around to see the plotter respond immediately! The accelerometer is in the center of the board, and depending on which axis you move the board around, the x, y or z values will change. Try moving it only one direction to watch a single value change! Try shaking it all around to see all the values change significantly!
This is a great way to see the motion values and plot the motion as you move the board!
Page last edited January 22, 2025
Text editor powered by tinymce.