ESP32 - MS5611 Pressure Sensor

The air on top of a tall bookshelf presses a little less than the air on the floor in front of it. For a shelf of 2 m, the difference is only about 0.02 percent, and no person can feel it. The MS5611 pressure sensor can. This tutorial instructs you how to use ESP32 to read air pressure and temperature from an MS5611 module (many shops call it GY-63), and print both values to the Serial Monitor. After that, we measure furniture with the same four wires. The sensor lies on the floor, and the ESP32 takes this place as 0 cm. Lift the sensor onto a chair, a table or a shelf, and the Serial Monitor shows how many centimeters it went up. Stairs are possible too, because the sensor does not need to see the floor below it. A tape measure is more exact: with air, expect an error of about 10 to 20 cm. You will see where it comes from, and how to measure again to be sure.

ESP32 wired to an MS5611 GY-63 pressure sensor module with the module pinout

This tutorial shows how to program the ESP32 using the Arduino language (C/C++) via the Arduino IDE. If you’d like to learn how to program the ESP32 with MicroPython, visit this ESP32 MicroPython - MS5611 Pressure Sensor tutorial.

Hardware Used In This Tutorial

1×38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
1×Alternatively, 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
1×Alternatively, 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×MS5611 Pressure Sensor Module
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for ESP32
1×Recommended: Breakout Expansion Board for ESP32
1×Recommended: Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables .

Have a tape measure ready as well, to check your results. A laptop is better than a desktop PC here, because it can follow the sensor up the stairs.

Introduction to MS5611 Pressure Sensor

Pinout of the blue MS5611 (GY-63) module: VCC, GND, SCL, SDA, CSB, SDO and PS
image source: diyables.io

The silver can on this blue module is the MS5611-01BA03, made by TE Connectivity. Around it sit small helper parts, such as a voltage regulator for 3.3V and a chip that adapts the level of the I2C lines. Shops also call the module barometer, altimeter, altitude sensor or barometric sensor.

The can has two small holes. Room air comes in and pushes against a membrane of silicon, which is so thin that the push can bend it a tiny bit. The chip measures the bend with a 24-bit converter, and the library gives us the result in hPa (hectopascal, the same number as mbar). On top of a ladder, less air is above the sensor than on the floor, so the push is weaker there. One meter makes a difference of around 0.12 hPa (near sea level).

MS5611 Pressure Sensor Module
The air pressure it can measure 10 to 1200 hPa, with full accuracy between 450 and 1100 hPa
The smallest pressure step 0.012 hPa, which is about 10 cm up or down
The possible error of the pressure ±1.5 hPa (25°C, 700 to 1100 hPa)
The time for one conversion about 9 ms for the pressure, at the best resolution
The chip temperature -40°C to 85°C, with an error of ±0.8°C (at 25°C)
The voltage on VCC 3.3V to 5V (here we use 3.3V)
The interface I2C, up to 400 kHz, or SPI (here we use I2C)
The I2C address 0x77, and 0x76 is possible
The size of the board about 19 mm x 13 mm

The sensor also measures the temperature of its own chip, and the library needs it to correct the pressure. It can be 1 to 3°C warmer than the room, as the board heats itself a bit. For the room, a DHT22 is the better sensor.

MS5611 Pinout

The module has 7 pins. We use 4 of them:

  • VCC pin: connect this pin to the 3.3V pin of the ESP32. It is the power of the module.
  • GND pin: connect this pin to GND (0V).
  • SCL pin: connect this pin to GPIO22. It is the I2C clock.
  • SDA pin: connect this pin to GPIO21. It is the I2C data.
  • CSB pin: leave this pin empty. It chooses the I2C address: empty or GND is 0x77, and 3.3V is 0x76. In SPI mode, it selects the chip.
  • SDO pin: leave this pin empty. Only the SPI mode sends data through it.
  • PS pin: leave this pin empty. The module itself keeps it HIGH, and HIGH selects I2C. For SPI, it needs GND.

The pull-ups of SCL and SDA are part of the module, so there is no resistor to add. To set CSB or PS to HIGH, use 3.3V and never 5V.

