The Adafruit CLUE comes with a built in accelerometer for measuring acceleration. For more information on how that works, check out Wikipedia.

Remember that you must have the necessary libraries installed. Verify that your lib folder matches the list found on the CLUE CircuitPython Libraries page before continuing.

Using CircuitPython, we can create a spirit level using generated shapes and the accelerometer data. This example generates circles as guide lines and and outline, and a dot as the "bubble". Move the board around to watch the bubble "float" to the top!

# SPDX-FileCopyrightText: 2019 Kattni Rembor, written for Adafruit Industries
# SPDX-License-Identifier: Unlicense
"""CLUE Spirit Level Demo"""
import board
import displayio
from import Circle
from adafruit_clue import clue

display = board.DISPLAY
clue_group = displayio.Group()

outer_circle = Circle(120, 120, 119, outline=clue.WHITE)
middle_circle = Circle(120, 120, 75, outline=clue.YELLOW)
inner_circle = Circle(120, 120, 35, outline=clue.GREEN)

x, y, _ = clue.acceleration
bubble_group = displayio.Group()
level_bubble = Circle(int(x + 120), int(y + 120), 20, fill=clue.RED, outline=clue.RED)


while True:
    x, y, _ = clue.acceleration
    bubble_group.x = int(x * -10)
    bubble_group.y = int(y * -10)

Let's take a look at the code.

First, we import the necessary libraries and modules. Then we create the display object for later use.

Next we create the clue_group that will hold all of the objects we plan to display. Then we create our outline and guide lines, and add them to the group.

Next we get the initial acceleration values. For this, we only care about x and y, but as acceleration is an x, y, z value, we must unpack three values from it. The _ takes the place of z which we never use.

Then we create the bubble_group to hold the bubble. With the circles, as they are static, we could simply append them into a group. The bubble needs to move, so it must be in its own group that we will add to the same group as the circles once created. Next we create the level bubble. Instead of a hard coded initial location, its initial location is based on the initial x, y value that we obtained. Then we add the bubble to the bubble group.

Finally, we add the bubble_group to the clue_group, and tell the display to show() everything contained within, which is all of the shapes we created.

Inside the loop, we get an updated x, y value. Since it is in the loop, it will continue to update. Then we set the bubble_group x and y locations to be the constantly updating x and y values from the accelerometer. We multiply them by -10 for two reasons. The multiplication by 10 is to extend the values to utilise the entire display - when moving slowly, x and y values are approximately -10 to 10, so without the multiplication, the dot would move in a small space in the center of the display. The negative is to cause the bubble to move "upwards" like an actual level bubble would, opposite the direction it would otherwise move based on the actual x and y values.

That's what goes into making a spirit level with CircuitPython and CLUE!

This guide was first published on Feb 08, 2020. It was last updated on Feb 08, 2020.

This page (CLUE Spirit Level) was last updated on Aug 19, 2022.

Text editor powered by tinymce.