BMP180 Sensor with ESP32 ESP-IDF

In this tutorial, we will learn how to use BMP180 barometric sensor module with ESP32 using ESP-IDF. Firstly, we will introduce you to the sensor, then interface it with our development board and program it for temperature and pressure readings measurement using ESP-IDF. For demonstration, we will first measure the BMP180 sensor data and print them on the ESP-IDF console. Afterwards, we will connect an SSD1306 OLED with the ESP32 board to view the sensor measurements on the OLED as well.

Before we move ahead, make sure you have the latest version of VS Code installed on your system with the ESP-IDF extension configured.

BMP180 Introduction

A BMP180 sensor is commonly used to measure atmospheric and biometric pressure although temperature and altitude measurements can also be made. It also allows us to measure pressure at sea level along with real altitude as well. Having a high precision along with being cost effective, this barometric sensor works very well for consumer applications which include weather forecast, sports devices, GPS, navigation, etc. Moreover, as the BMP180 sensor is able to measure pressure with altitude with great accuracy, therefore it can also act as an altimeter.

Lets discuss some of the key features of the BMP180 sensor.

  • The BMP180 sensor uses I2C protocol to connect and communicate with microcontrollers.
  • It is operable between 1.8 to 3.6V.
  • It can measure atmospheric pressure within the range of 300-1100 hPa.
  • Has low noise and low power consumption. Approximately 5µA at 1 sample/s in standard mode.
  • Can also be used to measure temperature.

BMP180 Sensor Pinout

The BMP180 sensor module has a compact size and is readily available in markets. The picture below shows a typical BMP180 sensor module.

BMP180 Sensor Module
BMP180 Sensor Module

The BMP180 senor module that we will use in this guide consists of 4 pins. Two are power supply pins (Vcc and Gnd) and two are the I2C communication pins (SCL and SDA). The table below briefly describes the four pins.

VccThe Vcc pin powers the BMP180 sensor. Connect it with 3.3-5V.
GNDThis is the ground pin which is used to provide the common ground.
SCLThis is the serial clock pin used for clock pulse. Connect it with SCL pin of the microcontroller.
SDAThis is the serial data pin used for serial communication. Connect it with SDA pin of the microcontroller.

Interfacing BMP180 with ESP32

We will need the following components to connect our ESP32 board with the BMP180 sensor.

  1. ESP32 board
  2. BMP180 sensor
  3. Connecting Wires

The connections between the devices are specified in the table below:

BMP180 PinESP32
VccThe VCC pin will be connected with 3.3V pin of the ESP32 board.
GndCommon ground with ESP32.
SCLGPIO22 is the default SCL pin for ESP32.
SDAGPIO21 is the default SDA pin for ESP32.

Connect the BMP180 sensor with the ESP32 development board as shown in the schematic diagram below:

ESP32 with BMP180 Sensor schematic diagram
ESP32 with BMP180 connection diagram

ESP32 BMP180 Sensor with ESP-IDF

We will build and create a project in VS Code with ESP-IDF extension to display BMP180 sensor data including temperature, pressure and altitude. The code will read the sensor data and print them in the ESP-IDF terminal after every second. We will use the BMP180 library provided by krzychb on GitHub.

Create Example Project

Open your VS Code and head over to View > Command Palette. Type ESP-IDF: New Project in the search bar and press enter.

Specify the project name and directory, along with the ESP-IDF board and target. For the ESP-IDF board, we have chosen the custom board option. For ESP-IDF target, we have chosen ESP32 module. Click ‘Choose Template’ button to proceed forward.

In the Extension, select ESP-IDF option:

ESP-IDF in VS Code New Project 2

We will click the ‘sample_project’ under the get-started tab. Now click ‘Create project using template sample_project.’

ESP-IDF in VS Code New Project 3

You will get a notification that the project has been created. To open the project in a new window, click ‘Yes.’

This opens the project that we created inside the EXPLORER tab. There are several folders inside our project folder. This is the same for every project which you will create through ESP-IDF Explorer.

  • First, let’s add the necessary header files for the libraries required for this project. Create a new folder called ‘components’ and add the three files listed below:
  1. bmp180.c
  2. bmp180.h
  3. CMakeLists.txt

bmp180.c

/*
 bmp180.c - BMP180 pressure sensor driver for ESP32
 This file is part of the ESP32 Everest Run project
 https://github.com/krzychb/esp32-everest-run
 Copyright (c) 2016 Krzysztof Budzynski <krzychb@gazeta.pl>
 This work is licensed under the Apache License, Version 2.0, January 2004
 See the file LICENSE for details.
 */

#include <math.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "driver/i2c.h"
#include "esp_log.h"

#include "bmp180.h"

static const char* TAG = "BMP180 I2C Driver";

#define ACK_CHECK_EN    0x1     // I2C master will check ack from slave
#define ACK_CHECK_DIS   0x0     // I2C master will not check ack from slave
#define ACK_VAL         0x0     // I2C ack value
#define NACK_VAL        0x1     // I2C nack value

#define BMP180_ADDRESS 0x77     // I2C address of BMP180

#define BMP180_ULTRA_LOW_POWER  0
#define BMP180_STANDARD         1
#define BMP180_HIGH_RES         2
#define BMP180_ULTRA_HIGH_RES   3

#define BMP180_CAL_AC1          0xAA  // Calibration data (16 bits)
#define BMP180_CAL_AC2          0xAC  // Calibration data (16 bits)
#define BMP180_CAL_AC3          0xAE  // Calibration data (16 bits)
#define BMP180_CAL_AC4          0xB0  // Calibration data (16 bits)
#define BMP180_CAL_AC5          0xB2  // Calibration data (16 bits)
#define BMP180_CAL_AC6          0xB4  // Calibration data (16 bits)
#define BMP180_CAL_B1           0xB6  // Calibration data (16 bits)
#define BMP180_CAL_B2           0xB8  // Calibration data (16 bits)
#define BMP180_CAL_MB           0xBA  // Calibration data (16 bits)
#define BMP180_CAL_MC           0xBC  // Calibration data (16 bits)
#define BMP180_CAL_MD           0xBE  // Calibration data (16 bits)

#define BMP180_CONTROL             0xF4  // Control register
#define BMP180_DATA_TO_READ        0xF6  // Read results here
#define BMP180_READ_TEMP_CMD       0x2E  // Request temperature measurement
#define BMP180_READ_PRESSURE_CMD   0x34  // Request pressure measurement

// Calibration parameters
static int16_t ac1;
static int16_t ac2;
static int16_t ac3;
static uint16_t ac4;
static uint16_t ac5;
static uint16_t ac6;
static int16_t b1;
static int16_t b2;
static int16_t mb;
static int16_t mc;
static int16_t md;
static uint8_t oversampling = BMP180_ULTRA_HIGH_RES;


static esp_err_t bmp180_master_write_slave(i2c_port_t i2c_num, uint8_t* data_wr, size_t size)
{
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, ( BMP180_ADDRESS  << 1 ) | I2C_MASTER_WRITE, ACK_CHECK_EN);
    i2c_master_write(cmd, data_wr, size, ACK_CHECK_EN);
    i2c_master_stop(cmd);
    esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
    return ret;
}


static esp_err_t bmp180_write_reg(i2c_port_t i2c_num, uint8_t reg, uint8_t cmd)
{
    uint8_t data_wr[] = {reg, cmd};
    esp_err_t err = bmp180_master_write_slave(i2c_num, data_wr, 2);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Write [0x%02x] = 0x%02x failed, err = %d", reg, cmd, err);
    }
    return err;
}


static esp_err_t bmp180_master_read_slave(i2c_port_t i2c_num, uint8_t* data_rd, size_t size)
{
    if (size == 0) {
        return ESP_OK;
    }
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, ( BMP180_ADDRESS << 1 ) | I2C_MASTER_READ, ACK_CHECK_EN);
    if (size > 1) {
        i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
    }
    i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
    i2c_master_stop(cmd);
    esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
    return ret;
}


