I haven’t tried to do it in Micro Python, but would like to try it. I’ve done it in Python with Pi Enviro+ code. The pressure changes so slowly that you need to graph it over a long period. A 2mb change over 2 hours is considered as “changing rapidly”.
I have wired up a BME280 to my Tufty to display weather info.
'''
pimoroni-pico-v1.19.0-micropython
pico lipo 16mb
Pico Display Pack, 1.14” 240x135 pixel IPS LCD screen
BME280
'''
import utime
import picographics
from breakout_bme280 import BreakoutBME280
from pimoroni_i2c import PimoroniI2C
from picographics import PicoGraphics, DISPLAY_TUFTY_2040, PEN_RGB332
from machine import ADC, Pin
vsys = ADC(29)
charging = Pin(24, Pin.IN)
conversion_factor = 3 * 3.3 / 65535
full_battery = 4.5
empty_battery = 2.8
display = PicoGraphics(display=DISPLAY_TUFTY_2040, rotate=270, pen_type=PEN_RGB332)
display.set_backlight(1.0)
display.set_font("bitmap8")
from pimoroni_i2c import PimoroniI2C
i2c = PimoroniI2C(sda=(4), scl=(5))
bme = BreakoutBME280(i2c,0x77)
# lets set up some pen colors to make drawing easier
tempcolor = display.create_pen(0, 255, 0) # this colour will get changed in a bit
humidcolor = display.create_pen(0, 255, 0) # this colour will get changed in a bit
presscolor = display.create_pen(0, 255, 0) # this colour will get changed in a bit
white = display.create_pen(255, 255, 255)
black = display.create_pen(0, 0, 0)
red = display.create_pen(255, 0, 0)
green = display.create_pen(0, 255, 0)
blue = display.create_pen(0, 0, 255)
yellow = display.create_pen(255, 255, 0)
orange = display.create_pen(255, 140, 0)
grey = display.create_pen(120, 120, 120)
violet = display.create_pen(255, 0, 255)
# converts the temperature into a description and pen colour
def describe_temperature(temperature):
global tempcolor
if temperature < 0:
description = "Very Cold"
tempcolor = blue
elif 0 <= temperature < 12:
description = "Cold"
tempcolor = yellow
elif 12 <= temperature < 17:
description = "Cool"
tempcolor = green
elif 17 <= temperature < 25:
description = "Warm"
tempcolor = green
elif 25 <= temperature < 30:
description = "Hot"
tempcolor = orange
elif temperature >= 30:
description = "Very Hot"
tempcolor = red
else:
description = ""
tempcolor = black
return description
# converts humidity into good/bad description and pen color
def describe_humidity(humidity):
global humidcolor
if humidity < 30:
description = "Low - Dry"
humidcolor = orange
elif 30 <= humidity < 61:
description = "OK"
humidcolor = green
elif 61 <= humidity < 81:
description = "High"
humidcolor = yellow
elif humidity >= 81:
description = "Very High"
humidcolor = red
else:
description = ""
humidcolor = black
return description
# converts pressure into barometer-type description and pen color
def describe_pressure(pressure):
global presscolor
if pressure < 982:
description = "Very Low"
presscolor = red
elif 982 <= pressure < 1004:
description = "Low"
presscolor = yellow
elif 1004 <= pressure < 1026:
description = "Unsettled"
presscolor = green
elif 1026 <= pressure < 1048:
description = "High"
presscolor = blue
elif pressure >= 1048:
description = "Very High"
presscolor = orange
else:
description = ""
presscolor = black
return description
start_time = utime.time()
while True:
time_elapsed = utime.time() - start_time
# read the sensors
temperature, pressure, humidity = bme.read()
# convert pressure to mb
pressuremb = pressure / 100
# drawing the temperature reading
display.set_pen(tempcolor)
display.text('{:.1f}'.format(temperature) + '`c', 10, 10, scale=4)
display.text(describe_temperature(temperature), 10, 45, scale=4)
# drawing the humidity reading
display.set_pen(humidcolor)
display.text('{:.0f}'.format(int(humidity)) + '%', 10, 90, scale=4)
display.text(describe_humidity(int(humidity)), 10, 125, scale=4)
# drawing the pressure reading
display.set_pen(presscolor)
display.text('{:.0f}'.format(pressuremb) + 'mb', 10, 165, scale=4)
display.text(describe_pressure(pressuremb), 10, 200, scale=4)
# draw a thermometer
display.set_pen(grey)
display.circle(210, 170, 20)
display.rectangle(199, 14, 24, 140)
display.set_pen(tempcolor)
display.circle(210, 170, 10)
thermometerheight = int((temperature) * 5)
if thermometerheight > 150:
thermometerheight = 150
if thermometerheight < 1:
thermometerheight = 1
display.rectangle(206, 60 + 120 - thermometerheight, 10, thermometerheight)
if charging.value() == 1: # if it's plugged into USB power...
display.set_pen(blue)
display.text("USB Powered", 5, 240, scale=4)
else:
display.set_pen(red)
display.text("On Battery", 25, 240, scale=4)
voltage = vsys.read_u16() * conversion_factor
percentage = 100 * ((voltage - empty_battery) / (full_battery - empty_battery))
if percentage > 100:
percentage = 100.00
display.set_pen(green)
display.text('{:.2f}'.format(voltage) + "v", 5, 280, scale=4)
display.text('{:.0f}%'.format(percentage), 150, 280, scale=4)
# time to update the display
if time_elapsed < 2:
temperature, pressure, humidity = bme.read()
display.set_pen(black)
display.clear()
display.update()
utime.sleep(1)
else:
display.update()
utime.sleep(1)
display.set_pen(black)
display.clear()