ESP32 UNO - MS5611 Pressure Sensor

Rain is bad news for a day at the lake, and also for fresh paint on the garden fence. The air often gives a hint some hours before: its pressure starts to go down. With the ESP32 Uno form board and a DIYables MS5611 module, also known as GY-63, you can watch for this hint at home, in the garden or at the water. Four wires and one library are enough. The finished sketch remembers the last three hours and ends every line with one word: Rising, Steady or Falling.

Step by step to your barometer:

  1. See what the seven pins of the MS5611 do, and why only four of them get a wire
  2. Connect the module to the SDA and SCL header holes and to 3.3V
  3. Read the air pressure in hPa and the temperature of the chip
  4. Store one averaged value every 10 minutes and compare it with the oldest one
  5. Test the trend in a few minutes, without waiting for rain
The wiring diagram between ESP32 Uno board, MS5611 GY-63 pressure sensor module with pinout, and I2C

Components You Need

1×ESP32 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

Or you can buy the following kits:

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 .

The MS5611 Module

Read this part once, and you know what the number on the screen is made of.

The blue board is about 19 x 13 mm. Shops and hobby pages call it GY-63, barometric pressure sensor, barometer, altimeter or altitude sensor. The important part is the silver can with two tiny holes: the MS5611-01BA03 chip from TE Connectivity.

Think of the can as a tiny drum. Its skin is a silicon membrane, and the weight of the air pushes this skin in, only a very little. The chip measures the push with a 24-bit converter. Carry the drum up a hill, and part of the air is now below you, so the push gets weaker. Near sea level you lose about 0.12 hPa per metre, which is 1 hPa for roughly 8.3 m. The weather changes the push as well, even when the module never moves. That second effect is the one for this page.

The result comes in hPa (hectopascal), the unit of the weather maps. Some maps write mbar, and the number is the same. 1013.25 hPa counts as the standard value at sea level.

MS5611 GY-63 module photo and top view with the pins VCC, GND, SCL, SDA, CSB, SDO and PS
Pin What It Is For Wire on This Page
VCC Power input, 3.3V up to 5V 3.3V
GND Ground, 0V GND
SCL I2C clock SCL
SDA I2C data SDA
CSB I2C address, no wire or GND = 0x77, 3.3V = 0x76 (for SPI it is the chip select) none
SDO Data out in SPI mode none
PS Bus selection, HIGH = I2C (a small resistor on the board takes care of that), GND = SPI none

Be careful with CSB and PS: they lead straight to the chip, and the chip runs at 3.3V. HIGH for these two pins means 3.3V, never 5V.

Feature Value
Supply voltage 3.3V to 5V, with a 3.3V regulator of its own
Bus I2C with up to 400 kHz, SPI is possible too
Address on I2C 0x77, or 0x76 with CSB on 3.3V
What it can measure 10 hPa to 1200 hPa, with full accuracy between 450 and 1100 hPa
Resolution 0.012 hPa, the same as about 10 cm up or down
Accuracy of the pressure ±1.5 hPa between 700 and 1100 hPa, at 25°C
Accuracy of the temperature ±0.8°C at 25°C
Works in -40°C to 85°C
Converter 24 bit, one pressure conversion takes about 9 ms at the best resolution

Good to know: an accuracy of ±1.5 hPa looks poor for a trend of 1 hPa. It is not a problem. The error of your own module stays almost the same from hour to hour, and the sketch subtracts two values of the same module. So the error nearly disappears.

ESP32 Uno Pinout

Two holes are important today, and the board prints their names: SDA and SCL. They are the last two holes of the digital header, after GND and RST. Behind them are GPIO21 (SDA) and GPIO22 (SCL).

ESP32 Uno pinout diagram

How to Connect

Four wires and no resistor: SCL and SDA need pull-up resistors, and the module brings its own. CSB, SDO and PS stay empty.

The wiring diagram between ESP32 Uno MS5611 pressure sensor

This image is created using Fritzing. Click to enlarge image