static esp_err_t bmp180_read_int16(i2c_port_t i2c_num, uint8_t reg, int16_t* value)
{
    esp_err_t err = bmp180_master_write_slave(i2c_num, &reg, 1);
    if (err == ESP_OK) {
        uint8_t data_rd[2] = {0};
        err = bmp180_master_read_slave(i2c_num, data_rd, 2);
        if (err == ESP_OK) {
            *value = (int16_t) ((data_rd[0] << 8) | data_rd[1]);
        }
    }
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Read [0x%02x] int16 failed, err = %d", reg, err);
    }
    return err;
}


static esp_err_t bmp180_read_uint16(i2c_port_t i2c_num, uint8_t reg, uint16_t* value)
{
    esp_err_t err = bmp180_master_write_slave(i2c_num, &reg, 1);
    if (err == ESP_OK) {
        uint8_t data_rd[2] = {0};
        err = bmp180_master_read_slave(i2c_num, data_rd, 2);
        if (err == ESP_OK) {
            *value = (uint16_t) ((data_rd[0] << 8) | data_rd[1]);
        }
    }
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Read [0x%02x] uint16 failed, err = %d", reg, err);
    }
    return err;
}


static esp_err_t bmp180_read_uint32(i2c_port_t i2c_num, uint8_t reg, uint32_t* value)
{
    esp_err_t err = bmp180_master_write_slave(i2c_num, &reg, 1);
    if (err == ESP_OK) {
        uint8_t data_rd[3] = {0};
        err = bmp180_master_read_slave(i2c_num, data_rd, 3);
        if (err == ESP_OK) {
            *value = (uint32_t) ((data_rd[0] << 16) | (data_rd[1] << 8) | data_rd[2]);
        }
    }
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Read [0x%02x] uint16 failed, err = %d", reg, err);
    }
    return err;
}


static esp_err_t bmp180_read_uncompensated_temperature(int16_t* temp)
{
    esp_err_t err = bmp180_write_reg(I2C_NUM_0, BMP180_CONTROL, BMP180_READ_TEMP_CMD);
    if (err == ESP_OK) {
        TickType_t xDelay = 5 / portTICK_PERIOD_MS;
        if (xDelay == 0) {
            xDelay = 1;
        }
        vTaskDelay(xDelay);
        err = bmp180_read_int16(I2C_NUM_0, BMP180_DATA_TO_READ, temp);
    }
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Read uncompensated temperature failed, err = %d", err);
    }
    return err;
}


static esp_err_t bmp180_calculate_b5(int32_t* b5)
{
    int16_t ut;
    int32_t x1, x2;

    esp_err_t err = bmp180_read_uncompensated_temperature(&ut);
    if (err == ESP_OK) {
        x1 = ((ut - (int32_t) ac6) * (int32_t) ac5) >> 15;
        x2 = ((int32_t) mc << 11) / (x1 + md);
        *b5 = x1 + x2;
    } else {
        ESP_LOGE(TAG, "Calculate b5 failed, err = %d", err);
    }
    return err;
}

static uint32_t bmp180_read_uncompensated_pressure(uint32_t* up)
{
    esp_err_t err = bmp180_write_reg(I2C_NUM_0, BMP180_CONTROL, BMP180_READ_PRESSURE_CMD + (oversampling << 6));
    if (err == ESP_OK) {
        TickType_t xDelay = (2 + (3 << oversampling)) / portTICK_PERIOD_MS;
        if (xDelay == 0) {
            xDelay = 1;
        }
        vTaskDelay(xDelay);
        err = bmp180_read_uint32(I2C_NUM_0, BMP180_DATA_TO_READ, up);
        if (err == ESP_OK) {
            *up >>= (8 - oversampling);
        }
    }
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Read uncompensated pressure failed, err = %d", err);
    }
    return err;
}


esp_err_t bmp180_read_temperature(float* temperature)
{
    int32_t b5;

    esp_err_t err = bmp180_calculate_b5(&b5);
    if (err == ESP_OK) {
        *temperature = ((b5 + 8) >> 4) / 10.0;
    } else {
        ESP_LOGE(TAG, "Read temperature failed, err = %d", err);
    }
    return err;
}


