Press button A to zero the calculator!

The Adafruit CLUE has a barometric pressure sensor that uses the pressure readings to calculate altitude. For more information on how this works, check out Wikipedia.

With CircuitPython and a little math, it is possible to use the altitude reading to calculate the height of an object using your CLUE! Start with the CLUE at the bottom of the object, and press button A to set the initial reading, and then lift your CLUE up to the top of the object to get your reading.

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.

To get the most accurate altitude reading, you'll want to find out the sea level pressure at your location. A google search should provide you with a number of options. Make sure you convert the results to hPa. Then set `clue.sea_level_pressure =` to the sea level pressure at your location in hPa.

```# SPDX-FileCopyrightText: 2019 Kattni Rembor, written for Adafruit Industries
#
"""Calculate the height of an object. Press button A to reset initial height and then lift the
CLUE to find the height."""

# Set to the sea level pressure in hPa at your location for the most accurate altitude measurement.
clue.sea_level_pressure = 1015

clue_display = clue.simple_text_display(
text_scale=2,
colors=(clue.CYAN, 0, clue.RED, clue.RED, 0, clue.YELLOW, 0, clue.GREEN),
)

initial_height = clue.altitude

clue_display[0].text = "Calculate height!"
clue_display[2].text = "Press A to reset"
clue_display[3].text = "initial height!"

while True:
if clue.button_a:
initial_height = clue.altitude
clue.pixel.fill(clue.RED)
else:
clue.pixel.fill(0)

clue_display[5].text = "Altitude: {:.1f} m".format(clue.altitude)
clue_display[7].text = "Height: {:.1f} m".format(clue.altitude - initial_height)
clue_display.show()
```

Let's take a look at the code.

First you set the sea level pressure at your location. Then you set up the text display to prepare to add your lines of text. Note that we don't use every line, so to set the colors on only the lines we used, we include a `0` as the color for the unused lines - this is easier and cleaner than setting them to a color! Then we get an initial height reading. The first three lines of text on the display are not dynamic in any way and can be set outside the loop.

Inside the loop, we check to see if button A is pressed, and if it is, we reset the initial height reading to the current altitude reading. This allows for you to reset the initial height reading while the program is running. As well, we turn the NeoPixel LED on the back of the board red when button A is pressed, otherwise we turn it off. Then we display the current altitude.

This is followed by the current height which is the change in altitude from the initial height reading, which is to say, our simple math: `altitude - initial_height`.

Then we call `show()` to make all of it show up on the display.

That's all there is to creating a height calculator with CircuitPython and CLUE!

Lift the clue to the top of an object to measure its height!

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

This page (CLUE Height Calculator) was last updated on Jun 14, 2022.