Example Code for Arduino-Read Temperature Pressure Altitude

Last revision 2026/01/16

Detect the barometric pressure and temperature of the current environment and calculate the altitude of the environment in which the module is located.

Hardware Preparation

  • DFRduino UNO R3 (or similar) x 1
  • BMP280 Digital pressure sensor module(V1.0) x1
  • Jumper wires

Software Preparation

Wiring Diagram

Connect this module with UNO board via I2C port.

Connection

Other Preparation Work

Download BMP280 Library. About how to install a library?

Sample Code

/*!
 * read_data.ino
 *
 * Download this demo to test simple read from bmp280, connect sensor through IIC interface
 * Data will print on your serial monitor
 *
 * Copyright   [DFRobot](http://www.dfrobot.com), 2016
 * Copyright   GNU Lesser General Public License
 *
 * version  V1.0
 * date  12/03/2019
 */

#include "DFRobot_BMP280.h"
#include "Wire.h"

typedef DFRobot_BMP280_IIC    BMP;    // ******** use abbreviations instead of full names ********

BMP   bmp(&Wire, BMP::eSdo_low);

#define SEA_LEVEL_PRESSURE    1015.0f   // sea level pressure

// show last sensor operate status
void printLastOperateStatus(BMP::eStatus_t eStatus)
{
  switch(eStatus) {
  case BMP::eStatusOK:    Serial.println("everything ok"); break;
  case BMP::eStatusErr:   Serial.println("unknow error"); break;
  case BMP::eStatusErrDeviceNotDetected:    Serial.println("device not detected"); break;
  case BMP::eStatusErrParameter:    Serial.println("parameter error"); break;
  default: Serial.println("unknow status"); break;
  }
}

void setup()
{
  Serial.begin(115200);
  bmp.reset();
  Serial.println("bmp read data test");
  while(bmp.begin() != BMP::eStatusOK) {
    Serial.println("bmp begin faild");
    printLastOperateStatus(bmp.lastOperateStatus);
    delay(2000);
  }
  Serial.println("bmp begin success");
  delay(100);
}

void loop()
{
  float   temp = bmp.getTemperature();
  uint32_t    press = bmp.getPressure();
  float   alti = bmp.calAltitude(SEA_LEVEL_PRESSURE, press);

  Serial.println();
  Serial.println("======== start print ========");
  Serial.print("temperature (unit Celsius): "); Serial.println(temp);
  Serial.print("pressure (unit pa):         "); Serial.println(press);
  Serial.print("altitude (unit meter):      "); Serial.println(alti);
  Serial.println("========  end print  ========");

  delay(1000);
}

Result

  • Read the values of the temperature, barometric pressure and altitude the BMP280 sensor module collected, and print out over the serial port. Their measurement units are ℃, pa and m. The altitude is calculated from the temperature and pressure BMP280 detected, which may lead to a certain amount of error.
    • Check the information on the serial monitor.

Result

Was this article helpful?

TOP