esp_err_t bmp180_read_pressure(uint32_t* pressure)
{
    int32_t b3, b5, b6, x1, x2, x3, p;
    uint32_t up, b4, b7;
    esp_err_t err;

    err = bmp180_calculate_b5(&b5);
    if (err == ESP_OK) {
        b6 = b5 - 4000;
        x1 = (b2 * (b6 * b6) >> 12) >> 11;
        x2 = (ac2 * b6) >> 11;
        x3 = x1 + x2;
        b3 = (((((int32_t)ac1) * 4 + x3) << oversampling) + 2) >> 2;

        x1 = (ac3 * b6) >> 13;
        x2 = (b1 * ((b6 * b6) >> 12)) >> 16;
        x3 = ((x1 + x2) + 2) >> 2;
        b4 = (ac4 * (uint32_t)(x3 + 32768)) >> 15;

        err  = bmp180_read_uncompensated_pressure(&up);
        if (err == ESP_OK) {
            b7 = ((uint32_t)(up - b3) * (50000 >> oversampling));
            if (b7 < 0x80000000) {
                p = (b7 << 1) / b4;
            } else {
                p = (b7 / b4) << 1;
            }

            x1 = (p >> 8) * (p >> 8);
            x1 = (x1 * 3038) >> 16;
            x2 = (-7357 * p) >> 16;
            p += (x1 + x2 + 3791) >> 4;
            *pressure = p;
        }
    }

    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Pressure compensation failed, err = %d", err);
    }

    return err;
}


esp_err_t bmp180_read_altitude(uint32_t reference_pressure, float* altitude)
{
    uint32_t absolute_pressure;
    esp_err_t err = bmp180_read_pressure(&absolute_pressure);
    if (err == ESP_OK) {
        *altitude =  44330 * (1.0 - powf(absolute_pressure / (float) reference_pressure, 0.190295));
    } else {
        ESP_LOGE(TAG, "Read altitude failed, err = %d", err);
    }
    return err;
}


esp_err_t bmp180_init(int pin_sda, int pin_scl)
{
    esp_err_t err;

    i2c_config_t conf;
    conf.mode = I2C_MODE_MASTER;
    conf.sda_io_num = pin_sda;
    conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
    conf.scl_io_num = pin_scl;
    conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
    conf.master.clk_speed = 100000;
    conf.clk_flags = 0;

    err = i2c_param_config(I2C_NUM_0, &conf);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "I2C driver configuration failed with error = %d", err);
        return ESP_ERR_BMP180_NOT_DETECTED;
    }
    i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "I2C driver installation failed with error = %d", err);
        return ESP_ERR_BMP180_NOT_DETECTED;
    }
    ESP_LOGI(TAG, "I2C master driver has been installed.");

    uint8_t reg = 0x00;
    err = bmp180_master_write_slave(I2C_NUM_0, &reg, 1);

    if (err != ESP_OK) {
        ESP_LOGE(TAG, "BMP180 sensor not found at 0x%02x", BMP180_ADDRESS);
        return ESP_ERR_BMP180_NOT_DETECTED;
    }

    ESP_LOGI(TAG, "BMP180 sensor found at 0x%02x", BMP180_ADDRESS);
    err  = bmp180_read_int16(I2C_NUM_0, BMP180_CAL_AC1, &ac1);
    err |= bmp180_read_int16(I2C_NUM_0, BMP180_CAL_AC2, &ac2);
    err |= bmp180_read_int16(I2C_NUM_0, BMP180_CAL_AC3, &ac3);
    err |= bmp180_read_uint16(I2C_NUM_0, BMP180_CAL_AC4, &ac4);
    err |= bmp180_read_uint16(I2C_NUM_0, BMP180_CAL_AC5, &ac5);
    err |= bmp180_read_uint16(I2C_NUM_0, BMP180_CAL_AC6, &ac6);
    err |= bmp180_read_int16(I2C_NUM_0, BMP180_CAL_B1, &b1);
    err |= bmp180_read_int16(I2C_NUM_0, BMP180_CAL_B2, &b2);
    err |= bmp180_read_int16(I2C_NUM_0, BMP180_CAL_MB, &mb);
    err |= bmp180_read_int16(I2C_NUM_0, BMP180_CAL_MC, &mc);
    err |= bmp180_read_int16(I2C_NUM_0, BMP180_CAL_MD, &md);

    if (err != ESP_OK) {
        ESP_LOGE(TAG, "BMP180 sensor calibration read failure, err = %d", err);
        return ESP_ERR_BMP180_CALIBRATION_FAILURE;
    }

    ESP_LOGI(TAG, "AC1: %d, AC2: %d, AC3: %d, AC4: %d, AC5: %d, AC6: %d", ac1, ac2, ac3, ac4, ac5, ac6);
    ESP_LOGI(TAG, "B1: %d, B2: %d, MB: %d, MC: %d, MD: %d", b1, b2, mb, mc, md);

    return ESP_OK;
}

