The Adafruit CLUE comes with a built in accelerometer for measuring acceleration. For more information on how that works, check out Wikipedia.
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!
Installing Project Code
To use with CircuitPython, you need to first install a few libraries, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the directory examples/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.
Your CIRCUITPY drive should now look similar to the following image:
# SPDX-FileCopyrightText: 2019 Kattni Rembor, written for Adafruit Industries # # SPDX-License-Identifier: MIT """CLUE Spirit Level Demo""" import board import displayio from adafruit_display_shapes.circle 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) clue_group.append(outer_circle) clue_group.append(middle_circle) clue_group.append(inner_circle) x, y, _ = clue.acceleration bubble_group = displayio.Group() level_bubble = Circle(int(x + 120), int(y + 120), 20, fill=clue.RED, outline=clue.RED) bubble_group.append(level_bubble) clue_group.append(bubble_group) display.root_group = clue_group 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!
Text editor powered by tinymce.