MS5611 Pin ESP32 Uno Pin
VCC 3.3V
GND GND
SCL SCL (GPIO22)
SDA SDA (GPIO21)

WARNING

On this board VCC gets 3.3V, never 5V. The module pulls SCL and SDA up to the voltage of VCC. With 5V there, GPIO21 and GPIO22 would get 5V too, and the pins of the ESP32 work at 3.3V: they are not 5V tolerant.

A barometer runs day and night, maybe in a shed or a boat house where nobody checks the wires. Screws are a safer choice than loose jumper ends. You can use a Screw Shield V2 for that, or also a Proto Screw Terminal Block Shield. Both sit on top of the ESP32 Uno, and the four pins stay the same:

The wiring diagram between ESP32 Uno MS5611 pressure sensor Screw Shield

This image is created using Fritzing. Click to enlarge image

Only two of the four wires sit under a screw here: 3.3V and GND. SDA and SCL have no screw terminal on the Screw Shield V2. The diagram shows where they go: into the SDA and SCL holes of the shield's own header.

Good to know: do not move SDA and SCL to A4 and A5. On this board A4 is GPIO36 and A5 is GPIO39. Both are input-only analog holes and carry no I2C.

A Good Place for the Sensor

  • Shade and calm air: sunlight and gusts of wind make the value jump. Cover the metal can with a bit of open foam.
  • Dry, but not airtight: a box with a small opening is fine. The two holes of the can must stay free and dry.
  • Always the same height: a move of 8.3 m up or down looks like 1 hPa of weather.

ESP32 Uno Code - Air Pressure and Chip Temperature

The first sketch shows that the wiring works. It prints one line every two seconds. Before that, it prints the I2C address of the sensor, or a message when no sensor answers.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-ms5611-pressure-sensor */ /* * ESP32 Uno sketch: first readings from a DIYables GY-63 MS5611 pressure sensor (I2C). * VCC -> 3.3V, GND -> GND, SCL -> SCL (GPIO22), SDA -> SDA (GPIO21) * One line every 2 seconds: air pressure in hPa, chip temperature in °C and °F. * Library: MS5611 by Rob Tillaart */ #include <Wire.h> #include <MS5611.h> #define MS5611_ADDRESS 0x77 // CSB without a wire = 0x77, CSB on 3.3V = 0x76 #define PRINT_INTERVAL 2000 // ms between two lines MS5611 barometer(MS5611_ADDRESS); void setup() { Serial.begin(9600); Wire.begin(); // I2C bus on GPIO21 (SDA) and GPIO22 (SCL) if (!barometer.begin()) { Serial.println("No MS5611 on the I2C bus: check VCC, GND, SCL and SDA"); while (true) delay(1000); // stop here, fix the wiring, then press reset } barometer.setOversampling(OSR_ULTRA_HIGH); // the smallest noise Serial.print("Barometer ready, I2C address 0x"); Serial.println(barometer.getAddress(), HEX); } void loop() { if (barometer.read() == MS5611_READ_OK) { // one fresh reading, takes about 20 ms float celsius = barometer.getTemperature(); Serial.print("Air pressure "); Serial.print(barometer.getPressure(), 2); // hPa Serial.print(" hPa | Chip "); Serial.print(celsius, 1); // °C Serial.print(" °C / "); Serial.print(celsius * 1.8 + 32, 1); // °F Serial.println(" °F"); } else { Serial.println("The sensor does not answer, check the wires"); } delay(PRINT_INTERVAL); }

Upload and Test

  1. No ESP32 core in your Arduino IDE yet? Add it with ESP32 Uno - Getting Started.
  2. Connect the four wires and look at VCC once more: it must be on 3.3V.
  3. Join the board and the computer with the USB Type-C cable. The board in the IDE is ESP32 Dev Module, the port is the one of the CH340.
  4. Find the library: click the Libraries icon, then type "MS5611" into the search box.
  5. Install the result called MS5611, by Rob Tillaart, with the Install button. Some neighbours in the list have a longer name, so read twice. One library is all you need here.
  • 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
  1. Put the sketch into the IDE and press Upload.
  2. Look at the Serial Monitor (9600 baud) and tap the reset (EN) button. Some strange signs before the first line are normal on an ESP32:
