ESP32 S3 - MS5611 Pressure Sensor
Take your thumbs off the sticks of a camera drone, and it does not sink. It stays at the same height until you move a stick again. This is called altitude hold, and in many drones a small barometer such as the MS5611 feels the height for it. With an ESP32 S3 and a DIYables MS5611 module (also sold as GY-63), you can build the first block of it: a height in meters that is new about 150 times per second. Each reading holds the program up for only about 6 ms. No drone is needed, because your hand lifts the module.
What you'll build in this tutorial:
- A circuit with only four wires: 3V3, GND, GPIO8 and GPIO9
- A first sketch that prints the air pressure and the chip temperature, and also measures how long one reading stops the program
- A second sketch that keeps this stop short, makes the height calm with its own smoothing, shows it 10 times per second and counts its readings
- A short list of things that disturb such a height, and the reason why a real drone does not trust a barometer alone

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of MS5611 Pressure Sensor
The part that matters on the blue board is a silver can with two tiny holes. It is the MS5611-01BA03 from TE Connectivity, an air pressure sensor that people also call barometer, altimeter or altitude sensor. Air enters through the holes and pushes against a skin of silicon, the membrane. It is so thin that it bends a tiny bit, and the chip measures this bend with a 24-bit converter. Now think of a drone that climbs one meter: it has one meter less air above it, so the pressure drops by about 0.12 hPa (near sea level). Read the other way, 1 hPa is about 8.3 m. Pressure is given in hPa (hectopascal), the same number as in mbar. On a standard day, the air at sea level has 1013.25 hPa.
Key Specifications
The module has its own 3.3V regulator and uses I2C when you do not change anything on its pins. The right column shows what each value means when you want a height:
| Feature | Value | For your height |
|---|---|---|
| Smallest pressure step | 0.012 hPa | about 10 cm |
| Accuracy of the pressure | ±1.5 hPa between 700 and 1100 hPa, at 25°C | about ±12 m above sea level, but height changes are far better |
| Measuring range | 10 to 1200 hPa, fully accurate from 450 to 1100 hPa | 450 hPa is at about 6300 m |
| Time for one conversion | about 9 ms (best resolution) | fast enough for many values per second |
| Chip thermometer | ±0.8°C at 25°C, works from -40°C up to 85°C | needed to correct the pressure |
| Signals | I2C by default (up to 400 kHz), or SPI | two signal wires |
| I2C address | 0x77 (default) or 0x76 | two modules can share one bus |
| Supply voltage | 3.3V to 5V | on this page 3.3V |
| Board size | around 19 x 13 mm | small enough for a model |
The temperature belongs to the chip. The board around it is a bit warm, so it can read 1 to 3°C more than the air in your room. For the room itself, use a DHT22 sensor.
MS5611 Pinout
Seven pins leave the module in one row, and four of them get a wire:

- VCC - power input, fed with 3.3V by the ESP32 S3
- GND - ground, 0V
- SCL - clock line of the I2C bus
- SDA - data line of the I2C bus
- CSB - address choice: empty or on GND gives 0x77 (the module pulls it LOW itself), 3.3V gives 0x76; SPI mode uses it as chip select
- SDO - data output in SPI mode, no job in I2C mode
- PS - protocol select: the module keeps it HIGH, and this means I2C; GND means SPI
Pins 5 to 7 stay empty here. If a project needs CSB or PS at HIGH, the right level is 3.3V. Never use 5V, because the chip behind them is a 3.3V chip.
Wiring Diagram
When a sketch says nothing about I2C pins, the ESP32 S3 puts SDA on GPIO8 and SCL on GPIO9, and this page keeps it so. The two are not neighbors on the header: pins 3 and 46 sit between them, so read the labels and do not just count. No extra resistor is needed, since the module brings its own pull-ups for both lines.

This image is created using Fritzing. Click to enlarge image
| MS5611 Module Pin | ESP32 S3 Pin |
|---|---|
| VCC | 3V3 |
| GND | GND |
| SCL | GPIO9 |
| SDA | GPIO8 |
| CSB, SDO, PS | leave empty |
Motors shake the whole frame, and a wire that comes loose for a moment gives a wrong height. For everything that moves, seat the ESP32 S3 in a Screw Terminal Block for ESP32 S3 and screw the four wires down. Only GND changes its place and comes from the other row:

This image is created using Fritzing. Click to enlarge image
On the desk, you may want to plug and unplug all day. A GPIO Breakout Board for ESP32 S3 is handy for that. SDA and SCL go to the yellow S pins of IO8 and IO9, while 3.3V and GND come from the power header marked 3V3. Keep away from the 5V header right beside it:

This image is created using Fritzing. Click to enlarge image
Safety Notes
The module would accept 5V, but your ESP32 S3 would not. The pull-ups on SCL and SDA follow the voltage on VCC, so 5V on VCC would also put 5V on GPIO8 and GPIO9, and these pins are made for 3.3V only. In the first drawing, the GND pin of the black wire is the neighbor of 5Vin, so check that the red wire sits at the far end of the same row, on 3V3. Keep the two holes of the can open and dry. And if you do own a drone, take the propellers off before you put any test board on it.
How To Program For MS5611 Pressure Sensor
The MS5611 library of Rob Tillaart needs three lines before the first reading. MS5611 baro(0x77); names the I2C address, Wire.begin(); starts I2C on GPIO8 and GPIO9, and baro.begin() loads the calibration of the sensor. It returns false when nobody answers.
The only way to get a new value is read(). It starts one conversion for the pressure and one for the temperature, waits for both, and returns MS5611_READ_OK when all went well. You cannot remove this wait, but you can choose how long it is. With setOversampling(), you tell the chip how carefully it has to measure:
| Question | OSR_ULTRA_HIGH | OSR_STANDARD |
|---|---|---|
| How long does one read() wait? | about 20 ms | about 6 ms |
| How many readings are possible in one second? | about 50 | about 150 |
| How strong is the noise of one reading? | 0.012 hPa, about 10 cm | 0.027 hPa, about 22 cm |
A flight controller corrects its motors hundreds of times per second. A stop of 20 ms is far too long for such a loop(), so the second sketch takes OSR_STANDARD. The price is more noise. But three fast readings fit into the time of one slow reading, and the average of three has only 0.016 hPa of noise. Smoothing over many of them makes the height calm again. (A real flight controller also avoids the 6 ms: it starts a conversion, does other work, and fetches the result later. This library has no function for that.) Do not leave setOversampling() out, because the library then uses OSR_ULTRA_LOW, where one reading has 0.065 hPa of noise, or 54 cm.
For the height, the second sketch takes the average pressure of 300 readings and calls it 0 m. From then on, getAltitude(zero_pressure) gives the meters above this point, or below it with a minus sign. With the sea-level pressure of the day (QNH) in the brackets, the same function gives the meters above the sea. There, each wrong hPa costs about 8.3 m. Altitude hold only asks "how far from the start?", so this page does not use it.
ESP32 S3 Code - MS5611 Pressure Sensor
Start with the simple way. Once per second, this sketch takes a reading at OSR_ULTRA_HIGH and prints the pressure, the chip temperature and the time that the program had to wait.
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Wire the module: Make the four connections of the table while the USB cable is unplugged.
- Select the board: Plug in the Type-C cable, then choose ESP32S3 Dev Module and your COM port in the Arduino IDE.
- Install the library: Open the Library Manager at the left edge, type MS5611, and press Install at the entry that is named exactly MS5611, by Rob Tillaart. It needs no other library.
- Search for MS5611 created by Rob Tillaart
and click the Install button.
- Upload: Paste the code into a new sketch and click Upload.
- Open the Serial Monitor: Set it to 115200 baud. If you opened it too late for the first line, press the RST button.
- Lift the module: Let it lie for some seconds, hold it high over your head, and put it down again.
- Pro Tip: If the last digits jump, lay a small piece of open foam over the can. Air still gets in, but a breeze from a window or a fan does not.
The three lower values came from the hand in the air, about 1.6 m above the desk. 1.6 x 0.12 hPa makes 0.19 hPa, and the lines show just this. Now look at the right side. Each reading cost 20 ms, sometimes 21, and loop() could do nothing else in that time.
ESP32 S3 Code - Fast Height for Altitude Hold
In this sketch, loop() has four small parts. The first one waits about 6 ms for a reading, counts it, and mixes the new height into the smooth one. The second one prints the height every 100 ms. The third one prints the count once per second. If the count is 0, it tells you that the height is old, because the last value stays in the memory when a wire falls off. The fourth part is only a comment. It marks the place where motor control, a radio or a battery check would go, and the program comes by about 150 times per second.
Quick Steps for the Height Test
- Use the same circuit and the same library. Upload the sketch, and turn on timestamps in the Serial Monitor (115200 baud).
- Leave the module on the desk for about five seconds, until the line "Height 0 is here" appears.
- Lift it quickly by 1.5 m and hold it there.
- Bring it back to the desk and see how close the value comes to 0.
- Pro Tip: To see the trade, write OSR_ULTRA_HIGH in place of OSR_STANDARD, set SMOOTHING to 8 and upload again. The height is as calm as before, but setting height 0 now takes 6 seconds, and the count falls to about 50. Other work in loop() would get its turn only every 20 ms. Change both back after the test.
The hand needed less than a second for the way up, and the height followed right behind. It ended between 1.42 and 1.47 m in place of 1.50 m. An error between 10 and 20 cm is normal for one barometer. Why 154 readings? One read() has two conversions, and the library gives each one 2.3 ms at OSR_STANDARD. The I2C messages and the rest of loop() need almost 2 ms more. This makes about 6.5 ms, and 1000 ms / 6.5 ms is 154. Some seconds show a little more or less, which is fine.
Choose the Smoothing
With SMOOTHING at 32, each new reading moves the height by 1/32 of the difference. This calms the value, and it also fills the steps between the readings: the library gives the pressure in whole Pa, and 1 Pa is already 8 cm. But a fast move shows up later. For altitude hold, late is also bad, so you look for a number in the middle:
| SMOOTHING | Noise of the height | Time until 90% of a sudden step is visible |
|---|---|---|
| 1 (off) | about 22 cm | 0.01 s |
| 8 | about 6 cm | about 0.1 s |
| 16 | about 4 cm | about 0.2 s |
| 32 | about 3 cm | about 0.5 s |
| 64 | about 2 cm, but slow | about 1 s |
Noise is the usual error (RMS) here. A single line can be two or three times as far away.
What Spoils the Height on a Real Drone
On the desk, the sketch looks almost perfect. In the air, three things disturb the barometer:
- Propeller wind: Moving air changes the pressure at the two holes. Only 0.1 hPa too much or too little already looks like 0.8 m, and the value changes each time the motors change their speed. Drone makers put the sensor under a cover or foam, away from the strongest air stream.
- Sunlight: Direct sun on the can disturbs the reading. Shade it.
- Weather: In an hour or two the air pressure itself can move by 1 hPa, and this is 8.3 m of drift. So set height 0 again just before each start.
For these reasons, real drones mix the barometer with an accelerometer. The accelerometer feels every push at once, but a height made from it alone drifts away within seconds. The barometer is slower (the smoothing adds a delay) and noisier, but it does not drift in such a short time. Together they give a height that is fast and steady. Your sketch is the barometer half of this mix. It shows numbers and does not control a flight, so please do not fly with it.
Troubleshooting
The Serial Monitor says "No MS5611 on GPIO8 (SDA) / GPIO9 (SCL)".
A wire is loose, SDA and SCL changed places, or one of them landed on pin 3 or 46 between the right pins. Also check that PS is not on GND, because then the module speaks SPI. If CSB is on 3.3V, write MS5611 baro(0x76);.
The sketch starts, but the values make no sense.
BMP180, BMP280 and BME280 boards also use 0x77 or 0x76, and begin() can be true for them. The library cannot tell them apart, so remove other I2C boards, and read what is printed on yours.
The pressure is about 500 hPa.
Your module carries an MS5607 chip, and the math of the MS5611 shows about half of the real pressure for it. Add baro.reset(1); after begin() to switch to the other math. In the second sketch, it must come before set_height_zero().
The count is far below 150.
About 50 means that OSR_ULTRA_HIGH is still set. If not, something in loop() takes too long. Look for delay(), for a while loop that waits, and for long Serial prints at a low baud rate.
The height changes slowly while the module lies still.
Give the chip a minute to warm up, then press RST to set height 0 again. A slow drift over hours is the weather.
The height jumps although nothing moves.
Look for a fan, an open window or the sun on the can. Foam and a larger SMOOTHING help.
Application and Project Ideas
A height that is new every few milliseconds is useful for more than drones.
- Climb sound for a glider: Beep faster while a model glider is lifted by warm air, so the pilot does not have to look at a screen.
- Hoist with a stop height: Switch a small electric winch off when the hook has reached its height.
- Landing helper: Use the barometer high up, and an ultrasonic sensor for the last meters above the ground, where propeller wind disturbs the air pressure most.
Challenge Yourself
- Beginner: Set SMOOTHING to 1, then to 64. Compare the lines while the module lies still and while you lift it fast.
- Intermediate: At the marked place in loop(), calculate the error of an altitude hold: a target of 1.00 m minus height. Print UP, OK or DOWN with it, and be the drone yourself: move the module as the text says.
- Intermediate: Write OSR_LOW in place of OSR_STANDARD. The count climbs above 200, but the noise of each reading grows to 0.042 hPa. Which SMOOTHING makes the height as calm as before?
- Advanced: Draw the height as a live curve in your browser with ESP32 S3 - Web Plotter, and check that the count stays at about 150.
Video Tutorial
The video for this altitude hold sensor will be added here later.