Skip to content

Instantly share code, notes, and snippets.

@futureshocked
Created January 6, 2020 05:59
Show Gist options
  • Save futureshocked/05fb1fe7fa6b9c5a68dbe910266686e3 to your computer and use it in GitHub Desktop.
Save futureshocked/05fb1fe7fa6b9c5a68dbe910266686e3 to your computer and use it in GitHub Desktop.
This sketch uses a BME280 sensor to report temperature, humidity, and atmospheric pressure.
#include <Wire.h> //*Include the Wire library which allows to use the I2C interface*
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h> //*library to easily take readings from the sensor*
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; //*Declare the bpm variable, an easy to remember reference for the device*
unsigned long delayTime;
void setup() {
Serial.begin(9600); //*Setup serial communication and speed*
Serial.println(F("BME280 test"));
bool status;
status = bme.begin(); //*Try to start the device*
if (!status) { //*If it is not starting, print message*
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1); //* Go in an endless loop. This prevents the Arduino from calling the loop function*
}
Serial.println("-- Default Test --");
delayTime = 1000;
Serial.println();
}
void loop() {
printValues();
delay(delayTime);
}
void printValues() {
Serial.print("Temperature = "); //*Read and print temperature*
Serial.print(bme.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = "); //*Read and print pressure*
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa"); // Calculate altitude assuming 'standard' barometric pressure of 1013.25 millibar = 101325 Pascal
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); //*Read and print altitude*
// you can get a more precise measurement of altitude if you know the current sea level pressure which willvvary with weather and such. If it is 1015 millibars that is equal to 101500 Pascals.
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity()); //*Read and print humidity*
Serial.println(" %");
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment