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 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi Pico W |
|
Wiringď
Codeď
Note
Open the
20_bmp280_module.pyfile under the path ofuniversal-maker-sensor-kit-main/pico/Lesson_20_BMP280_Moduleor 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ď
Importing Libraries and Initializing I2C Communication:
This code segment imports necessary libraries and initializes I2C communication. The
machinemodule is used to interact with the hardware components like I2C and pins. Thebmp280library is imported to interact with the BMP280 sensor.For more information about the
bmp280library, 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)
Configuring the BMP280 Sensor:
Here, the BMP280 sensor is configured. An object
bmpis 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)
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)