∞
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
Barometer ready, I2C address 0x77 Air pressure 1017.46 hPa | Chip 24.3 °C / 75.8 °F Air pressure 1017.47 hPa | Chip 24.3 °C / 75.8 °F Air pressure 1017.45 hPa | Chip 24.4 °C / 75.8 °F Air pressure 1017.46 hPa | Chip 24.4 °C / 75.9 °F
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Pro tip: hold the module near the floor, then high above your head. With 2 m between the two places, the pressure up there is about 0.24 hPa lower. You can see it in the digits after the point.

Why is there a temperature at all? The library uses it to correct the pressure. It belongs to the chip itself, and the parts around it add some heat, so 1 to 3°C above the room is normal. For the air in the garden or the greenhouse, take a DHT22 or a DS18B20.

ESP32 Uno Code - Is the Weather Going Up or Down?

One pressure value says little. The change over some hours says much more. This sketch takes 50 readings in about 5 seconds, stores their average, and does it again every 10 minutes. The array has room for 19 values. They have 18 gaps of 10 minutes between them, and that makes 3 hours. The newest value minus the oldest one is the change, and two limits turn it into a word:

Word Change against the Oldest Value What It Often Means
Rising +1.0 hPa or more fair weather is likely
Steady between the two limits no clear sign of a change
Falling -1.0 hPa or lower clouds, wind or rain are likely

Watch out for -3.0 hPa or lower. Such a fast fall within 3 hours points to a strong change of the weather, so it is better to pack up the rods and close the greenhouse. All this is a rule of thumb, so read the official forecast as well. And it is about the weather only: the sketch knows nothing about the fish.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-ms5611-pressure-sensor */ /* * ESP32 Uno sketch: is the weather going up or down? * Every 10 minutes the sketch stores one averaged air pressure value. A small array holds * the last 3 hours. The newest value is compared with the oldest one: * Rising, Steady or Falling. * Library: MS5611 by Rob Tillaart */ #include <Wire.h> #include <MS5611.h> #define MS5611_ADDRESS 0x77 // CSB without a wire = 0x77, CSB on 3.3V = 0x76 #define SAMPLE_INTERVAL 600000UL // ms between two stored values: 10 minutes #define HISTORY_SIZE 19 // 19 values have 18 steps between them, 18 x 10 minutes = 3 hours #define READINGS 50 // readings averaged for one stored value #define READING_GAP 80 // ms between two of them, so the average covers about 5 seconds #define RISE_LIMIT 1.0 // hPa, the pressure went up at least this much = Rising #define FALL_LIMIT -1.0 // hPa, the pressure went down at least this much = Falling MS5611 barometer(MS5611_ADDRESS); float history[HISTORY_SIZE]; // history[0] is always the oldest value int stored = 0; // how many values the array holds now unsigned long last_sample = 0; // when the last value was taken // average of several readings, so a short puff of wind counts less bool readAverage(float &average) { float sum = 0; for (int i = 0; i < READINGS; i++) { if (barometer.read() != MS5611_READ_OK) return false; // the sensor did not answer sum += barometer.getPressure(); delay(READING_GAP); } average = sum / READINGS; return true; } // put the new value at the end, drop the oldest one when the array is full void storeValue(float pressure) { if (stored == HISTORY_SIZE) { for (int i = 1; i < HISTORY_SIZE; i++) history[i - 1] = history[i]; stored--; } history[stored] = pressure; stored++; } const char *trendWord(float change) { if (change >= RISE_LIMIT) return "Rising"; if (change <= FALL_LIMIT) return "Falling"; return "Steady"; } void setup() { Serial.begin(9600); Wire.begin(); // I2C bus on GPIO21 (SDA) and GPIO22 (SCL) if (!barometer.begin()) { Serial.println("No MS5611 on the I2C bus: check VCC, GND, SCL and SDA"); while (true) delay(1000); // stop here, fix the wiring, then press reset } barometer.setOversampling(OSR_ULTRA_HIGH); // the smallest noise Serial.println("Pressure trend, 3 hours back"); } void loop() { // the first value comes at once, every other value waits for its time if (stored > 0 && millis() - last_sample < SAMPLE_INTERVAL) return; unsigned long now = millis(); float pressure; if (!readAverage(pressure)) { Serial.println("The sensor does not answer, next try in 5 seconds"); delay(5000); return; } last_sample = now; storeValue(pressure); float change = pressure - history[0]; // newest minus oldest float minutes = (stored - 1) * (SAMPLE_INTERVAL / 60000.0); // time between the two Serial.print(pressure, 2); Serial.print(" hPa | "); if (change > 0) Serial.print("+"); Serial.print(change, 2); Serial.print(" hPa in "); Serial.print(minutes, 1); Serial.print(" min | "); Serial.println(trendWord(change)); }

