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 to use BMP390 as Thermostat to contorl heater or cooler.

    Affiliated with Amazon USA

    Affiliated with AliExpress

    
     /*
     * Lesson 96: Using Precision BMP390 Barometric Pressure and Temperatrure Sensor
     * with Arduino using it as Thermostat to control heater or cooler
     * 
     * 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 bmpVCCPin = 12;//define pin 12 as VCC for bmp sensor
    
    // all thermostat settings
    
    const int relayPin =8;
    const int relayON = HIGH;// 
    const int relayOFF = LOW; //
    int relayState = relayOFF;//initial state of relay
    
    const int TEMPERATURE_UNIT =1;//1=Celsius, 2=Fahrenheit, 3=Keliven
    const float START_TEMPERATURE = 32.0;//unit above
    const float STOP_TEMPERATURE = 45.0;//unit above
    const int CONTROL_TYPE = 2;// 1= heater, 2=cooler
    
    //do not change the line below
    float temperature, pressure, altitude;
    
    
    
    void setup() {
      Serial.begin(9600);
      while (!Serial);
      Serial.println("Adafruit BMP388 / BMP390 test");
    
      pinMode(bmpVCCPin, OUTPUT);// set pin as output
      digitalWrite(bmpVCCPin,HIGH);// always keep it high (5V) for BMB390 module
      delay(500);//wait for the bmpVCCPin to turn ON.    
    
      
      pinMode(relayPin, OUTPUT);//pin for relay
      digitalWrite(relayPin, relayState);//change initial state of relay
    
    
      if (!bmp.begin_I2C()) {   // hardware I2C mode, can pass in address & alt Wire
      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);
    
    
      
      
    }
    
    void loop() {
      if (! bmp.performReading()) {
        Serial.println("Failed to perform reading :(");
        return;
      }
        readValues();
        printTemperature();
    
      Serial.println();
       loadControl(); 
    
       //do extra action if temperature is greator than 89.5
       if(temperature >=89.5)
       {
        ///and your code here
       }
      delay(2000);//wait 2 seconds or make is less or more
    }
    
    
    
    /*
     * loadControl()
     * @brief controls the load based 
     * @param state can be either : relayON or relayOFF
    
     * @return returns none
     * Written by Ahmad Shamshiri for robojax.com
     * on Jan 15, 2022 at 08:11 in Ajax, Ontario, Canada
     */
    void loadControl()
    {
    //Robojax.com heater/cooler with BMP390 Thermocoupler  
         // Serial.print("Start: ");
         // Serial.print(START_TEMPERATURE);
         // Serial.print(" Stop: ");
          //Serial.println(STOP_TEMPERATURE);      
      if(CONTROL_TYPE ==1)
      {
        if(START_TEMPERATURE >= temperature && STOP_TEMPERATURE >=temperature)
        {
          relayControl(relayON);
        }
        if(STOP_TEMPERATURE <=temperature)
        {
          relayControl(relayOFF);
        }
      }else{
        if(START_TEMPERATURE >= temperature && STOP_TEMPERATURE >=temperature)
        {
          relayControl(relayOFF);
        }
        if(STOP_TEMPERATURE <=temperature)
        {
          relayControl(relayON);
        }
      }
        
    //Robojax.com heater/cooler with BMP390 Thermocoupler
    }//loadControl()
    
    
    /*
     * relayControl(int state))
     * @brief turns the relay ON or OFF
     * @param state is "relayON" or "relayOFF" defined at the top of code
     * @return returns none
     * Written by Ahmad Shamshiri for robojax.com
     * on May 20, 2020 at 15:23 in Ajax, Ontario, Canada
     */
    void relayControl(int state)
    {
      //Robojax.com heater/cooler with BMP390 Thermocoupler
      if(state ==relayON)
      {
        digitalWrite(relayPin, relayON);
        Serial.println("Relay ON");   
      }else{
       digitalWrite(relayPin, relayOFF);    
        Serial.println("Relay OFF");   
      }
      //Robojax.com heater/cooler with BMP390 Thermocoupler  
    
    }//relayControl()
    
    
    /*
     *  readValues()
     * @brief reads the temperature based on the TEMPERATURE_UNIT
     * @param average temperature
    
     * @return returns one of the values above
     * Written by Ahmad Shamshiri for robojax.com
     * on Jan 15, 2022 at 08:02 in Ajax, Ontario, Canada
     */
    void readValues()
    {
     //Robojax.com heater/cooler with BMPP390 Thermocoupler 
    
      altitude =  bmp.readAltitude(SEALEVELPRESSURE_HPA);// read altitude
      pressure = bmp.pressure / 100.0F; // get  pressure in hecto pascal
      
      
      if(TEMPERATURE_UNIT ==2)
       {
       temperature = bmp.temperature *9/5 + 32;//convert to Fahrenheit 
       }else if(TEMPERATURE_UNIT ==3)
       {
        temperature = bmp.temperature + 273.15;//convert to Kelvin
       }else{
        temperature = bmp.temperature;// return Celsius
       }    
     //Robojax.com heater/cooler with BMP390 Thermocoupler 
    }// readValues()
    
    
    /*
     * printTemperature()
     * @brief prints  temperature on serial monitor
     * @param charact type
     * @param "type" is character
     *     C = Celsius
     *     K = Keliven
     *     F = Fahrenheit
     * @return none
     * Written by Ahmad Shamshiri for robojax.com
     * on Jan 15, 2022 at 08:09 in Ajax, Ontario, Canada
     * www.Robojax.com
     */
    void printTemperature()
    {
    //Robojax.com heater/cooler with BMP390 Thermocoupler
       Serial.print(temperature);
        printDegree();    
      if(TEMPERATURE_UNIT ==2)
       {
         Serial.print("F");
        }else if(TEMPERATURE_UNIT ==3)
       {
         Serial.print("K");
       }else{
         Serial.print("C");
       }    
      Serial.println();
    //Robojax.com heater/cooler with BMP390 Thermocoupler  
    }//printTemperature()
    
    /*
     * @brief prints degree symbol on serial monitor
     * @param none
     * @return returns nothing
     * Written by Ahmad Shamshiri on July 13, 2019
     * for Robojax Tutorial Robojax.com
     */
    void printDegree()
    {
        Serial.print("\xC2"); 
        Serial.print("\xB0");  
    }