How do do I show BME280 reading on Badger?

I have BME280 connected via QW/ST, and a little script printing the temp to the Thonny terminal.

import time
from breakout_bme280 import BreakoutBME280
from pimoroni_i2c import PimoroniI2C

PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}

i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
bme = BreakoutBME280(i2c)

    
while True:
      temperature, pressure, humidity = bme.read()
      # temperature= round(temperature)
      print(temperature)
      print("{:0.1f}Ā°C" .format(temperature))
      time.sleep(2)

How do I get that reading to show on the badger2040?
And then, an update when a button is pressed?

Thank you.

Whatā€™s posted here should work with a little editing to swap for a BME280
Badger2040 and BME680 - Support - Pimoroni Buccaneers
It should be enough to get you started.

Ah, that helped a lot. Thank you.
With a few tweaks I got this working:

import badger2040
import machine
import time
from breakout_bme280 import BreakoutBME280
from pimoroni_i2c import PimoroniI2C

PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}

i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
bme = BreakoutBME280(i2c)


badger = badger2040.Badger2040()
badger.update_speed(badger2040.UPDATE_FAST)

TEXT_SIZE = 0.7

button_a = machine.Pin(badger2040.BUTTON_A, machine.Pin.IN, machine.Pin.PULL_DOWN)



badger.pen(0)
badger.text("Mushroom Temp Checker", 5, 20, TEXT_SIZE)
badger.update()
time.sleep(5)
badger.pen(15)
badger.clear()

# Make redundant initial garbage reading:
temperature, pressure, humidity = bme.read()
time.sleep(1)

while True:
    temperature, pressure, humidity = bme.read()
    #heater = "Stable" if status & STATUS_HEATER_STABLE else "Unstable"
    print("{:0.2f}c, {:0.2f}Pa, {:0.2f}%".format(
        temperature, pressure, humidity))
    temp = str(round(temperature,1))
    press = str(round(pressure,1))
    humid = str(round(humidity,1))
    print(temp, press, humid)
    badger.pen(0)
    badger.text("Temp: "+temp+"C", 2, 10, TEXT_SIZE)
    badger.text("Prsr: "+press+"Pa", 2, 30, TEXT_SIZE)
    badger.text("Hmdty: "+humid+"%", 2, 50, TEXT_SIZE)
    badger.update()
    time.sleep(30)
    badger.pen(15)
    badger.clear()

Ideally Iā€™d like to replace the default ā€˜infoā€™ option with something this.
Any clues on how to do this, please? Thanks.

Iā€™m not understanding what you want to do?

With Badge running, press A + C
Press Down

See Info on the B button, thatā€™s what Iā€™d like to change to show the temp from the sensor.

Thank you.

If you do this

    #badger.text("Prsr: "+press+"Pa", 2, 30, TEXT_SIZE)
    #badger.text("Hmdty: "+humid+"%", 2, 50, TEXT_SIZE)

Only the Temperature will be displayed.
As written the code checks the sensor and updates the readings every 30 seconds. If you want to do it only when a button is pressed you have a bit of code to add. I donā€™t own a Badger, and havenā€™t used that code. The button example is here.
pimoroni-pico/button_test.py at main Ā· pimoroni/pimoroni-pico (github.com)

I have used the Buttons on my Tufty, it does it differently, code wise anyway. There are a fair number of badger users on the forums so Iā€™m sure one of them can help you get it done.

1 Like

For the BME680 and Badger 2040 W, Iā€™ve put code here: badger2040/ieq.py at main Ā· OpenDevEd/badger2040 Ā· GitHub

The example works within the overall badger_os, but adding the temperature sensing as another ā€œappā€. Iā€™ve tested this with BME680. If anybody knows how to test other sensors (e.g., BME280 as above, BME688 etc), and display the different set measurements accordingly, help is very welcome!

2 Likes