Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 20: Temperature, Humidity & Pressure Sensor (BMP280)

In this lesson, you’ll learn how to connect the BMP280 temperature, humidity, and pressure sensor to the Raspberry Pi Pico W using MicroPython. You’ll get practical experience in setting up I2C communication, configuring the BMP280 sensor for weather monitoring, and obtaining temperature and pressure data. By the end of this tutorial, you’ll be able to view real-time environmental data on your console.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Raspberry Pi Pico W

BUY

Temperature, Humidity & Pressure Sensor (BMP280)

BUY

Breadboard

BUY

Wiring

../_images/Lesson_20_bmp280_bb.png

Code

Note

  • Open the 20_bmp280_module.py file under the path of universal-maker-sensor-kit-main/pico/Lesson_20_BMP280_Module or copy this code into Thonny, then click “Run Current Script” or simply press F5 to run it. For detailed tutorials, please refer to Open and Run Code Directly.

  • Here you need to use the bmp280.py, please check if it has been uploaded to Pico W, for a detailed tutorial refer to Upload the Libraries to Pico.

  • Don’t forget to click on the “MicroPython (Raspberry Pi Pico)” interpreter in the bottom right corner.

from machine import I2C, Pin
import bmp280
import time

# Initialize I2C communication
i2c = I2C(0, sda=Pin(20), scl=Pin(21), freq=100000)

# Configure BMP280 sensor
bmp = bmp280.BMP280(i2c)
bmp.oversample(bmp280.BMP280_OS_HIGH)

while True:
    # Set sensor to weather monitoring mode
    bmp.use_case(bmp280.BMP280_CASE_WEATHER)

    # Print temperature and pressure data
    print("tempC: {}".format(bmp.temperature))
    print("pressure: {}Pa".format(bmp.pressure))

    # Read data every second
    time.sleep_ms(1000)

Code Analysis

  1. Importing Libraries and Initializing I2C Communication:

    This code segment imports necessary libraries and initializes I2C communication. The machine module is used to interact with the hardware components like I2C and pins. The bmp280 library is imported to interact with the BMP280 sensor.

    For more information about the bmp280 library, please visit dafvid/micropython-bmp280.

    from machine import I2C, Pin
    import bmp280
    import time
    
    # Initialize I2C communication
    i2c = I2C(0, sda=Pin(20), scl=Pin(21), freq=100000)
    
  2. Configuring the BMP280 Sensor:

    Here, the BMP280 sensor is configured. An object bmp is created to interact with the sensor. The oversampling setting is adjusted for higher accuracy.

    # Configure BMP280 sensor
    bmp = bmp280.BMP280(i2c)
    bmp.oversample(bmp280.BMP280_OS_HIGH)
    
  3. Reading and Displaying Sensor Data in a Loop:

    The sensor is continuously read in an infinite loop. Each iteration sets the sensor to weather monitoring mode, reads the temperature and pressure, and prints them. The time.sleep_ms(1000) ensures the loop runs once every second.

    while True:
        # Set sensor to weather monitoring mode
        bmp.use_case(bmp280.BMP280_CASE_WEATHER)
    
        # Print temperature and pressure data
        print("tempC: {}".format(bmp.temperature))
        print("pressure: {}Pa".format(bmp.pressure))
    
        # Read data every second
        time.sleep_ms(1000)