BMP280 with ESP32 on I2C
參考來源 https://www.circuitschools.com/interfacing-bmp280-with-esp-32-on-i2c-with-errors-and-solutions/
參考來源 https://simple-circuit.com/arduino-bmp280-sensor-lcd/
/*
* Interfacing Arduino with BMP280 temperature and pressure sensor.
* Temperature and pressure values are displayed on 16x2 LCD.
* This is a free software with NO WARRANTY.
* https://simple-circuit.com/
*/
#include <Wire.h> // include Wire library, required for I2C devices
#include <Adafruit_Sensor.h> // include Adafruit sensor library
#include <Adafruit_BMP280.h> // include adafruit library for BMP280 sensor
// define device I2C address: 0x76 or 0x77 (0x77 is library default address)
#define BMP280_I2C_ADDRESS 0x76
Adafruit_BMP280 bmp280;
void setup() {
Serial.begin(115200);
Serial.println(F("Arduino + BMP280"));
if (!bmp280.begin(BMP280_I2C_ADDRESS))
{
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
}
// main loop
void loop()
{
// get temperature, pressure and altitude from library
float temperature = bmp280.readTemperature(); // get temperature
float pressure = bmp280.readPressure(); // get pressure
float altitude_ = bmp280.readAltitude(1013.25); // get altitude (this should be adjusted to your local forecast)
// print data on the serial monitor software
// 1: print temperature
Serial.print("Temperature CELSIUS 攝氏溫度= ");
Serial.print(temperature);
Serial.println(" °C");
float TempF = (temperature*1.8)+32;
Serial.print("Temperature FAHRENHEIT 華氏溫度= ");
Serial.print(TempF);
Serial.println(" °F");
// 2: print pressure
Serial.print("Pressure 大氣壓力百帕(hPa)= ");
Serial.print(pressure/100);
Serial.println(" hPa");
// 3: print altitude
Serial.print("Approx Altitude 大概海拔高度= ");
Serial.print(altitude_);
Serial.println(" m");
Serial.println(); // start a new line
delay(2000); // wait 2 seconds
}
// end of code.
沒有留言:
張貼留言