Measure Altitude with BME280 and Raspberry Pi Pico

The BME280 is a sensor that can measure temperature, humidity, and air pressure. It can be used to estimate altitude using air pressure readings. The Adafruit BME280 library for CircuitPython comes with built-in altitude estimation, which will be shown how to set up in this tutorial.

Where to buy BME280:

Amazon Link

Step 1-) Install CircuitPython on Your Device

This can be done by holding the bootsel button on your Pico and plugging it into power. Once this is done you can go to Tools > Interpreter in Thonny, select CircuitPython from the dropdown, and click Install or Update CircuitPython. Select the volume and version you would like to download.

Step 2-) Get the Library Setup

The library can be found here https://learn.adafruit.com/circuitpython-libraries-on-micropython-using-the-raspberry-pi-pico/bme280-library-example

Click Download Project Bundle and unzip the contents. You want to add the two libraries, adafruit_bme280 and adafruit_bus_device, to the lib folder on the device so that you have access to them on your Pico. Simple as that.

Step 3-) Physical Setup

You will need to setup like the diagram shown here:

Step 4-) Run the code shown here

import time
import board
import busio
from adafruit_bme280 import basic as adafruit_bme280

# Create sensor object, using the board's default I2C bus.
i2c = busio.I2C(board.GP1, board.GP0)  # SCL, SDA
# address can change based on bme device
# if 0x76 does not work try 0x77 :)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76)
# change this to match the location's pressure (hPa) at sea level
bme280.sea_level_pressure = 1009.9

while True:
    print("\nTemperature: %0.1f C" % bme280.temperature)
    print("Humidity: %0.1f %%" % bme280.relative_humidity)
    print("Pressure: %0.1f hPa" % bme280.pressure)
    print("Altitude = %0.2f meters" % bme280.altitude)
    time.sleep(1)

You will need to adjust the sea_level_pressure according to your location. You can google sea level pressure in your area and substitute the value.

Once this is done, you should start getting values on par with your actual altitude, which you can confirm on Google Earth. You will notice changes in altitude quickly upon moving the sensor up and down a few meters. Congrats!

If you learned something do not forget to like, comment, and subscribe to the channel. Thanks for reading!

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

How to Use ADS1115 with the Raspberry Pi (Part 1)

How to Use ADS1115 with the Raspberry Pi (Part 1)

Discover how to expand your Raspberry Pi projects by integrating the ADS1115 ADC for precise analog signal reading....

How to Install Pip Packages in AWS Lambda Using Docker and ECR

How to Install Pip Packages in AWS Lambda Using Docker and ECR

Learn how to streamline AWS Lambda deployments by using Docker and Amazon Elastic Container Registry (ECR) to package...

Create Tabular Product Descriptions on Your Shopify Store

Create Tabular Product Descriptions on Your Shopify Store

Enhance your Shopify store's product pages with our comprehensive guide on implementing tabular descriptions. Learn how to add a...

SSH Into Raspberry Pi with Tailscale VPN

SSH Into Raspberry Pi with Tailscale VPN

Effortlessly access and manage your Raspberry Pi from anywhere using Tailscale's secure mesh VPN.

Send Email with Lua and the ESP32

Send Email with Lua and the ESP32

In this tutorial, we delve into sending emails with the ESP32-S3 using Lua, focusing on the Xedge IDE's built-in SMTP...

How to Code with Lua on ESP32 with XEdge32

How to Code with Lua on ESP32 with XEdge32

Learn how to set up Xedge32 and start coding on the ESP32-S3 with Lua programming!

Stream Audio From Raspberry Pi to Local Computer

Stream Audio From Raspberry Pi to Local Computer

Discover the simplicity of streaming live audio directly from a USB microphone connected to your Raspberry Pi to...

SSH Raspberry Pi via Cell Phone

SSH Raspberry Pi via Cell Phone

This beginner-friendly guide will walk you through remotely controlling your Raspberry Pi using SSH through your cell phone.

Remotely Control Raspberry Pi via SSH from External Network

Remotely Control Raspberry Pi via SSH from External Network

Learn how to SSH into your Raspberry Pi from any network. This is critical in IoT since you can control...

Stream Video from Raspberry Pi Camera to YouTube Live

Stream Video from Raspberry Pi Camera to YouTube Live

Learn how to stream to YouTube from a Raspberry Pi Camera.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.