Robojax

Lesson 96: Barometric Pressure, Temperature, Approximate Altitude Sensor BMP390 with LCD

Lesson 96: Barometric Pressure, Temperature, Approximate Altitude Sensor BMP390 with LCD

Please select other codes for this lecture from the links below.

  • Purchase it from Amazon USA
  • Purchase it from Amazon Canada
  • Purchase it from all other Amazon
  • Purchase it from AliExpress
  • Purchase it from Banggood
  • BMP390 Bosch Official Product page
  • Part 6: Temperatures Sensors

    In this lesson BMP390 is explained, wiring shown for I2C and SPI. Then shown to display barometric pressure, temperature and approximate altitude on the serial monitor, on LCD1602, on LCD2004. Full wiring diagram and wiring shown. Library installation shown and approximate altitude is measurged on first floor and compared it with altitude on 25th floor. Code is fully explained and demonstrated using Arduino UNO and Arduino MEGA.

    This code is for LCD2004 with I2C which has only 4 wires.

    Affiliated with Amazon USA

    Affiliated with AliExpress

    
     /*
     * Lesson 96: Using Precision BMP390 Barometric Pressure and Temperatrure Sensor
     * with Arduino and LCD1602
     * Wath video instruction on YouTube:  https://youtu.be/XevQYG_A5xA
     * 
     * Code updated by Ahmad Shamshiri for Robojax.com
     * on Jan 13, 2022 at 17:10 in Ajax, Ontario, Canada
      
      This video is part of Arduino Step by Step Course which starts here: https://youtu.be/-6qSrDUA5a8
     
    
    If you found this tutorial helpful, please support me so I can continue creating contents like this
    and make donation using PayPal http://robojax.com/L/?id=64
    
    
      This is a library for the BMP390 temperature & pressure sensor
    
      Designed specifically to work with the Adafruit BMP388 Breakout
      ----> http://www.adafruit.com/products/3966
    
      These sensors use I2C or SPI to communicate, 2 or 4 pins are required
      to interface.
    
      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_BMP3XX.h"
    
    
    #define SEALEVELPRESSURE_HPA (1013.25)
    
    Adafruit_BMP3XX bmp;
    int bmpVinPin = 12;
    boolean fahrenheit = true;//for Celsius set it to "false"
    
    #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(bmpVinPin, OUTPUT);// set pin as output
        digitalWrite(bmpVinPin,HIGH);// always keep it high (5V) for BMB390 module
         
      Serial.begin(9600);
      while (!Serial);
      Serial.println("Adafruit BMP388 / BMP390 test");
    
      if (!bmp.begin_I2C()) {   // hardware I2C mode, can pass in address & alt Wire
      //if (! bmp.begin_SPI(BMP_CS)) {  // hardware SPI mode  
      //if (! bmp.begin_SPI(BMP_CS, BMP_SCK, BMP_MISO, BMP_MOSI)) {  // software SPI mode
        Serial.println("Could not find a valid BMP3 sensor, check wiring!");
        while (1);
      }
    
      // Set up oversampling and filter initialization
      bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
      bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
      bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
      bmp.setOutputDataRate(BMP3_ODR_50_HZ);
      // 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("BMP390 Test");
      delay(2000);  
    }
    
    void loop() {
      if (! bmp.performReading()) {
        Serial.println("Failed to perform reading :(");
        return;
      }
    
        // Robojax.com BME390 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
                 getBMP('C'),
                 'C'
                 );  
    
     lcdDisplay(
                 // to print fahrenheit:
                 0, // character 0 
                 1, // line 1
                 "Fahr.: ", 
    
                 // to print Fahrenheit
                 9, // character 9
                 1, // line 0
                 getBMP('F'),
                 'F'
                 );  
        delay(4000);
      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
                 getBMP('P'),
                 'p' 
                 );  
      lcdDisplay(
                 // to print altitude text
                 0, // character 0 
                 1, // line 1
                 "Ap. Alt.:", 
    
                 // to print Altitude
                 99, // character undefined
                 1, // line 1
                 getBMP('A'),
                 'm' 
                 );                   
            delay(4000);
    
         // Robojax.com BMP390 Code                                
      
      if(bmp.temperature >38.0)
      {
       digitalWrite(10, HIGH); 
    
       
      }else{
        digitalWrite(10, LOW);
      }
      delay(2000);
    
      printRobojax(true);//set to true or false
    }
    
    /*
     * @brief returns temperature or relative humidity
     * @param "type" is character
     *     C = Celsius
     *     K = Keliven
     *     F = Fahrenheit
     *     H = Humidity
     *     P = Pressure
     *     A = Altitude
     * @return returns one of the values above
     * Usage: to get Fahrenheit type: getBMP('F')
     * to print it on serial monitor Serial.println(getBMP('F'));
     * Written by Ahmad Shamshiri on July 13, 2019. Update 13 Jan 2022
     * at 20:56 
     * in Ajax, Ontario, Canada
     * www.Robojax.com 
     */
    float getBMP(char type)
    {
       // Robojax.com BMP390 Code
      float value;
        float temp = bmp.temperature;
      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 =='P')
       {
        value = bmp.pressure / 100.0F; // read pressure
       }else if(type =='A')
       {
        value = bmp.readAltitude(SEALEVELPRESSURE_HPA);// read altitude
       }else{
        value = bmp.temperature;// read temperature
       }
       return value;
        // Robojax.com BME390 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')
     *   
     *   'C' is degree symbol for C and F
     * 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 BMP390 Demo
    
       lcd.setCursor (tc,tr); //
       lcd.print(title);
       if(vc !=99)
       {
       lcd.setCursor (vc,vr); //
       }   
       lcd.print(value);
       if(symbol == 'C')
       {
        lcd.print((char)223);
        lcd.print('C');
       }else if(symbol == 'F')
       {
        lcd.print((char)223);
        lcd.print('F');
       }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 BMP390 Demo
    
    
    
     /*
     * 
     * this fuction if called will print Robojax stuff
     * */
     void printRobojax( boolean doPrint)
     {
      if (doPrint){
       lcd.clear();// clear previous values from screen  
       lcd.setCursor (0,0); //set to line char 1, line 1
       lcd.print("Robojax  BMP390"); 
    
       lcd.setCursor (0,1); //set to line char 1, line 2
       lcd.print("www.ROBOJAX.com");    
    
      
       delay(4000);   
       }       
     }