Air Pressure on a Chair, a Table and a Shelf

Let us say the pressure on the floor is 1006.52 hPa. Then the sensor finds these values in a home:

Place of the sensor Tape measure Air pressure Difference to the floor
Floor 0 cm 1006.520 hPa 0 hPa
Chair 47 cm 1006.464 hPa 0.056 hPa
Table 74 cm 1006.432 hPa 0.088 hPa
Top of a bookshelf 202 cm 1006.279 hPa 0.241 hPa
One floor up, by the stairs 281 cm 1006.185 hPa 0.335 hPa

The library goes the other way: from the two pressures, it calculates the height in meters:

height = 44307.694 x (1 - (pressure / floor pressure) ^ 0.190284)

The possible error of ±1.5 hPa is more than 12 m of height. How can such a sensor measure a chair? The error is nearly the same in all readings of one sensor. If it shows 0.8 hPa too much on the floor, it also shows 0.8 hPa too much on the chair. We only use the difference, so the error goes away.

The altitude above sea level is more difficult: for it, the sketch must know the sea-level pressure of the day, and 1 hPa wrong makes about 8.3 m wrong. For the height change, we only need the floor.

Wiring Diagram

Four jumper wires are enough. GPIO21 and GPIO22 are the default I2C pins of the ESP32, so Wire.begin() in the code needs no pin numbers.

ESP32 and MS5611 pressure sensor wiring diagram

This image is created using Fritzing. Click to enlarge image

MS5611 Module ESP32
VCC → 3.3V
GND → GND
SCL → GPIO22
SDA → GPIO21
CSB, SDO, PS (not connected)

On many ESP32 boards, the 3.3V pin is printed as 3V3.

WARNING

VCC belongs on 3.3V, never on 5V (VIN). The pull-up resistors of the module bring SCL and SDA to the same voltage as VCC. From a 5V supply, GPIO22 and GPIO21 would see 5V, and the ESP32 is a 3.3V chip.

In this project the sensor travels, so make it easy to carry:

  • Put the ESP32 and the module together on a small piece of cardboard, for example with tape. No tape on the can.
  • Let the module stand the same way on the floor and on the table, then its own height does not count.
  • Keep away from direct sunlight, fans and open windows. Some open foam on top of the can stops the wind. The two holes of the can must stay open and dry, so do not touch them.

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: The best way to Power ESP32 and sensors/displays.

How To Program For MS5611

  • Include Wire for the I2C bus, and the MS5611 library by Rob Tillaart for the sensor:
#include <Wire.h> #include <MS5611.h>
  • Declare a sensor object with its I2C address. Only a module with CSB on 3.3V needs 0x76:
MS5611 ms5611(0x77);
  • Start the I2C bus first, and then the sensor. If nothing answers at this address, or its calibration data is empty, begin() returns false:
Wire.begin(); if (!ms5611.begin()) { Serial.println("No MS5611 on GPIO21 and GPIO22. Check the four wires"); }
  • Choose the finest resolution. Without this line, one reading jumps by 0.065 hPa, and that is half a meter of height:
ms5611.setOversampling(OSR_ULTRA_HIGH);
  • Read the sensor, then get the pressure and the temperature of this reading:
if (ms5611.read() == MS5611_READ_OK) { float pressure = ms5611.getPressure(); // hPa float temperature = ms5611.getTemperature(); // degrees Celsius }
  • To measure a height, make the place of the sensor the zero point. In the second sketch, measureFloor() takes the average of 50 readings for it, so one bad reading does no harm:
floorPressure = sum / FLOOR_READINGS;
  • After that, every new reading also gives the height above the zero point, in meters. Below the zero point, the number is negative:
float newHeight = ms5611.getAltitude(floorPressure);
  • Smooth this height, and print only the smoothed value:
height += (newHeight - height) / SMOOTHING;

What does the smoothing do? One reading alone can easily be wrong by 10 cm. With SMOOTHING 25, a new reading may move the value by only 1/25 of the difference. The sketch takes about 50 readings per second, so after a lift, the value needs about 3 seconds to arrive at the new height.

Do you need GPIO21 or GPIO22 for something else? The ESP32 can have I2C on other pins as well. Write them into Wire.begin():

Wire.begin(25, 26); // SDA on GPIO25, SCL on GPIO26

ESP32 Code - MS5611 Pressure Sensor

The first code prints one line per second, with the pressure in hPa and the temperature in °C and °F. It shows if the wiring is right, and you can already see the pressure fall when you lift the sensor.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-ms5611-pressure-sensor */ // ESP32 reads the air pressure and the chip temperature from the MS5611 (GY-63) pressure sensor // Wiring: VCC -> 3V3, GND -> GND, SCL -> GPIO22, SDA -> GPIO21 (the default I2C pins of the ESP32) // Library: MS5611 by Rob Tillaart #include <Wire.h> #include <MS5611.h> MS5611 ms5611(0x77); // create the MS5611 object (0x76 if the CSB pin is on 3.3V) void setup() { Serial.begin(9600); delay(500); Wire.begin(); // start I2C on GPIO21 (SDA) and GPIO22 (SCL) if (!ms5611.begin()) { Serial.println("No MS5611 on GPIO21 and GPIO22. Check the four wires"); while (true) delay(1000); // stop here } ms5611.setOversampling(OSR_ULTRA_HIGH); // the finest resolution Serial.print("MS5611 found at 0x"); Serial.print(ms5611.getAddress(), HEX); Serial.println(", start measuring"); } void loop() { if (ms5611.read() == MS5611_READ_OK) { // measure one time, about 20 ms float temperature = ms5611.getTemperature(); Serial.print("Air: "); Serial.print(ms5611.getPressure(), 2); // hPa, the same number as mbar Serial.print(" hPa | Chip: "); Serial.print(temperature, 1); // degrees Celsius Serial.print(" C = "); Serial.print(temperature * 1.8 + 32, 1); // degrees Fahrenheit Serial.println(" F"); } else { Serial.println("The MS5611 does not answer. Is a wire loose?"); } delay(1000); // about one line per second }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a USB Type-C cable.
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Type "MS5611" on the search box, and find the library with exactly this name by Rob Tillaart. MS5611_SPI and MS5xxx are other libraries.
  • Click to the Install button. No other library is needed.
  • Search for MS5611 created by Rob Tillaart and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Library Manager
Type:
All
Topic:
All
MS5611 by Rob Tillaart
Experimental, GY-63, GY63, MS5607,altitude. More info
0.5.2
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32 Dev Module on COM15
1
  • Copy the above code and open with Arduino IDE.
  • Click to the Upload button on Arduino IDE to compile and upload code to the ESP32.
How to upload ESP32 code on Arduino IDE
  • Open the Serial Monitor and select 9600 baud.
How to open serial monitor on Arduino IDE
  • Leave the sensor on the floor for four lines. Then hold it as high as your arm can reach, and after three lines put it back on the floor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
MS5611 found at 0x77, start measuring Air: 1006.53 hPa | Chip: 22.6 C = 72.7 F Air: 1006.54 hPa | Chip: 22.6 C = 72.7 F Air: 1006.54 hPa | Chip: 22.6 C = 72.8 F Air: 1006.49 hPa | Chip: 22.6 C = 72.7 F Air: 1006.28 hPa | Chip: 22.6 C = 72.7 F Air: 1006.28 hPa | Chip: 22.6 C = 72.7 F Air: 1006.26 hPa | Chip: 22.6 C = 72.7 F Air: 1006.53 hPa | Chip: 22.6 C = 72.7 F Air: 1006.52 hPa | Chip: 22.6 C = 72.7 F
Ln 11, Col 1
ESP32 Dev Module on COM15
2

In the example, the arm was 2.1 m above the floor, and the pressure fell by about 0.25 hPa. The numbers also move by some hundredths of an hPa while the sensor lies still. There is no smoothing yet, and 0.01 hPa is already more than 8 cm of height.

Your own pressure can be much lower, if you live high above the sea.

ESP32 Code - Measure Furniture with Air

The second code waits 3 seconds, makes the floor 0 cm, and then reads the sensor without a pause. Twice per second, it prints the height change in whole centimeters and in meters.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-ms5611-pressure-sensor */ // ESP32 measures furniture with air: the floor is 0 cm, then the MS5611 shows how far it was lifted // Wiring: VCC -> 3V3, GND -> GND, SCL -> GPIO22, SDA -> GPIO21 (the default I2C pins of the ESP32) // Library: MS5611 by Rob Tillaart #include <Wire.h> #include <MS5611.h> #define SMOOTHING 25 // 1 = off, a bigger number = a steadier but slower value #define FLOOR_READINGS 50 // the average of these readings is the floor (0 cm) #define PRINT_INTERVAL 500 // milliseconds between two lines MS5611 ms5611(0x77); // create the MS5611 object (0x76 if the CSB pin is on 3.3V) float floorPressure = 0; // hPa, the air pressure on the floor float height = 0; // meters above the floor, smoothed unsigned long lastPrintTime = 0; // average the air pressure on the floor, takes about 1 second bool measureFloor() { float sum = 0; for (int i = 0; i < FLOOR_READINGS; i++) { if (ms5611.read() != MS5611_READ_OK) return false; sum += ms5611.getPressure(); } floorPressure = sum / FLOOR_READINGS; return true; } void setup() { Serial.begin(9600); delay(500); Wire.begin(); // start I2C on GPIO21 (SDA) and GPIO22 (SCL) if (!ms5611.begin()) { Serial.println("No MS5611 on GPIO21 and GPIO22. Check the four wires"); while (true) delay(1000); // stop here } ms5611.setOversampling(OSR_ULTRA_HIGH); // the finest resolution Serial.println("Put the sensor on the floor and take your hands away..."); delay(3000); if (!measureFloor()) { Serial.println("The MS5611 does not answer. Is a wire loose?"); while (true) delay(1000); // stop here } Serial.println("The floor is 0 cm. Lift the sensor!"); } void loop() { // read without a pause, because the smoothing needs many readings if (ms5611.read() != MS5611_READ_OK) { Serial.println("The MS5611 does not answer. Is a wire loose?"); delay(500); return; } float newHeight = ms5611.getAltitude(floorPressure); // meters above the floor, this reading height += (newHeight - height) / SMOOTHING; // move a small step towards it if (millis() - lastPrintTime >= PRINT_INTERVAL) { lastPrintTime = millis(); int cm = round(height * 100); // meters to whole centimeters Serial.print("Height change: "); Serial.print(cm); Serial.print(" cm | "); Serial.print(cm / 100.0, 2); Serial.println(" m"); } }

Quick Instructions

  • Copy the above code and open with Arduino IDE.
  • Click to the Upload button on Arduino IDE to upload code to the ESP32.
  • Open the Serial Monitor (9600 baud).
  • Give the sensor a few minutes with power on, so that its temperature becomes steady.
  • Lay the cardboard with the ESP32 and the sensor on the floor next to the table.
  • Press the EN button of the ESP32. The sketch starts again. Take your hands away until you see "The floor is 0 cm".
  • Lift the cardboard onto the table. Wait 5 seconds, then read the number.
  • Put the cardboard back on the floor. Wait 5 seconds and read the number again.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Put the sensor on the floor and take your hands away... The floor is 0 cm. Lift the sensor! Height change: -1 cm | -0.01 m Height change: -2 cm | -0.02 m Height change: -6 cm | -0.06 m Height change: 3 cm | 0.03 m Height change: 25 cm | 0.25 m Height change: 49 cm | 0.49 m Height change: 59 cm | 0.59 m Height change: 66 cm | 0.66 m Height change: 71 cm | 0.71 m Height change: 75 cm | 0.75 m Height change: 79 cm | 0.79 m Height change: 81 cm | 0.81 m Height change: 78 cm | 0.78 m Height change: 81 cm | 0.81 m Height change: 83 cm | 0.83 m Height change: 72 cm | 0.72 m Height change: 47 cm | 0.47 m Height change: 26 cm | 0.26 m Height change: 14 cm | 0.14 m Height change: 8 cm | 0.08 m Height change: 8 cm | 0.08 m Height change: 1 cm | 0.01 m Height change: 0 cm | 0.00 m Height change: 2 cm | 0.02 m
Ln 11, Col 1
ESP32 Dev Module on COM15
2

The real height of this table is 74 cm, and the air says about 80 cm. At the end, the sensor is on the floor again and shows 2 cm. That is close to 0, so we can trust this measurement.

※ NOTE THAT:

When the ESP32 loses power, or when you press EN, the place of the sensor at that moment becomes the new 0 cm. So press EN only on the floor, and do not unplug the USB cable on the way to the table.

How to Repeat a Measurement

One measurement is not enough. Do it three times, always in this order:

  • Floor: the Serial Monitor shows about 0 cm.
  • Table: wait 5 seconds and write down the number.
  • Floor again: wait 5 seconds. If it shows between -10 and 10 cm, the result is good. If not, forget the result, press EN and start again.

At the end, take the middle one of your three results. For a staircase, start on the floor at the foot of the stairs, carry the laptop and the cardboard up, read, and come back down for the check.

Line-by-line Code Explanation

Please read the comments in both codes!

How Exact Is It?

With a little practice, your list can look like this example:

Thing Tape measure Air, first Air, second Air, third Middle result
Chair 47 cm 53 cm 39 cm 50 cm 50 cm
Table 74 cm 80 cm 69 cm 77 cm 77 cm
Top of a bookshelf 202 cm 191 cm 205 cm 196 cm 196 cm
Stairs, one floor 281 cm 266 cm 279 cm 270 cm 270 cm

Single results in this list are up to 15 cm away from the tape measure, and 20 cm can happen too. That is too much for a footstool, but fine for a bookshelf or a staircase. Where does the error come from?

  • The sensor: its smallest step is 0.012 hPa, about 10 cm. Many smoothed readings make this better, but not perfect.
  • The room: a door that opens, a fan or wind at the house changes the pressure for a moment.
  • The weather: the pressure outside changes all the time, and the house follows it. With 1 hPa in one hour, the height moves away by about 14 cm every minute. So be quick: floor, table and floor again in less than one minute.
  • The room temperature: the formula always calculates with 15°C. At 25°C, the air weighs less, and a staircase of 281 cm reads about 9 cm too low. For a table, it is only 2 to 3 cm.

If you need an exact number, use sound instead of air pressure: ESP32 - Ultrasonic Sensor. It needs a free view down to the floor, so it cannot measure a staircase with a corner. The MS5611 can.

Troubleshooting

  • "No MS5611 on GPIO21 and GPIO22": SCL and SDA are easy to swap, so check them first. Then check that VCC is on 3.3V, that GND is connected, and that nothing is on the PS pin. If CSB is on 3.3V, change 0x77 to 0x76 in the sketch.
  • The pressure or the temperature cannot be true: BMP280, BME280 and BMP180 boards answer at these addresses too, and begin() may accept them. Is it really an MS5611 that you connected? Read the print on the module.
  • The air pressure is only around 500 hPa: the chip on your module is an MS5607. In setup(), write ms5611.reset(1); in the line above ms5611.setOversampling().
  • The height jumps by 20 cm or more while the sensor lies still: look for wind, a fan or sunlight. Close the window and use the foam.
  • The height slowly moves away from 0: the weather is changing, or the sensor is still warming up. Wait some minutes, then press EN on the floor.

What Comes Next

Without a laptop, the measuring is much easier: show the centimeters on a small screen, see ESP32 - OLED. The OLED is an I2C device too, so it goes on the same two data pins, next to the MS5611. Another idea is to measure all floors of your house, one staircase after the other, with a new 0 cm for each of them. Keep the sensor out of water and tires: it is built for the air around us, and it must stay dry.

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES