Table of Contents

ESP32 BMP280

This is an example of the BMP280 reading and displaying on screen.

Prequisits

There is no necessary prequisits, the wire library will be pulled in automatically.

Example

#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_LiquidCrystal.h>
#include <Adafruit_BMP280.h>

/* BMP280 */
Adafruit_BMP280 bmp; // I2C

#define SDA_PIN 17
#define SCL_PIN 16

#define LCD_RS 48
#define LCD_ENABLE 47
#define LCD_D4 34
#define LCD_D5 33
#define LCD_D6 26
#define LCD_D7 21

static Adafruit_LiquidCrystal lcd(LCD_RS, LCD_ENABLE, LCD_D4, LCD_D5, LCD_D6, LCD_D7);

void setup() {
  Serial.begin(152000);

  // Initialize I2C on custom pins
  Wire.begin(SDA_PIN, SCL_PIN);

  // Initialize LCD
  if (!lcd.begin(16, 2)) {
    Serial.println("Could not init LCD");
    while (1);
  }
  Serial.println("LCD ready.");
  lcd.print("IOT-OPEN");

  // Initialize BMP280 with custom I2C (Wire) instance
  if (!bmp.begin(0x76)) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring or address!"));
    while (1) delay(10);
  }

  bmp.setSampling(Adafruit_BMP280::MODE_FORCED,
                  Adafruit_BMP280::SAMPLING_X2,
                  Adafruit_BMP280::SAMPLING_X16,
                  Adafruit_BMP280::FILTER_X16,
                  Adafruit_BMP280::STANDBY_MS_500);
}

void loop() {
  float h = bmp.readPressure();
  float t = bmp.readTemperature();

  if (bmp.takeForcedMeasurement()) {
    Serial.print(F("Temperature = "));
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");

    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");

    Serial.print(F("Approx altitude = "));
    Serial.print(bmp.readAltitude(1013.25)); // Adjust to local forecast
    Serial.println(" m");
  }

  lcd.setCursor(0, 0);
  lcd.print("P: ");
  lcd.print(h / 100.0); // Convert Pa to hPa
  lcd.print("hPa");

  lcd.setCursor(0, 1);
  lcd.print("T: ");
  lcd.print(t);
  lcd.print("C");

  delay(1000);
}

}
en/iot-open/practical/hardware/rtu/esp32/scenarios/bmp280.txt · Last modified: 2025/07/31 14:18 by kivilands6
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0