Example Code for Arduino-Environmental Monitoring (BME280)

This example demonstrates how to use the onboard BME280 sensor to monitor environmental data (temperature, humidity, pressure, and approximate altitude). Users will learn how to read data from the BME280 using I2C.

Hardware Preparation

  • DFR0216 DFRduino UNO R3 - Arduino Compatible x1
  • DFR0505 SIM7000C Arduino NB-IoT/LTE/GPRS Expansion Shield x1 (with onboard BME280)
  • USB Wire x1

Software Preparation

Wiring Diagram

The BME280 uses I2C communication. Ensure the I2C pins (SDA, SCL) are connected correctly. The default I2C address is 0x76.

Other Preparation Work

  • Ensure the I2C drive is initialized by using the line DFRobot_BME280 bme; //I2C in the code.
  • The sea level pressure is set to 1013.25 hPa by default. Adjust if necessary for your location.

Sample Code

/*!
 * @file basicTestI2C.ino
 * @brief DFRobot's Temperature、Pressure、Humidity and Approx altitude
 * @n [Get the module here]
 * @n This example read the Temperature、Pressure、Humidity and Altitude from BME280, and then print them
 * @n [Connection and Diagram]
 *
 * @copyright  [DFRobot](https://www.dfrobot.com), 2016
 * @copyright GNU Lesser General Public License
 *
 * @author [yangyang]
 * @version  V1.0
 * @date  2017-7-5
 */

#include <DFRobot_BME280.h>

#define SEA_LEVEL_PRESSURE  1013.25f
#define BME_CS 10

DFRobot_BME280 bme; //I2C

float temp, pa, hum, alt;

void setup() {
    Serial.begin(115200);

    // I2c default address is 0x76, if the need to change please modify bme.begin(Addr)
    if (!bme.begin()) {
        Serial.println("No sensor device found, check line or address!");
        while (1);
    }

    Serial.println("-- BME280 DEMO --");
}

void loop() {
  temp = bme.temperatureValue();
  pa = bme.pressureValue();
  hum = bme.humidityValue();
  alt = bme.altitudeValue(SEA_LEVEL_PRESSURE);

  Serial.print("Temp:");
  Serial.print(temp);
  Serial.println(" C");

  Serial.print("Pa:");
  Serial.print(pa);
  Serial.println(" Pa");

  Serial.print("Hum:");
  Serial.print(hum);
  Serial.println(" %");

  Serial.print("Alt:");
  Serial.print(alt);
  Serial.println(" m");

  Serial.println("------END------");

  delay(1000);
}

Result

The serial monitor will print the temperature (in °C), pressure (in Pa), humidity (in %RH), and approximate altitude (in m) every second. Expected output:

-- BME280 DEMO --
Temp:25.5 C
Pa:101325 Pa
Hum:50 %
Alt:0 m
------END------
warning_yellow.png NOTE: DFRobot_BME280 bme // IIC sentence mentioned in the sample code is to initialize IIC drive.

Was this article helpful?

TOP