Displaying BME280 Pressure, Humidity, and Temperature Sensor Data on LCD
This is the code for using a BME280 to display temperature as degrees Celsius, Fahrenheit, or Kelvin, and also display pressure as hPa and approximate elevation on the LCD1602-I2C or LCD2004. I have written custom functions to make this all easy. Please watch the video instructions to understand it fully.
219-Purchase this module
语言: C++
/*
* Display values from BME280 on LCD1602-I2C or LCD2004
*
* Written/Updated by Ahmad Shamshiri on July 27, 2019 at 18:42
* in Ajax, Ontario, Canada for Robojax.com
* Watch video instructions for this code: https://youtu.be/mXY_6LOekkw
*
This is a library for the BME280 humidity, temperature & pressure sensor
Designed specifically to work with the Adafruit BME280 Breakout
----> http://www.adafruit.com/products/2650
These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface. The device's I2C address is either 0x76 or 0x77.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing products
from Adafruit!
Written by Limor Fried & Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
**************************************************************************
*/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
const int bmeVinPin = 12;// 5V pin for BME280
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 or 0x3F for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
void setup() {
pinMode(bmeVinPin, OUTPUT);// set pin as output
digitalWrite(bmeVinPin,HIGH);// always keep it high (5V) for BMB280 module
bool status;
// default settings
// (you can also pass in a Wire library object like &Wire2)
status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
// initialize the LCD,
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.print("Robojax Video");
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print("BME280 Test");
delay(2000);
}
void loop() {
// Robojax.com BME280 Code
lcd.clear();// clear previous values from screen
lcdDisplay(
// to print Celsius:
0, // character 0
0, // line 0
"Celsius: ",
// to print Celsius
99, // character undefined
0, // line 0
getBME('C'),
'd'
);
lcdDisplay(
// to print Fahrenheit:
0, // character 0
1, // line 1
"Fahr.: ",
// to print Fahrenheit
99, // character undefined
1, // line 0
getBME('F'),
'd'
);
delay(5000);
lcd.clear();// clear previous values from screen
lcdDisplay(
// to print Kelvin:
0, // character 0
0, // line 0
"Kelvin :",
// to print Celsius
99, // character undefined
0, // line 0
getBME('K'),
'k'
);
lcdDisplay(
// to print humidity text
0, // character 0
1, // line 1
"Humidity: ",
// to print humidity
99, // character undefined
1, // line 1
getBME('H'),
'%'
);
delay(5000);
lcd.clear();// clear previous values from screen
lcdDisplay(
// to print pressure text
0, // character 0
0, // line 1
"Pres.:",
// to print pressure
99, // character undefined
0, // line 1
getBME('P'),
'p'
);
lcdDisplay(
// to print altitude text
0, // character 0
1, // line 1
"Ap. Alt.:",
// to print Altitude
99, // character undefined
1, // line 1
getBME('A'),
'm'
);
delay(5000);
// Robojax.com BME280 Code
}// loop end
/*
* @brief returns temperature or relative humidity
* @param "type" is character
* C = Celsius
* K = Kelvin
* F = Fahrenheit
* H = Humidity
* P = Pressure
* A = Altitude
* @return returns one of the values above
* Usage: to get Fahrenheit type: getBME('F')
* to print it on serial monitor Serial.println(getBME('F'));
* Written by Ahmad Shamshiri on July 13, 2019. Updated July 25, 2019
* in Ajax, Ontario, Canada
* www.Robojax.com
*/
float getBME(char type)
{
// Robojax.com BME280 Code
float value;
float temp = bme.readTemperature();
if(type =='F')
{
value = temp *9/5 + 32;//convert to Fahrenheit
}else if(type =='K')
{
value = temp + 273.15;//convert to Kelvin
}else if(type =='H')
{
value = bme.readHumidity();// read humidity
}else if(type =='P')
{
value = bme.readPressure() / 100.0F; // read pressure
}else if(type =='A')
{
value = bme.readAltitude(SEALEVELPRESSURE_HPA);// read altitude
}else{
value = bme.readTemperature();// read temperature
}
return value;
// Robojax.com BME280 Code
}//getBME
/*
* lcdDisplay(int tc, int tr, String title, int vc, int vr, float value)
* displays value and title on LCD1602
* How to use:
* If you want to display: "Temp.: 340.45K" starting from the first character
* on the second row.
* use:
* lcdDisplay(0, 1, "Temp.: ", 340.45,'K')
*
* 'd' is degree symbol
* tc is character number (0)
* tr is row in the lcd (1)
* title is the text (Voltage:)
* vc value for character
* vr value for row or line
* value is the value (13.56)
*/
void lcdDisplay(int tc, int tr, String title, int vc, int vr, float value,char symbol)
{
// Robojax.com LCD1602 for BME280 Demo
lcd.setCursor (tc,tr); //
lcd.print(title);
if(vc !=99)
{
lcd.setCursor (vc,vr); //
}
lcd.print(value);
if(symbol == 'd')
{
lcd.print((char)223);
}else if(symbol =='%')
{
lcd.print("%");
}else if(symbol =='k')
{
lcd.print("K");
}else if(symbol =='p')
{
lcd.print("hPa");
}else if(symbol =='m')
{
lcd.print("m");
}
// Robojax.com LCD1602 for BME280 Demo
}
资源与参考
-
外部Adafruit BME280 库(来自 GitHub)github.com
-
外部BME280 数据表 (PDF)ae-bst.resource.bosch.com
-
外部BME280制造商网站bosch-sensortec.com
-
外部KR1204 四通道无线继电器在加拿大亚马逊上amzn.to
文件📁
没有可用的文件。