Projects / ESP32-C3 height gained meter

ESP32-C3 height gained meter

Intermediate2.3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Wire the sensor and display

    Both to 3V3 and GND, SDA to GPIO8, SCL to GPIO9.

  2. ESP32

    Wire the button

    GPIO3 to GND.

  3. ESP32BMP28025.7°CSSD1306

    Install the libraries

    Adafruit BMP280, Adafruit SSD1306 and Adafruit GFX.

  4. ESP32BMP28025.7°C

    Upload the sketch

    The display shows pressure and a height near 0.

  5. ESP32

    Press the button at the bottom

    Sets the starting point.

  6. ESP32

    Climb stairs

    The height gained rises by about three metres per floor.

  7. ESP32

    Come back down

    The Highest value keeps the peak.

In short

How do you build an ESP32-C3 height gained meter?

An ESP32-C3 reads a BMP280 pressure sensor and shows the pressure and the height gained since you pressed a start button, for a hill walk or a stair climb. You need ESP32-C3 SuperMini, BMP280 pressure sensor, SSD1306 OLED Display, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 7 steps and about 140 minutes.

Level
Intermediate
Time
About 140 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
BMP280 VCC and OLED VCCESP32-C3 SuperMini 3V3Power at 3.3 V
BMP280 GND and OLED GNDESP32-C3 SuperMini GNDCommon ground
BMP280 SDA and OLED SDAESP32-C3 SuperMini GPIO8Shared I2C data
BMP280 SCL and OLED SCLESP32-C3 SuperMini GPIO9Shared I2C clock
Zero buttonESP32-C3 SuperMini GPIO3 and GNDSets the starting height

Code

Air pressure falls as you climb, by about 12 pascals for each metre near sea level. The BMP280 measures the pressure very precisely, and the program turns a change in pressure into a change in height using the standard atmosphere formula. It cannot know your absolute height without knowing the weather, because the pressure also changes with the weather, so the build measures height gained from the point where you pressed the button. The sensor has built-in oversampling and filtering settings, set here to reduce noise, and the program averages eight readings on top. Expect the number to wobble by half a metre or so, and to drift slowly as the weather changes over hours. Hold the board still, out of the wind and out of direct sun, because wind across the sensor changes the pressure it sees. A hobby instrument, not a safety device.

Runs on a virtual ESP32-C3 in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_BMP280 bmp;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int BTN = 3;

float refPressure = 0;                             // pressure at the starting point, in pascals
float maxGain = 0;

float readPressure() {
  float sum = 0;
  for (int i = 0; i < 8; i++) { sum += bmp.readPressure(); delay(10); }
  return sum / 8.0;                                // average to smooth out noise
}

float heightAbove(float p, float p0) {
  return 44330.0 * (1.0 - pow(p / p0, 0.1903));    // standard atmosphere formula
}

void setup() {
  pinMode(BTN, INPUT_PULLUP);
  Wire.begin(8, 9);
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
  if (!bmp.begin(0x76)) { while (true) {} }        // use 0x77 if your module is set to that
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
                  Adafruit_BMP280::SAMPLING_X2,
                  Adafruit_BMP280::SAMPLING_X16,
                  Adafruit_BMP280::FILTER_X16,
                  Adafruit_BMP280::STANDBY_MS_500);
  delay(1000);
  refPressure = readPressure();
}

void loop() {
  if (digitalRead(BTN) == LOW) {
    refPressure = readPressure();
    maxGain = 0;
    delay(300);
  }
  float p = readPressure();
  float gain = heightAbove(p, refPressure);
  if (gain > maxGain) maxGain = gain;

  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  oled.setTextSize(1);
  oled.setCursor(0, 0);
  oled.print("Pressure "); oled.print(p / 100.0, 1); oled.print(" hPa");
  oled.setCursor(0, 14);
  oled.print("Highest "); oled.print(maxGain, 1); oled.print(" m");
  oled.setTextSize(2);
  oled.setCursor(0, 36);
  oled.print(gain, 1); oled.print(" m");
  oled.display();
  delay(300);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
At the starting pointAbout 0.0 m
Lift the board by one metreAbout 1 m gain
Climb one floorAbout 3 m
Press the buttonResets to 0
PressureAbout 1000 hPa near sea level

If it does not work

Sensor not found

Check the address 0x76 or 0x77 and the SDA and SCL wires.

Reading wobbles

Keep the board out of draughts and shade it from the sun.

Height drifts over hours

The weather changes the pressure, press the button again.

Wrong sign

The height should rise as you go up, check the formula line was not changed.

Display blank

Check the OLED address 0x3C.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ESP32-C3 height gained meter?

ESP32-C3 SuperMini, BMP280 pressure sensor, SSD1306 OLED Display, Tactile push button 6 mm and Breadboard 830 Tie Points.

How long does the ESP32-C3 height gained meter take to build?

About 140 minutes for a intermediate maker, in 7 steps.

How do I know the ESP32-C3 height gained meter works?

At the starting point: About 0.0 m. Lift the board by one metre: About 1 m gain. Climb one floor: About 3 m.

Sensor not found: what should I check?

Check the address 0x76 or 0x77 and the SDA and SCL wires.

Reading wobbles: what should I check?

Keep the board out of draughts and shade it from the sun.

Height drifts over hours: what should I check?

The weather changes the pressure, press the button again.

Wrong sign: what should I check?

The height should rise as you go up, check the formula line was not changed.