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.

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!

1 Like