Logging Temperature, Humidity Data with BME 280 using Pi Pico and Micropython

These days if you wanted to know the temperature outside, or even the humidity levels you would probably just look it up on your phone or better still ask Alexa or Google about it. Here’s a project to challenge the maker in you. Instead of asking Google or Alexa the weather conditions how about building your own simple home weather monitoring project using a Raspberry Pi Pico and a BME 280 programmed with Micropython.

Raspberry Pi Pico

Raspberry Pi Pico is a tiny, fast, and versatile board built using RP2040, the flagship microcontroller chip designed by Raspberry Pi in the UK. From light displays and IoT devices to signage and manufacturing processes, Raspberry Pi Pico gives the maker the power to control countless home, hobby, and industrial projects. Programmable in C and MicroPython, the Pi Pico is adaptable to a vast range of applications and skill levels, and getting started is as easy as dragging and dropping a file. More experienced users can take advantage of Raspberry Pi Pico’s rich peripheral set, including SPI, I2C, and eight Programmable I/O (PIO) state machines for custom peripheral support.

The BME280 is a humidity sensor especially developed for mobile applications and wearables where size and low power consumption are key design parameters. The unit combines high linearity and high accuracy sensors and is perfectly feasible for low current consumption, long-term stability and high EMC robustness. The humidity sensor offers an extremely fast response time and therefore supports performance requirements for emerging applications such as context awareness, and high accuracy over a wide temperature range. Operational range for the BME280 is :

  1. Pressure: 300…1100 hPa
  2. Temperature: -40…85°C
BME 280 Temperature, Pressure, Humidity sensor

Here are the components you will need to build your home weather station –

  1. Raspberry Pi Pico microcontroller x 1
  2. BME280 (not a BMP280) x 1
  3. Dupont male to male wires x 4
  4. USB micro cable x 1
  5. Breadboard x 1

Circuit diagram – Follow the circuit diagram provided below and assemble the circuit before you proceed with programming the Pi Pico to fetch temperature, pressure and humidity from the BME 280.

BME 280 with the Pi Pico

Code for the tutorial – Open up your favourite Micropython editor and type in the following code. There are a number of editors out there for you to choose from. The two most commonly used editors for Micropython are Thonny and Mu. We tend to use Thonny a lot in class with our kids.

from machine import Pin, I2C, RTC     #importing relevant modules & classes
import time
import bme280       #importing BME280 library
# Setting up the pin which we'll use to blink the onboard LED:
led = Pin(25, Pin.OUT)
#Let's setup I2C for the BME280 
i2c = I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)    #initializing the I2C method 
#Let's create our BME object
bme = bme280.BME280(i2c=i2c) #BME280 object created
# Create a Real Time Clock object.
# Note, the date and time will be set by Thonny if connected to a computer.
# When the Pi Pico is run disconnected from Thonny, the date at time will start at 01/01/2021 12:00AM.
rtc = RTC()
# Open/create a log.csv file to add data to.
logFile = open("hab_log.csv", "a")
# Write the a CSV header line
logFile.write("Date time, Temperature, Pressure, Humidity" + "\n")
#Close the file to avoid corruption.
while True:
    # Get the current Date and Time from the Real Time Clock object.
    # Format the Date and Time into an easy to read string.
    timestring="%04d-%02d-%02d %02d:%02d:%02d"%(timestamp[0:3] + timestamp[4:7])
    # Declare variables and assign values
    temperature = bme.values[0]
    pressure = bme.values[1]
    humidity = bme.values[2]
    # Testing out variables by printing them out on the screen
    print("Temperature is - ",temperature)
    print("Pressure is - ",pressure)
    print("Humidity is - ",humidity)
    # Create a string that contains our Date and Time, Temperature and Humidity, each separated by a comma.
    csvLine = timestring + "," + str(temperature) + "," + str(pressure) + "," + str(humidity)
    # Blink LED so we know its still running when not connected to Thonny.
    # Open/create a log.csv file to add data to.
    logFile = open("hab_log.csv", "a")
    # Write the data to the file including adding "\n" to the end to end the line.
    logFile.write(csvLine + "\n")
    #Close the file to avoid corruption.
    # Used for purposes of testing
    #print("Temp: {temp} Pressure: {pre} Hum: {hum}".format(temp=bme.values[0],pre=bme.values[1],hum=bme.values[2]))
    time.sleep(10)           #delay of 10s

Summary – In this tutorial we used Micropython to program the Raspberry Pi Pico to grab temperature, pressure and humidity from the BME 280. The code is designed to run an infinite loop and grab data from the sensor every 10 seconds, logging the data to a file.

We hope you enjoyed the tutorial. Drop us a note at trevor at hack2 dot live if you have any questions, comments or suggestions.

Leave a Reply

Your email address will not be published. Required fields are marked *