Robojax

Display BME280 Pressure, Humidity and Temperature Sensor on LCD

دروس آردوینو به فارسی

Display BME280 Pressure, Humidity and Temperature Sensor on LCD

This is the code on using BME280 to display temperature as degree Celsius, Fahrenheit or Kelvin and also display pressure as hPa and approximate elevation on the LCD1602-I2C or LCD2004. I have written custom function to make this all easy. Please watch the video instruction to understand it fully.

This Tutorial requires you to watch 2 article

1-Introduction to LCD1602-I2C with code
2-Introduction BMB280

Purcahse this module

-From Amazon USA
-From Amazon Canada

BME280 wiht LCD display: Celsius and Fahrenheit

BME280 wiht LCD display: Celsius and Fahrenheit
Click on image to enlarge

BME280 wiht LCD display: Kelvin and relative Humidity

BME280 wiht LCD display: Kelvin and relative Humidity
Click on image to enlarge

BME280 wiht LCD display: pressure and approximate elevation

BME280 wiht LCD display: pressure and approximate elevation
Click on image to enlarge

Resources for this sketch


 /*
* 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 instruction 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 andopen-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. Update 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 wan to diaplay: "Temp.: 340.45K" starting from first character
 * on 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
}