bmp180.h

/*
 bmp180.h - BMP180 pressure sensor I2C driver for ESP32
 This file is part of the ESP32 Everest Run project
 https://github.com/krzychb/esp32-everest-run
 Copyright (c) 2016 Krzysztof Budzynski <krzychb@gazeta.pl>
 This work is licensed under the Apache License, Version 2.0, January 2004
 See the file LICENSE for details.
*/

#ifndef BMP180_H
#define BMP180_H

#include "esp_err.h"

#ifdef __cplusplus
extern "C" {
#endif

#define ESP_ERR_BMP180_BASE                  0x30000
#define ESP_ERR_TOO_SLOW_TICK_RATE           (ESP_ERR_BMP180_BASE + 1)
#define ESP_ERR_BMP180_NOT_DETECTED          (ESP_ERR_BMP180_BASE + 2)
#define ESP_ERR_BMP180_CALIBRATION_FAILURE   (ESP_ERR_BMP180_BASE + 3)

esp_err_t bmp180_init(int pin_sda, int pin_scl);
esp_err_t bmp180_read_temperature(float* temperature);
esp_err_t bmp180_read_pressure(uint32_t* pressure);
esp_err_t bmp180_read_altitude(uint32_t reference_pressure, float* altitude);

#ifdef __cplusplus
}
#endif

#endif  // BMP180_H

CMakeLists.txt

idf_component_register(SRCS "bmp180.c"
                    INCLUDE_DIRS .)

Now head over to the main.c file. The main folder contains the source code meaning the main.c file will be found here. Go to main > main.c and open it. Copy the code given below in that file and save it.

ESP32 ESP-IDF BMP180 Sensor Code

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"

#include "bmp180.h"

static const char *TAG = "BMP180 I2C Read";

#define REFERENCE_PRESSURE 101325l

#define I2C_PIN_SDA 21
#define I2C_PIN_SCL 22

void bmp180_task(void *pvParameter)
{
    while(1) {
        esp_err_t err;
        uint32_t pressure;
        float altitude;
        float temperature;

        err = bmp180_read_pressure(&pressure);
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "Reading of pressure from BMP180 failed, err = %d", err);
        }
        err = bmp180_read_altitude(REFERENCE_PRESSURE, &altitude);
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "Reading of altitude from BMP180 failed, err = %d", err);
        }
        err = bmp180_read_temperature(&temperature);
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "Reading of temperature from BMP180 failed, err = %d", err);
        }
        ESP_LOGI(TAG, "Pressure %d Pa, Altitude %.1f m, Temperature : %.1f degC", pressure, altitude, temperature);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}


void app_main()
{
    esp_err_t err;

    err = bmp180_init(I2C_PIN_SDA, I2C_PIN_SCL);
    if(err == ESP_OK){
        xTaskCreate(&bmp180_task, "bmp180_task", 1024*4, NULL, 5, NULL);
    } else {
        ESP_LOGE(TAG, "BMP180 init failed with error = %d", err);
    }
}

How the Code Works?

We will start off by including all the necessary libraries for this project. This includes the FreeRTOS libraries to create the tasks and generate delays, gpio driver to configure the I2C GPIO pins of ESP32, esp_log.h as the logging library, and bmp180.h to use the BMP180 sensor functionality.

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"

