Created
June 11, 2024 18:53
-
-
Save cybersholt/9600d7b23e7c5a293f4d32bdb2731318 to your computer and use it in GitHub Desktop.
What I used to debug a BME280 with Espresence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <SPI.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_BME280.h> | |
// Define I2C pins for ESP32 | |
#define SDA_PIN 11 | |
#define SCL_PIN 12 | |
// BME280 object | |
Adafruit_BME280 bme; | |
// Sea level pressure (Pa) for altitude calculation, can be adjusted for your location | |
#define SEALEVELPRESSURE_HPA (1013.25) | |
void setup() { | |
Wire.begin(SDA_PIN, SCL_PIN); // Initialize I2C communication | |
Serial.begin(9600); | |
while (!Serial); // Wait for serial port to connect | |
Serial.println(F("BME280 comprehensive test")); | |
// Initialize BME280 sensor | |
if (!bme.begin(0x76)) { | |
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!"); | |
while (1) delay(10); | |
} | |
Serial.println("-- Default Test --"); | |
} | |
void loop() { | |
printSensorData(); | |
delay(1000); // Wait 1 second before next reading | |
} | |
// Function to convert Celsius to Fahrenheit | |
float celsiusToFahrenheit(float c) { | |
return c * 9.0 / 5.0 + 32; | |
} | |
// Function to convert hPa to inHg | |
float hPaToInHg(float hPa) { | |
return hPa * 0.02953; | |
} | |
// Function to convert meters to feet | |
float metersToFeet(float meters) { | |
return meters * 3.28084; | |
} | |
// Function to print sensor values in imperial units | |
void printSensorData() { | |
Serial.print("Temperature = "); | |
Serial.print(celsiusToFahrenheit(bme.readTemperature())); | |
Serial.println(" °F"); | |
Serial.print("Pressure = "); | |
Serial.print(hPaToInHg(bme.readPressure() / 100.0F)); | |
Serial.println(" inHg"); | |
Serial.print("Humidity = "); | |
Serial.print(bme.readHumidity()); | |
Serial.println(" %"); | |
Serial.print("Altitude = "); | |
Serial.print(metersToFeet(bme.readAltitude(SEALEVELPRESSURE_HPA))); | |
Serial.println(" feet"); | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment