#include <Wire.h>
#include <Adafruit_SH1107.h>
#include <Adafruit_BME280.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 128
// BME280 I2C address is 0x76(108)
#define Addr 0x76
Adafruit_BME280 bme;
// OLED Display Configuration
Adafruit_SH1106 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup()
{
// Initialize I2C communication as MASTER
Wire.begin();
// Initialize Serial communication, set baud rate = 9600
Serial.begin(9600);
// Initialize the OLED display
if (!display.begin(SSD1306_I2C_ADDRESS, SCREEN_WIDTH, SCREEN_HEIGHT))
{
Serial.println(F("SH1106 allocation failed"));
for (;;);
}
display.display(); // Initialize with the buffer cleared
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
// Initialize the BME280 sensor
if (!bme.begin(Addr))
{
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
}
void loop()
{
// Read sensor data
float cTemp = bme.readTemperature();
float fTemp = cTemp * 1.8 + 32;
float pressure = bme.readPressure() / 100.0F; // Convert pressure to hPa
float humidity = bme.readHumidity();
// Clear the OLED display
display.clearDisplay();
// Display data on the OLED
display.setCursor(0, 0);
display.print("Temp: ");
display.print(cTemp, 2);
display.println(" C");
display.print(fTemp, 2);
display.println(" F");
display.print("Pressure: ");
display.print(pressure, 2);
display.println(" hPa");
display.print("Humidity: ");
display.print(humidity, 2);
display.println(" %");
// Display on OLED
display.display();
// Output data to serial monitor
Serial.print("Temperature in Celsius : ");
Serial.print(cTemp);
Serial.println(" C");
Serial.print("Temperature in Fahrenheit : ");
Serial.print(fTemp);
Serial.println(" F");
Serial.print("Pressure : ");
Serial.print(pressure);
Serial.println(" hPa");
Serial.print("Relative Humidity : ");
Serial.print(humidity);
Serial.println(" %");
delay(1000);
}