#include "bmp180.h"

This code uses Informational Logging. The log function takes in two arguments. The first argument is the tag and the second argument is a formatted string. Therefore this global variable will be useful while calling the ESP_LOGI() functions. Here, “BMP180 I2C Read” and is the tag that will be used while logging.

static const char *TAG = "BMP180 I2C Read";

We will define the sea level pressure to set as the reference pressure.

#define REFERENCE_PRESSURE 101325l

Then we have the definitions for the SCL and SDA pins that will be used for I2C communication between the ESP32 board and the BMP180 sensor. We are using the default I2C pins, however you can define other GPIO pins as SDA/SCL pins as well.

#define I2C_PIN_SDA 21
#define I2C_PIN_SCL 22

Inside the bmp180_task(), we will start capturing the BMP180 sensor readings and print them on the ESP-IDF terminal after every second.

void bmp180_task(void *pvParameter)
{
    while(1) {
        esp_err_t err;
        uint32_t pressure;
        float altitude;
        float temperature;

        err = bmp180_read_pressure(&pressure);
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "Reading of pressure from BMP180 failed, err = %d", err);
        }
        err = bmp180_read_altitude(REFERENCE_PRESSURE, &altitude);
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "Reading of altitude from BMP180 failed, err = %d", err);
        }
        err = bmp180_read_temperature(&temperature);
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "Reading of temperature from BMP180 failed, err = %d", err);
        }
        ESP_LOGI(TAG, "Pressure %d Pa, Altitude %.1f m, Temperature : %.1f degC", pressure, altitude, temperature);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

The following functions are used to read the pressure, altitude and temperature readings respectively.

err = bmp180_read_pressure(&pressure);
err = bmp180_read_altitude(REFERENCE_PRESSURE, &altitude);
err = bmp180_read_temperature(&temperature);

If the ESP error response is not ESP_OK, then an error message will be printed for that particular sensor reading

       err = bmp180_read_pressure(&pressure);
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "Reading of pressure from BMP180 failed, err = %d", err);
        }
        err = bmp180_read_altitude(REFERENCE_PRESSURE, &altitude);
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "Reading of altitude from BMP180 failed, err = %d", err);
        }
        err = bmp180_read_temperature(&temperature);
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "Reading of temperature from BMP180 failed, err = %d", err);
        }

Otherwise, will print out the pressure, altitude and temperature readings on the ESP-IDF terminal along with their units after every second.

ESP_LOGI(TAG, "Pressure %d Pa, Altitude %.1f m, Temperature : %.1f degC", pressure, altitude, temperature);
 vTaskDelay(1000 / portTICK_PERIOD_MS);

Inside the app_main() function, we will first initialize the BMP180 sensor by calling bmp180_init() and pass the SDA and SCL pins as parameters inside it. After that, we will check the response of the sensor initialization. If the sensor is successfully initialized, then we will create the bmp180 task using the xTaskCreate() function. Otherwise, the BMP180 init failure message along with the error code will be printed on the terminal.

void app_main()
{
    esp_err_t err;

    err = bmp180_init(I2C_PIN_SDA, I2C_PIN_SCL);
    if(err == ESP_OK){
        xTaskCreate(&bmp180_task, "bmp180_task", 1024*4, NULL, 5, NULL);
    } else {
        ESP_LOGE(TAG, "BMP180 init failed with error = %d", err);
    }
}

Compiling the Sketch

To flash your chip, type the following command in the serial terminal. Remember to replace the COM port with the one through which your board is connected.

idf.py -p COMX flash monitor

After the code is successfully flashed, you can view the different pressure, altitude and temperature readings updating in the console every second.

ESP32 with BMP180 Sensor ESP-IDF print readings on terminal

You may also like to read:

1 thought on “BMP180 Sensor with ESP32 ESP-IDF”

  1. espressif cannot find file driver/i2c.h, I get error message: driver/i2c.h: No such file or directory. Inlellisense does not work in bmp180.c file at all, but in different programs it finds h-files without problems. Can you help me?

    Reply

Leave a Comment