#include <Wire.h>
#include <Adafruit_BMP280.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_BMP280 bmp;
#define BMP_CS 5
#define BMP_SCK 13
#define BMP_MOSI 23
#define BMP_MISO 19
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
// Initialize the SPI communication for BMP280
pinMode(BMP_CS, OUTPUT);
digitalWrite(BMP_CS, HIGH);
SPI.begin(BMP_SCK, BMP_MISO, BMP_MOSI, BMP_CS);
Serial.println("BMP280 Initialized");
lcd.setCursor(0, 0);
lcd.print("BMP280 Initialized");
delay(2000);
lcd.clear();
}
uint32_t readBMP280Pressure() {
uint8_t msb, lsb, xlsb;
uint32_t rawPressure;
// Read PRESS_MSB (0xF7), PRESS_LSB (0xF8), and PRESS_XLSB (0xF9)
digitalWrite(BMP_CS, LOW); // Enable chip select
SPI.transfer(0xF7); // PRESS_MSB address
msb = SPI.transfer(0x00);
digitalWrite(BMP_CS, HIGH); // Disable chip select
digitalWrite(BMP_CS, LOW);
SPI.transfer(0xF8); // PRESS_LSB address
lsb = SPI.transfer(0x00);
digitalWrite(BMP_CS, HIGH);
digitalWrite(BMP_CS, LOW);
SPI.transfer(0xF9); // PRESS_XLSB address
xlsb = SPI.transfer(0x00);
digitalWrite(BMP_CS, HIGH);
// Combine the three bytes to get the raw pressure value
rawPressure = ((uint32_t)msb << 12) | ((uint32_t)lsb << 4) | ((uint32_t)xlsb >> 4);
return rawPressure;
}
void loop() {
// Read the raw pressure from BMP280
uint32_t rawPressure = readBMP280Pressure();
// Convert raw pressure to hPa (hectopascals) using your calibration values if needed
float pressure = rawPressure / 256.0; // Example conversion (adjust as necessary)
// Display the pressure on the LCD
lcd.setCursor(0, 0);
lcd.print("Pressure: ");
lcd.setCursor(0, 1);
lcd.print(pressure);
lcd.print(" hPa");
// Print the pressure to the serial monitor for debugging
Serial.print("Raw Pressure: ");
Serial.println(rawPressure);
Serial.print("Pressure: ");
Serial.print(pressure);
Serial.println(" hPa");
delay(1000); // Delay for a second
}