Skip to content

Commit 0e9e86d

Browse files
authored
Merge pull request #2298 from adafruit/servo_barometer
Adding code for CLUE servo barometer
2 parents d966ddc + b64659b commit 0e9e86d

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

CLUE_Servo_Barometer/code.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import simpleio
7+
import adafruit_bmp280
8+
import pwmio
9+
import displayio
10+
from adafruit_motor import servo
11+
import terminalio
12+
from adafruit_clue import clue
13+
from adafruit_display_text import label
14+
15+
# board display
16+
# scaling for terminalio font
17+
display = board.DISPLAY
18+
group = displayio.Group(scale=3)
19+
20+
# text elements
21+
temp_header = "Temperature:"
22+
press_header = "Pressure:"
23+
temp_text = " ºC"
24+
press_text = " hPa"
25+
font = terminalio.FONT
26+
blue = 0x0000FF
27+
red = 0xFF0000
28+
29+
# pwm setup for servo
30+
pwm = pwmio.PWMOut(board.D0, duty_cycle=2 ** 15, frequency=50)
31+
gauge = servo.Servo(pwm)
32+
33+
# bmp280 sensor setup
34+
i2c = board.I2C() # uses board.SCL and board.SDA
35+
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
36+
37+
bmp280.sea_level_pressure = 1013.25
38+
39+
# temperature text elements
40+
temp_label = label.Label(font, text=temp_header, color=red, x=5, y=10)
41+
temp_data = label.Label(font, text=temp_text, color=red, x=5, y=25)
42+
43+
# pressure text elements
44+
press_label = label.Label(font, text=press_header, color=blue, x=5, y=50)
45+
press_data = label.Label(font, text=press_text, color=blue, x=5, y=65)
46+
47+
# adding text to display group
48+
group.append(temp_label)
49+
group.append(press_label)
50+
group.append(temp_data)
51+
group.append(press_data)
52+
53+
display.show(group)
54+
55+
# function to convert celcius to fahrenheit
56+
def c_to_f(temp):
57+
temp_f = (temp * 9/5) + 32
58+
return temp_f
59+
60+
# function to convert hPa to inHg
61+
def hpa_to_inHg(hPa):
62+
inches_mercury = hPa * 0.02953
63+
return inches_mercury
64+
65+
# time.monotonic clock
66+
clock = 0
67+
# units state
68+
us_units = False
69+
70+
while True:
71+
# non-blocking 2 second delay
72+
if (clock + 2) < time.monotonic():
73+
# map servo range to barometric pressure range
74+
servo_value = simpleio.map_range(bmp280.pressure, 970, 1050, 180, 0)
75+
# set servo to pressure
76+
gauge.angle = servo_value
77+
# print data for debugging
78+
print("\nTemperature: %0.1f C" % bmp280.temperature)
79+
print("Pressure: %0.1f hPa" % bmp280.pressure)
80+
print(servo_value)
81+
# if not US units...
82+
if not us_units:
83+
# update temp & pressure text in celcius and hPa
84+
temp_data.text = "%0.1f ºC" % bmp280.temperature
85+
press_data.text = "%0.1f hPa" % bmp280.pressure
86+
# if US units...
87+
if us_units:
88+
# convert celcius to fahrenheit
89+
temp_fahrenheit = c_to_f(bmp280.temperature)
90+
# convert hPa to inHg
91+
pressure_inHg = hpa_to_inHg(bmp280.pressure)
92+
# update temp & pressure text
93+
temp_data.text = "%0.1f ºF" % temp_fahrenheit
94+
press_data.text = "%0.1f inHg" % pressure_inHg
95+
# reset time.monotonic() clock
96+
clock = time.monotonic()
97+
# if a button is pressed, us_units is False, show imperial
98+
if clue.button_a:
99+
us_units = False
100+
# if b button is pressed, us_units is True, show US units
101+
if clue.button_b:
102+
us_units = True

0 commit comments

Comments
 (0)