Upload and Test

  1. Keep the circuit and the library. Nothing changes there.
  2. Upload the second sketch and press the reset (EN) button. The first value comes after about 5 seconds, then one line every 10 minutes:
∞
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
Pressure trend, 3 hours back 1016.84 hPa | 0.00 hPa in 0.0 min | Steady 1016.81 hPa | -0.03 hPa in 10.0 min | Steady 1016.75 hPa | -0.09 hPa in 20.0 min | Steady 1016.70 hPa | -0.14 hPa in 30.0 min | Steady
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Leave it alone for the afternoon. On this day the pressure goes down, slowly at first. The word changes after a bit more than two hours:
∞
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
1015.99 hPa | -0.85 hPa in 110.0 min | Steady 1015.85 hPa | -0.99 hPa in 120.0 min | Steady 1015.72 hPa | -1.12 hPa in 130.0 min | Falling 1015.60 hPa | -1.24 hPa in 140.0 min | Falling
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Look at the time in each line. From the 19th value on, it stays at 180.0 min. Each new value then pushes out the oldest one:
∞
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
1015.12 hPa | -1.72 hPa in 170.0 min | Falling 1014.95 hPa | -1.89 hPa in 180.0 min | Falling 1014.77 hPa | -2.04 hPa in 180.0 min | Falling 1014.56 hPa | -2.19 hPa in 180.0 min | Falling
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Pro tip: in the first three hours the sketch looks back over a shorter time. Do not trust the word Steady too early.

Quick Test without Waiting for Rain

Make the time short and the limits small. Change three lines at the top of the sketch:

#define SAMPLE_INTERVAL 20000UL // 20 seconds, now the array covers 6 minutes #define RISE_LIMIT 0.1 #define FALL_LIMIT -0.1

Leave the module on the floor for three lines. Then put it on a shelf about 2 m higher:

∞
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
Pressure trend, 3 hours back 1015.42 hPa | 0.00 hPa in 0.0 min | Steady 1015.43 hPa | +0.01 hPa in 0.3 min | Steady 1015.41 hPa | -0.01 hPa in 0.7 min | Steady 1015.19 hPa | -0.23 hPa in 1.0 min | Falling 1015.18 hPa | -0.24 hPa in 1.3 min | Falling
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Wait six minutes on the shelf, and the word goes back to Steady, because all the floor values have left the array. Back on the floor, you get Rising. When the test is done, set the three lines to 600000UL, 1.0 and -1.0 again.

Video Tutorial

Code Explanation

Code Part What It Does
Wire.begin() Starts the I2C bus on GPIO21 and GPIO22, the sensor cannot answer before this line
barometer.begin() Looks for the sensor at MS5611_ADDRESS and loads its factory calibration, gives false when one of the two fails
setOversampling(OSR_ULTRA_HIGH) Lowers the noise from 0.065 hPa (the setting after the start) to 0.012 hPa
barometer.read() Takes one reading in about 20 ms, gives MS5611_READ_OK when all went well
getPressure() Pressure of the last reading in hPa
getTemperature() Chip temperature in °C, for °F the sketch takes it x 1.8 + 32
readAverage() Averages 50 readings, with 80 ms between them
storeValue() Puts the new value at the end, and moves all values one place forward when the array is full
history[0] The oldest value, at most 3 hours old
millis() - last_sample Time since the last value, loop() ends at once while it is too early
trendWord() Compares the change with the two limits

Wire.h is part of the ESP32 core, so there is nothing to install for it.

Compare with the Weather Report

The report says 1016 hPa, but the barometer in your hill garden says 978 hPa. Both can be right. Reports show the pressure at sea level (QNH), so that all towns can be compared. If you know the height of your garden from a map, the library does the same math with the last reading and this height:

Serial.println(barometer.getSeaLevelPressure(barometer.getPressure(), 320), 2); // 320 = height of the garden in metres

With 978.30 hPa at 320 m it prints 1016.29. It also works the other way: barometer.getAltitude(1016.29), with today's sea-level pressure between the brackets, returns the metres above sea level. Take a fresh value from a weather website or an airport report, because 1 hPa wrong means about 8.3 m wrong.

ESP32 Uno vs UNO R4 for This Project

The two boards have the same shape, but the module is wired in a different way.

Item Arduino UNO R4 ESP32 Uno
VCC of the module 5V 3.3V only
I2C data A4 or the SDA hole SDA hole only (GPIO21)
I2C clock A5 or the SCL hole SCL hole only (GPIO22)
Arduino code works as it is works as it is

Try These Next

  • Screen in the shed: show the pressure and the word on an OLED 128x64. It shares the SDA and SCL holes with the MS5611 (0x3C and 0x77) and also runs from 3.3V.
  • Curve on the phone: send the value to the Bluetooth Plotter. You see a bend in the curve before the word changes.
  • Storm line: print one extra line when the change is -3.0 hPa or lower.
  • Longer memory: set HISTORY_SIZE to 73, and the array looks back 12 hours.

Troubleshooting

Problem Possible Cause Solution
"No MS5611 on the I2C bus" A loose wire, SDA and SCL are on A4 and A5, or CSB is on 3.3V Use the SDA and SCL holes, check 3.3V and GND, then press reset. With CSB on 3.3V, set MS5611_ADDRESS to 0x76
Numbers that no weather can make There is a BMP280, BME280 or BMP180 on the bus, with the address of the MS5611 The sketch cannot see the difference. Read the print on the module and take the wrong one off
Only half of the real pressure, about 500 hPa The chip on the module is a MS5607 Put barometer.reset(1); into setup(), after barometer.begin()
Values jump by some tenths of a hPa Wind or a slamming door Find a calmer place, use the foam
Rising or Falling right after a walk You changed the height, not the weather Press reset at the new place and start with fresh values
No COM port CH340 driver missing, or a charge-only cable Install the driver and use a USB cable with data wires

FAQ

Does the trend need my altitude?

No. The module stays in one place, so its height is the same in the newest and in the oldest value. Only the weather is left in the difference. At the lake, set it up first and press reset there: a lake 20 m below the car park adds about 2.4 hPa.

What happens after a reset or an empty battery?

The array is in RAM, so it starts empty. You get a full 3 hour trend again after 3 hours.

Is 1.0 hPa the right limit everywhere?

It is a good start. Near the equator the pressure goes up and down twice a day by itself, and 1 to 2 hPa in 3 hours is normal there on a dry day. Try 2.0 and -2.0 in such a place.

Can the MS5611 measure the depth of the lake?

No. It is made for air, not for water or tyre pressure. Keep the can dry.

Can I connect two MS5611 modules?

Yes. Connect CSB of the second module to 3.3V. It then answers at 0x76, while the first one stays at 0x77, and both share SDA and SCL. In the sketch, give the second one its own object: MS5611 barometer2(0x76);.

※ 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!