0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use BMP280 Barometric Sensor with Raspberry Pi

To use the BMP280 barometric sensor with a Raspberry Pi, connect it via I2C pins and install the Adafruit_BMP280 Python library. Then, write a Python script to initialize the sensor and read temperature and pressure data easily.
📐

Syntax

Here is the basic syntax to use the BMP280 sensor with Raspberry Pi in Python:

  • import board: Access Raspberry Pi pins.
  • import busio: Use I2C communication.
  • import adafruit_bmp280: Use the BMP280 sensor library.
  • i2c = busio.I2C(board.SCL, board.SDA): Initialize I2C bus.
  • bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c): Create sensor object.
  • bmp280.temperature: Read temperature in Celsius.
  • bmp280.pressure: Read pressure in hPa.
python
import board
import busio
import adafruit_bmp280

i2c = busio.I2C(board.SCL, board.SDA)
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)

print('Temperature:', bmp280.temperature, 'C')
print('Pressure:', bmp280.pressure, 'hPa')
💻

Example

This example shows how to read temperature and pressure from the BMP280 sensor connected to Raspberry Pi using I2C.

python
import board
import busio
import adafruit_bmp280
import time

def main():
    i2c = busio.I2C(board.SCL, board.SDA)
    bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)

    while True:
        temperature = bmp280.temperature
        pressure = bmp280.pressure
        print(f"Temperature: {temperature:.2f} C")
        print(f"Pressure: {pressure:.2f} hPa")
        print('---')
        time.sleep(2)

if __name__ == '__main__':
    main()
Output
Temperature: 24.56 C Pressure: 1013.25 hPa --- Temperature: 24.58 C Pressure: 1013.20 hPa ---
⚠️

Common Pitfalls

  • Incorrect wiring: Make sure SDA and SCL pins on the sensor connect to Raspberry Pi's SDA (GPIO 2) and SCL (GPIO 3) pins.
  • I2C not enabled: Enable I2C interface on Raspberry Pi using raspi-config.
  • Wrong sensor address: BMP280 usually uses address 0x76 or 0x77; check with i2cdetect -y 1.
  • Missing libraries: Install adafruit-circuitpython-bmp280 and dependencies via pip.
python
## Wrong way: Not enabling I2C or wrong pins
# i2c = busio.I2C(board.D4, board.D5)  # Wrong pins

## Right way:
i2c = busio.I2C(board.SCL, board.SDA)  # Correct pins
📊

Quick Reference

StepDescription
Connect sensor SDA to GPIO 2 (SDA)Connect sensor SCL to GPIO 3 (SCL)
Enable I2C on Raspberry PiUse sudo raspi-config > Interface Options > I2C
Install Python librariespip3 install adafruit-circuitpython-bmp280
Run Python scriptReads temperature and pressure continuously
Check sensor addressUse i2cdetect -y 1 to verify 0x76 or 0x77

Key Takeaways

Connect BMP280 sensor to Raspberry Pi using I2C pins SDA (GPIO 2) and SCL (GPIO 3).
Enable I2C interface on Raspberry Pi before running code.
Install the Adafruit BMP280 Python library for easy sensor access.
Use Python code to read temperature and pressure from the sensor.
Verify sensor address with i2cdetect if connection issues occur.