Back to Arduino Step By Step Course

Lesson 37: BMe280 humidity, barometric pressure and ambient temperature Sensor| Arduino Step by Step Course

If you don't like to see ads while video is being played you can purchase this course for $200 from Udemy or purchase YouTube premium Here

Please download required files for this code

Part 4: Temperature Sensors

In this lesson we learn how to use BME280 Humidity sensor measuring relative humidity, barometric pressure and ambient temperature The BME280 is a humidity sensor especially developed for mobile applications and wearables where size and low power consumption are key design parameters. The unit combines high linearity and high accuracy sensors and is perfectly feasible for low current consumption, long-term stability and high EMC robustness. The humidity sensor offers an extremely fast response time and therefore supports performance requirements for emerging applications such as context awareness, and high accuracy over a wide temperature range.
This code has two custom function that makes reading and displaying temperature or humidity very easy.

 * Robojax Arduino Step By Step Course
 * Part 4: Temperature Sensors
 * Lesson 37: BME280 code with 2 extra function to display temperature easily
* written and Updated by Ahmad Shamshiri on July 25, 2019 at 18:42 
* in Ajax, Ontario, Canada for

  Please watch video instruction here
 This code is available at
with over 100 lectures Free On  YouTube Watch it here
Get the code for the course:  

If you found this tutorial helpful, please support me so I can continue creating 
 make donation using PayPal

  This is a library for the BME280 humidity, temperature & pressure sensor

  Designed specifically to work with the Adafruit BME280 Breakout

  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

void setup() {
    Serial.println(F("BME280 test"));

    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);
    Serial.println("-- Robojax Test --");


void loop() { 
    // BME280 Code YouTube Watch it here
    Serial.print("Temperature = ");
    Serial.print("C ");
    Serial.print("F "); 
    Serial.println("K ");       

    Serial.print("Pressure = ");
    Serial.println(" hPa ");

    Serial.print("Humidity = ");
    Serial.println("% ");
    Serial.print("Approx. Altitude = ");
    Serial.println(" m");


    // action
    if(getBME('C') <65.2)
     //digitalWrite(5, HIGH); 
     // BME280 Code YouTube Watch it here
}// loop end

 * @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: getHTU('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
float getBME(char type)
   // BME280 Code YouTube Watch it here
  float value;
    float temp = bme.readTemperature();// read temperature
    float pressure = bme.readPressure() / 100.0F; // read pressure
    float rel_hum = bme.readHumidity();// read humidity
    float alt =bme.readAltitude(SEALEVELPRESSURE_HPA);// read altitude
   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 = rel_hum;//return relative humidity
   }else if(type =='P')
    value = pressure;//return pressure
   }else if(type =='A')
    value = alt;//return approximate altitude
    value = temp;// return Celsius
   return value;
    // BME280 Code YouTube Watch it here

 * @brief prints degree symbol on serial monitor
 * @param none
 * @return returns nothing
 * Written by Ahmad Shamshiri on July 13, 2019
 * for Robojax Tutorial
 void printDegree()
   // Code


The least I expect from you is to thumb up the video and subscribe to my channel. I appriciate that. .I have spent months making these lectures and writing code. You don't lose anything by subscribging to my channel. Your subscription is stamp of approval to my videos and more people can find them and in it turn it helps me. Thank you

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal