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.

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
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) |
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

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.

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:
- Declare a sensor object with its I2C address. Only a module with CSB on 3.3V needs 0x76:
- Start the I2C bus first, and then the sensor. If nothing answers at this address, or its calibration data is empty, begin() returns false:
- Choose the finest resolution. Without this line, one reading jumps by 0.065 hPa, and that is half a meter of height:
- Read the sensor, then get the pressure and the temperature of this reading:
- 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:
- After that, every new reading also gives the height above the zero point, in meters. Below the zero point, the number is negative:
- Smooth this height, and print only the smoothed value:
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():
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.
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.
- 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.

- Open the Serial Monitor and select 9600 baud.

- 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.
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.
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.
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.