Skip to content

Commit 1d1b521

Browse files
committed
stellar: add BME280 and BME68x examples
1 parent 3786cbd commit 1d1b521

File tree

3 files changed

+282
-0
lines changed

3 files changed

+282
-0
lines changed

micropython/examples/stellar_unicorn/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
- [Other Examples](#other-examples)
2525
- [CO2](#co2)
2626
- [Encoder Wheel](#encoder-wheel)
27+
- [Thermometer (BME280)](#thermometer-bme280)
28+
- [Thermometer (BME68x)](#thermometer-bme68x)
2729
- [Launch (Demo Reel)](#launch-demo-reel)
2830

2931
## About Stellar Unicorn
@@ -180,6 +182,8 @@ The examples in the folder use `numpy`-like array functions contained in the `ul
180182

181183
## Other Examples
182184

185+
These examples use additional hardware.
186+
183187
### CO2
184188

185189
[co2.py](co2.py)
@@ -193,6 +197,16 @@ This example uses a custom tiny bitmap font, find 3x5.bitmapfont in [fonts](../.
193197

194198
This example uses [RGB Encoder Wheel breakout](https://shop.pimoroni.com/products/rgb-encoder-wheel-breakout) to make an RGB colour picker. Use the encoder wheel to pick a hue and view the RGB breakdown of that colour on the Unicorn display (you can adjust saturation and brightness using the buttons on the breakout too).
195199

200+
### Thermometer (BME280)
201+
[thermometer_bme280.py](thermometer_bme280.py)
202+
203+
Shows temperature, humidity and pressure (from a [BME280 sensor breakout](https://shop.pimoroni.com/products/bme280-breakout)) against an appropriately coloured pulsing blob.
204+
205+
### Thermometer (BME68x)
206+
[thermometer_bme68x.py](thermometer_bme68x.py)
207+
208+
Shows temperature, humidity and pressure (from a [BME680](https://shop.pimoroni.com/products/bme680-breakout) or [BME688](https://shop.pimoroni.com/products/bme688-breakout) sensor breakout) against an appropriately coloured pulsing blob.
209+
196210
### Launch (Demo Reel)
197211

198212
[launch](launch)
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import time
2+
from stellar import StellarUnicorn
3+
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN
4+
from pimoroni_i2c import PimoroniI2C
5+
from pimoroni import BREAKOUT_GARDEN_I2C_PINS
6+
from breakout_bme280 import BreakoutBME280
7+
8+
"""
9+
Reads the temperature from a BME280
10+
... and displays an appropriately coloured pulsing blob.
11+
12+
Buttons:
13+
A - Show temperature
14+
B - Show humidity
15+
C - Show pressure
16+
"""
17+
18+
# The range of readings that we want to map to colours
19+
MIN = 10
20+
MAX = 30
21+
22+
# pick what bits of the colour wheel to use (from 0-360°)
23+
# https://www.cssscript.com/demo/hsv-hsl-color-wheel-picker-reinvented/
24+
HUE_START = 230 # blue
25+
HUE_END = 359 # red
26+
27+
# rainbow party mode
28+
rainbow_orb = False
29+
30+
# set up the Unicron
31+
su = StellarUnicorn()
32+
graphics = PicoGraphics(DISPLAY_STELLAR_UNICORN)
33+
34+
# set up the sensor
35+
i2c = PimoroniI2C(**BREAKOUT_GARDEN_I2C_PINS)
36+
bme = BreakoutBME280(i2c)
37+
38+
# set up constants and variables for drawing
39+
WIDTH, HEIGHT = graphics.get_bounds()
40+
41+
BLACK = graphics.create_pen(0, 0, 0)
42+
WHITE = graphics.create_pen(255, 255, 255)
43+
44+
forward = True
45+
orb_brightness = 0.5
46+
hue = 0.0
47+
mode = "temperature"
48+
49+
graphics.set_font("bitmap8")
50+
51+
while True:
52+
53+
if su.is_pressed(StellarUnicorn.SWITCH_A):
54+
mode = "temperature"
55+
print(f"mode = {mode}")
56+
57+
elif su.is_pressed(StellarUnicorn.SWITCH_B):
58+
mode = "humidity"
59+
print(f"mode = {mode}")
60+
61+
elif su.is_pressed(StellarUnicorn.SWITCH_C):
62+
mode = "pressure"
63+
print(f"mode = {mode}")
64+
65+
# read the onboard sensor
66+
# the following two lines do some maths to convert the number from the temp sensor into celsius
67+
temperature, pressure, humidity = bme.read()
68+
69+
print(f"""
70+
Temperature: {temperature:.2f} °C
71+
Humidity: {humidity:.2f} %
72+
Pressure: {pressure/100:.2f} hPa
73+
""")
74+
75+
# fills the screen with black
76+
graphics.set_pen(BLACK)
77+
graphics.clear()
78+
79+
# draw a weird orb:
80+
# three overlapping circles with varying saturations
81+
if rainbow_orb is True:
82+
graphics.set_pen(graphics.create_pen_hsv((hue / 360), 0.5, orb_brightness))
83+
graphics.circle(8, 8, 7)
84+
graphics.set_pen(graphics.create_pen_hsv((hue / 360), 0.7, orb_brightness))
85+
graphics.circle(7, 7, 7)
86+
graphics.set_pen(graphics.create_pen_hsv((hue / 360), 1.0, orb_brightness))
87+
graphics.circle(7, 7, 5)
88+
hue += 0.01 * 360
89+
else:
90+
# calculate a colour from the temperature
91+
hue = max(0, HUE_START + ((temperature - MIN) * (HUE_END - HUE_START) / (MAX - MIN)))
92+
graphics.set_pen(graphics.create_pen_hsv((hue / 360), 0.6, orb_brightness))
93+
graphics.circle(8, 8, 7)
94+
graphics.set_pen(graphics.create_pen_hsv((hue / 360), 0.8, orb_brightness))
95+
graphics.circle(7, 7, 7)
96+
graphics.set_pen(graphics.create_pen_hsv((hue / 360), 1.0, orb_brightness))
97+
graphics.circle(7, 7, 5)
98+
99+
# pulse the orb!
100+
if forward is True:
101+
orb_brightness += 0.01
102+
if orb_brightness >= 0.7:
103+
orb_brightness = 0.7
104+
forward = False
105+
106+
if forward is False:
107+
orb_brightness -= 0.01
108+
if orb_brightness <= 0.3:
109+
orb_brightness = 0.3
110+
forward = True
111+
112+
# select a pen colour for the text
113+
# try BLACK for a funky negative space effect
114+
graphics.set_pen(WHITE)
115+
116+
if mode == "temperature":
117+
graphics.text(f"{temperature:.0f}°", 2, 5, scale=1)
118+
# or uncomment these lines if you'd prefer it in Freedom Units
119+
# fahrenheit = (temperature * 9 / 5) + 32
120+
# graphics.text(f"{fahrenheit:.0f}°", 2, 5, scale=1)
121+
122+
if mode == "humidity":
123+
graphics.text(f"{humidity:.0f}%", 1, 5, scale=1)
124+
125+
if mode == "pressure":
126+
if pressure/100 < 1000:
127+
graphics.text(f"{pressure/100:.0f} hPa", 1, 0, WIDTH, scale=1)
128+
else:
129+
pressure_string = str(pressure/100)
130+
graphics.text(f"{pressure_string[0]}.{pressure_string[1]}k hPa", 0, 0, WIDTH, scale=1)
131+
132+
# time to update the display
133+
su.update(graphics)
134+
time.sleep(0.1)
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import time
2+
from stellar import StellarUnicorn
3+
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN
4+
from pimoroni_i2c import PimoroniI2C
5+
from pimoroni import BREAKOUT_GARDEN_I2C_PINS
6+
from breakout_bme68x import BreakoutBME68X
7+
8+
"""
9+
Reads the temperature from a BME680 or BME688
10+
... and displays an appropriately coloured pulsing blob.
11+
12+
Buttons:
13+
A - Show temperature
14+
B - Show humidity
15+
C - Show pressure
16+
"""
17+
18+
# The range of readings that we want to map to colours
19+
MIN = 10
20+
MAX = 30
21+
22+
# pick what bits of the colour wheel to use (from 0-360°)
23+
# https://www.cssscript.com/demo/hsv-hsl-color-wheel-picker-reinvented/
24+
HUE_START = 230 # blue
25+
HUE_END = 359 # red
26+
27+
# rainbow party mode
28+
rainbow_orb = False
29+
30+
# set up the Unicron
31+
su = StellarUnicorn()
32+
graphics = PicoGraphics(DISPLAY_STELLAR_UNICORN)
33+
34+
# set up the sensor
35+
i2c = PimoroniI2C(**BREAKOUT_GARDEN_I2C_PINS)
36+
bme = BreakoutBME68X(i2c)
37+
38+
# set up constants and variables for drawing
39+
WIDTH, HEIGHT = graphics.get_bounds()
40+
41+
BLACK = graphics.create_pen(0, 0, 0)
42+
WHITE = graphics.create_pen(255, 255, 255)
43+
44+
forward = True
45+
orb_brightness = 0.5
46+
hue = 0.0
47+
mode = "temperature"
48+
49+
graphics.set_font("bitmap8")
50+
51+
while True:
52+
53+
if su.is_pressed(StellarUnicorn.SWITCH_A):
54+
mode = "temperature"
55+
print(f"mode = {mode}")
56+
57+
elif su.is_pressed(StellarUnicorn.SWITCH_B):
58+
mode = "humidity"
59+
print(f"mode = {mode}")
60+
61+
elif su.is_pressed(StellarUnicorn.SWITCH_C):
62+
mode = "pressure"
63+
print(f"mode = {mode}")
64+
65+
# read the onboard sensor
66+
# the following two lines do some maths to convert the number from the temp sensor into celsius
67+
temperature, pressure, humidity, gas, status, _, _ = bme.read()
68+
69+
print(f"""
70+
Temperature: {temperature:.2f} °C
71+
Humidity: {humidity:.2f} %
72+
Pressure: {pressure/100:.2f} hPa
73+
""")
74+
75+
# fills the screen with black
76+
graphics.set_pen(BLACK)
77+
graphics.clear()
78+
79+
# draw a weird orb:
80+
# three overlapping circles with varying saturations
81+
if rainbow_orb is True:
82+
graphics.set_pen(graphics.create_pen_hsv((hue / 360), 0.5, orb_brightness))
83+
graphics.circle(8, 8, 7)
84+
graphics.set_pen(graphics.create_pen_hsv((hue / 360), 0.7, orb_brightness))
85+
graphics.circle(7, 7, 7)
86+
graphics.set_pen(graphics.create_pen_hsv((hue / 360), 1.0, orb_brightness))
87+
graphics.circle(7, 7, 5)
88+
hue += 0.01 * 360
89+
else:
90+
# calculate a colour from the temperature
91+
hue = max(0, HUE_START + ((temperature - MIN) * (HUE_END - HUE_START) / (MAX - MIN)))
92+
graphics.set_pen(graphics.create_pen_hsv((hue / 360), 0.6, orb_brightness))
93+
graphics.circle(8, 8, 7)
94+
graphics.set_pen(graphics.create_pen_hsv((hue / 360), 0.8, orb_brightness))
95+
graphics.circle(7, 7, 7)
96+
graphics.set_pen(graphics.create_pen_hsv((hue / 360), 1.0, orb_brightness))
97+
graphics.circle(7, 7, 5)
98+
99+
# pulse the orb!
100+
if forward is True:
101+
orb_brightness += 0.01
102+
if orb_brightness >= 0.7:
103+
orb_brightness = 0.7
104+
forward = False
105+
106+
if forward is False:
107+
orb_brightness -= 0.01
108+
if orb_brightness <= 0.3:
109+
orb_brightness = 0.3
110+
forward = True
111+
112+
# select a pen colour for the text
113+
# try BLACK for a funky negative space effect
114+
graphics.set_pen(WHITE)
115+
116+
if mode == "temperature":
117+
graphics.text(f"{temperature:.0f}°", 2, 5, scale=1)
118+
# or uncomment these lines if you'd prefer it in Freedom Units
119+
# fahrenheit = (temperature * 9 / 5) + 32
120+
# graphics.text(f"{fahrenheit:.0f}°", 2, 5, scale=1)
121+
122+
if mode == "humidity":
123+
graphics.text(f"{humidity:.0f}%", 1, 5, scale=1)
124+
125+
if mode == "pressure":
126+
if pressure/100 < 1000:
127+
graphics.text(f"{pressure/100:.0f} hPa", 1, 0, WIDTH, scale=1)
128+
else:
129+
pressure_string = str(pressure/100)
130+
graphics.text(f"{pressure_string[0]}.{pressure_string[1]}k hPa", 0, 0, WIDTH, scale=1)
131+
132+
# time to update the display
133+
su.update(graphics)
134+
time.sleep(0.1)

0 commit comments

Comments
 (0)