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:

  1. A circuit with only four wires: 3V3, GND, GPIO8 and GPIO9
  2. A first sketch that prints the air pressure and the chip temperature, and also measures how long one reading stops the program
  3. 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
  4. A short list of things that disturb such a height, and the reason why a real drone does not trust a barometer alone
ESP32 S3 board wired to the MS5611 GY-63 pressure sensor over GPIO8 and GPIO9

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×Alternatively, ESP32 S3 SuperMini Dev Module
1×Alternatively, ESP32 S3 Uno-form Board
1×Alternatively, ESP32 S3 Camera 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×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for ESP32 S3
1×Recommended: Breakout Expansion Board for ESP32 S3
1×Recommended: Power Splitter for ESP32 S3

Or you can buy the following kits:

1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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:

MS5611 GY-63 pressure sensor module pinout with VCC, GND, SCL, SDA, CSB, SDO and PS
  1. VCC - power input, fed with 3.3V by the ESP32 S3
  2. GND - ground, 0V
  3. SCL - clock line of the I2C bus
  4. SDA - data line of the I2C bus
  5. 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
  6. SDO - data output in SPI mode, no job in I2C mode
  7. 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.

The wiring diagram between ESP32 S3 MS5611 pressure sensor

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:

The wiring diagram between ESP32 S3 MS5611 pressure sensor Screw Terminal

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:

The wiring diagram between ESP32 S3 MS5611 pressure sensor GPIO Breakout Board

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.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-ms5611-pressure-sensor */ #include <Wire.h> #include <MS5611.h> // MS5611 library by Rob Tillaart // I2C wires: SDA of the MS5611 -> GPIO8, SCL -> GPIO9 // These are the default I2C pins of the ESP32 S3, so Wire.begin() needs no pin numbers #define READ_EVERY_MS 1000 // pause between two readings MS5611 baro(0x77); // baro = barometer, the sensor object. If CSB is on 3.3V, write 0x76 void setup() { Serial.begin(115200); delay(1000); // time to open the Serial Monitor Wire.begin(); // start I2C if (!baro.begin()) { Serial.println("No MS5611 on GPIO8 (SDA) / GPIO9 (SCL). Check the four wires"); while (true) delay(1000); // stop here } baro.setOversampling(OSR_ULTRA_HIGH); // least noise, longest wait Serial.print("MS5611 ready, I2C address 0x"); Serial.println(baro.getAddress(), HEX); } void loop() { unsigned long start_time = millis(); int result = baro.read(); // waits until the reading is finished unsigned long waiting_time = millis() - start_time; // how long loop() had to wait if (result == MS5611_READ_OK) { Serial.print("Pressure: "); Serial.print(baro.getPressure(), 2); // hPa, the same number as mbar Serial.print(" hPa | Chip: "); Serial.print(baro.getTemperature(), 2); // temperature of the chip, not of the room Serial.print(" °C | read() took "); Serial.print(waiting_time); Serial.println(" ms"); } else { Serial.println("The MS5611 does not answer. Is a wire loose?"); } delay(READ_EVERY_MS); }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the module: Make the four connections of the table while the USB cable is unplugged.
  3. Select the board: Plug in the Type-C cable, then choose ESP32S3 Dev Module and your COM port in the Arduino IDE.
  4. 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.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 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
ESP32S3 Dev Module on COM15
1
  1. Upload: Paste the code into a new sketch and click Upload.
  2. Open the Serial Monitor: Set it to 115200 baud. If you opened it too late for the first line, press the RST button.
  3. Lift the module: Let it lie for some seconds, hold it high over your head, and put it down again.
  4. 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.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
MS5611 ready, I2C address 0x77 Pressure: 1007.85 hPa | Chip: 26.31 °C | read() took 20 ms Pressure: 1007.85 hPa | Chip: 26.31 °C | read() took 20 ms Pressure: 1007.87 hPa | Chip: 26.32 °C | read() took 20 ms Pressure: 1007.84 hPa | Chip: 26.34 °C | read() took 21 ms Pressure: 1007.67 hPa | Chip: 26.35 °C | read() took 20 ms Pressure: 1007.65 hPa | Chip: 26.35 °C | read() took 20 ms Pressure: 1007.67 hPa | Chip: 26.36 °C | read() took 20 ms Pressure: 1007.86 hPa | Chip: 26.37 °C | read() took 20 ms
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

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.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-ms5611-pressure-sensor */ #include <Wire.h> #include <MS5611.h> // MS5611 library by Rob Tillaart // I2C wires: SDA of the MS5611 -> GPIO8, SCL -> GPIO9 (default I2C pins of the ESP32 S3) #define ZERO_SAMPLES 300 // height 0 = the average of 300 readings, this takes about 2 seconds #define SMOOTHING 32 // 1 = off, a bigger number gives a calmer height that follows a move later #define PRINT_MS 100 // print the height 10 times per second #define REPORT_MS 1000 // print the number of readings once per second MS5611 baro(0x77); // baro = barometer, the sensor object. If CSB is on 3.3V, write 0x76 float zero_pressure = 0; // the pressure at height 0, in hPa float height = 0; // the smooth height, in meters unsigned long last_print = 0; // time of the last height line unsigned long last_report = 0; // time of the last report line int readings = 0; // new readings since the last report // height 0 = the average pressure of many readings. Returns false if the sensor does not answer bool set_height_zero() { double sum = 0; for (int i = 0; i < ZERO_SAMPLES; i++) { if (baro.read() != MS5611_READ_OK) return false; sum += baro.getPressure(); } zero_pressure = sum / ZERO_SAMPLES; return true; } void setup() { Serial.begin(115200); // a fast baud rate, so printing does not slow down loop() delay(1000); // time to open the Serial Monitor Wire.begin(); // start I2C if (!baro.begin()) { Serial.println("MS5611 is not ready. Check the four wires"); while (true) delay(1000); // stop here } baro.setOversampling(OSR_STANDARD); // a short wait: one read() takes about 6 ms Serial.println("Do not move the sensor..."); delay(2000); // the chip warms up a little if (!set_height_zero()) { Serial.println("Cannot set height 0. Check the wires"); while (true) delay(1000); // stop here } Serial.println("Height 0 is here. Lift the sensor!"); last_print = last_report = millis(); } void loop() { // 1. Sensor: the only place where loop() waits, about 6 ms if (baro.read() == MS5611_READ_OK) { readings++; float new_height = baro.getAltitude(zero_pressure); // meters above (+) or below (-) height 0 height += (new_height - height) / SMOOTHING; // go only a small part of the way to the new value } // 2. Height: 10 lines per second if (millis() - last_print >= PRINT_MS) { last_print += PRINT_MS; Serial.print("Height: "); Serial.print(height, 2); Serial.println(" m"); } // 3. Report: 1 line per second if (millis() - last_report >= REPORT_MS) { last_report += REPORT_MS; if (readings > 0) { Serial.print("--- readings in this second: "); Serial.println(readings); } else { Serial.println("--- no readings, the height above is old. Check the wires"); } readings = 0; } // 4. Other work goes here: motor control, radio, battery check, ... // This place is reached about 150 times per second, once after each reading. // Keep the work short and do not use delay() here, because the next reading has to wait for it. }

Quick Steps for the Height Test

  1. Use the same circuit and the same library. Upload the sketch, and turn on timestamps in the Serial Monitor (115200 baud).
  2. Leave the module on the desk for about five seconds, until the line "Height 0 is here" appears.
  3. Lift it quickly by 1.5 m and hold it there.
  4. Bring it back to the desk and see how close the value comes to 0.
  5. 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.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
09:41:12.566 -> Do not move the sensor... 09:41:16.516 -> Height 0 is here. Lift the sensor! 09:41:16.619 -> Height: 0.02 m 09:41:16.716 -> Height: 0.00 m 09:41:16.820 -> Height: 0.01 m 09:41:16.918 -> Height: -0.01 m 09:41:17.017 -> Height: 0.02 m 09:41:17.120 -> Height: 0.10 m 09:41:17.218 -> Height: 0.24 m 09:41:17.322 -> Height: 0.44 m 09:41:17.420 -> Height: 0.68 m 09:41:17.517 -> Height: 0.88 m 09:41:17.517 -> --- readings in this second: 154 09:41:17.620 -> Height: 1.11 m 09:41:17.719 -> Height: 1.28 m 09:41:17.816 -> Height: 1.34 m 09:41:17.922 -> Height: 1.37 m 09:41:18.018 -> Height: 1.45 m 09:41:18.124 -> Height: 1.42 m 09:41:18.219 -> Height: 1.43 m 09:41:18.317 -> Height: 1.43 m 09:41:18.421 -> Height: 1.44 m 09:41:18.518 -> Height: 1.47 m 09:41:18.518 -> --- readings in this second: 154
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

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:

  1. 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.
  2. Sunlight: Direct sun on the can disturbs the reading. Shade it.
  3. 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.

  1. 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.
  2. Hoist with a stop height: Switch a small electric winch off when the hook has reached its height.
  3. 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

  1. Beginner: Set SMOOTHING to 1, then to 64. Compare the lines while the module lies still and while you lift it fast.
  2. 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.
  3. 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?
  4. 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.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!