/**
* Program: Temperature, Pressure and Altitude Monitoring with BMP280
*
* This program uses a BMP280 sensor to measure temperature, pressure and altitude,
* displaying the values on an SSD1306 OLED display.
*
* Connections:
* - BMP280 connected via SPI:
* - SCK: pin 13
* - MISO: pin 12
* - MOSI: pin 11
* - CS: pin 10
* - OLED connected via I2C (SDA, SCL)
*/
#include <Wire.h>
#include <U8glib.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <SPI.h>
// Constants
#define SEA_LEVEL_PRESSURE 1013.25 // Sea level pressure in hPa (standard)
#define UPDATE_DELAY 1000 // Time between updates (1 seconds)
// Define SPI pins for BMP280
#define BMP_SCK 13 // SPI Clock
#define BMP_MISO 12 // SPI MISO
#define BMP_MOSI 11 // SPI MOSI
#define BMP_CS 10 // Chip Select
// BMP280 sensor instance using SPI
Adafruit_BMP280 bmp_sensor(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
// OLED display instance
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_FAST);
/**
* Initialize the BMP280 sensor via SPI
* Returns true if the sensor was successfully initialized
*/
bool initBMP280() {
if (!bmp_sensor.begin()) {
Serial.println("BMP280 sensor not found. Check SPI connections!");
return false;
}
// Optional sensor configurations
bmp_sensor.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operation mode */
Adafruit_BMP280::SAMPLING_X2, /* Temperature oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time */
return true;
}
/**
* Initialize the OLED display
*/
void initDisplay() {
// Display color settings
if (u8g.getMode() == U8G_MODE_R3G3B2) {
u8g.setColorIndex(255); // white
}
else if (u8g.getMode() == U8G_MODE_GRAY2BIT) {
u8g.setColorIndex(3); // maximum intensity
}
else if (u8g.getMode() == U8G_MODE_BW) {
u8g.setColorIndex(1); // pixel on
}
else if (u8g.getMode() == U8G_MODE_HICOLOR) {
u8g.setHiColorByRGB(255, 255, 255);
}
}
/**
* Draw the interface on the OLED display with sensor data
*/
void drawInterface() {
// Font settings
u8g.setFont(u8g_font_8x13B);
// Draw frames
u8g.drawRFrame(0, 16, 128, 48, 4); // Main frame
u8g.drawRFrame(0, 0, 128, 16, 4); // Title frame
// Title
u8g.drawStr(25, 13, "BMP280 SPI");
// Temperature
float temperature = bmp_sensor.readTemperature();
u8g.drawStr(10, 31, "Temp: C");
u8g.drawCircle(93, 22, 2); // Degree symbol
u8g.setPrintPos(55, 31);
u8g.print(temperature, 1); // 1 decimal place
// Pressure (in hPa)
float pressure = bmp_sensor.readPressure() / 100.0F; // Convert to hPa
u8g.drawStr(10, 45, "Pres: hPa");
u8g.setPrintPos(55, 45);
u8g.print(pressure, 1); // 1 decimal place
// Altitude
float altitude = bmp_sensor.readAltitude(SEA_LEVEL_PRESSURE);
u8g.drawStr(10, 59, "Alt: m");
u8g.setPrintPos(55, 59);
u8g.print(altitude, 1); // 1 decimal place
}
/**
* Update the display with current sensor information
*/
void updateDisplay() {
u8g.firstPage();
do {
drawInterface();
} while (u8g.nextPage());
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
Serial.println("Starting BMP280 monitoring system via SPI");
// Initialize SPI communication
SPI.begin();
// Initialize the BMP280 sensor
if (!initBMP280()) {
Serial.println("Sensor initialization failed. Check connections!");
while (1); // Halt execution if the sensor is not found
}
// Initialize the OLED display
initDisplay();
Serial.println("System successfully initialized");
}
void loop() {
// Update the display with current data
updateDisplay();
// Print data to serial monitor
float temperature = bmp_sensor.readTemperature();
float pressure = bmp_sensor.readPressure() / 100.0F; // Convert to hPa
float altitude = bmp_sensor.readAltitude(SEA_LEVEL_PRESSURE);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Pressure: ");
Serial.print(pressure);
Serial.println(" hPa");
Serial.print("Approx. altitude: ");
Serial.print(altitude);
Serial.println(" m");
Serial.println("-------------------");
// Wait before the next update
delay(UPDATE_DELAY);
}