Arduino Mega - MS5611 Pressure Sensor

A thermometer knows only the weather of this moment. The air pressure often changes before the weather does, so a barometer is a very good first part for a home weather station. This tutorial instructs you how to use the Arduino Mega 2560 with the MS5611 pressure sensor, a small module that shops also sell under the name GY-63. The Mega becomes the base of the station, and the Serial Monitor is its first display. In detail, we will learn:

Arduino Mega 2560 connected to an MS5611 GY-63 pressure sensor module, with module pinout

We suggest:

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×MS5611 Pressure Sensor Module
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

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 .

Overview of MS5611 Pressure Sensor and Its Pinout

MS5611 GY-63 pressure sensor module and its seven pins

Look for the small metal can on the blue board. This is the sensor itself, the MS5611-01BA03 from TE Connectivity. Under its lid there is a thin membrane of silicon, similar to the skin of a tiny drum. The air pushes on this skin, and it bends by a very small amount. A 24-bit converter in the same chip measures this bend, and the Mega calculates the pressure from the result. On a hill, less air is above us, so the push is weaker: close to sea level, every meter up takes away about 0.12 hPa. That is why one sensor can work as a barometer and as an altimeter.

DIYables GY-63 MS5611 Module
Measures air pressure, and the temperature of its own chip
Working range 10 to 1200 hPa (full accuracy between 450 and 1100 hPa)
Accuracy of the pressure ±1.5 hPa at 25°C, in the range 700 to 1100 hPa
Smallest step 0.012 hPa, which is a height of about 10 cm
Temperature -40°C to 85°C, accuracy ±0.8°C at 25°C
Time for one pressure value about 9 ms at the finest setting
Connection I2C (up to 400 kHz) as delivered, SPI is possible
I2C address 0x77, can be changed to 0x76
Power on VCC 3.3V to 5V, the board has its own 3.3V regulator
Size 19 x 13 mm, roughly

Four pins get a wire:

  • VCC pin: must be connected to the 5V pin of the Arduino Mega.
  • GND pin: must be connected to GND (0V).
  • SCL pin: the I2C clock. It must be connected to pin 21 (SCL) of the Arduino Mega.
  • SDA pin: the I2C data. It must be connected to pin 20 (SDA) of the Arduino Mega.

Three pins can be disregarded in this tutorial:

  • CSB pin: makes the choice between the two I2C addresses. Empty, or wired to GND, it gives 0x77 (the module has a pull-down resistor here). Wired to 3.3V, it gives 0x76. SPI users need it as chip select.
  • SDO pin: the data output for SPI. It has no job in I2C.
  • PS pin: makes the choice between I2C and SPI. The module pulls it HIGH for I2C, so we leave it alone. Wired to GND, it would switch to SPI.

※ NOTE THAT:

The pins are not all equal. SCL and SDA go through a level shifter and already have pull-up resistors on the module, so the 5V signals of the Mega are fine and no extra part is needed. CSB and PS go straight to the chip, and the chip runs at 3.3V. A HIGH for these two must come from the 3.3V pin of the Mega, never from 5V.

Wiring Diagram

The module is wired to the I2C pins of the Mega, which are in the COMMUNICATION corner of the board: pin 20 (SDA) and pin 21 (SCL). The drawing takes the GND pin beside pin 13, but every GND pin of the Mega is fine.

The wiring diagram between Arduino Mega MS5611 pressure sensor

This image is created using Fritzing. Click to enlarge image

MS5611 Module Arduino Mega
VCC 5V
GND GND
SCL 21 (SCL)
SDA 20 (SDA)
CSB, SDO, PS not connected

Are pins 20 and 21 hard to reach? The Mega also has SDA and SCL holes next to the AREF pin. These two holes carry the same two signals, so use the pair that you like better. But do not use A4 and A5: on the Mega they are normal analog inputs, not I2C pins.

Indoors or Outdoors?

The air pressure in a house is practically the same as outside, because the air finds its way through small gaps. So this part of the weather station does not need a place in the rain. It can stay next to the Mega on a shelf, with short wires, if you follow these rules:

  • Choose a place in the shade, away from the window and from any fan. Sun and moving air make the value jump. Some open foam over the metal can calms the air.
  • Never cover the two small holes of the can with tape or glue, and never let water get in.
  • Think of it as a sensor for the air around you. It cannot measure water pressure or the pressure in a tire.

How To Program For MS5611 Pressure Sensor

  • The first step is to include two libraries. Wire.h belongs to the Arduino IDE and runs the I2C bus, MS5611.h knows the sensor:
#include <Wire.h> #include <MS5611.h>
  • A sensor object is declared. The number is its I2C address, so change it to 0x76 if your CSB pin is wired to 3.3V:
MS5611 ms5611(0x77);
  • In setup(), start the bus and the sensor, in this order. begin() returns false if nothing answers at the address, or if the calibration data of the chip is empty:
Wire.begin(); if (!ms5611.begin()) { Serial.println("No MS5611 at this I2C address - check the four wires"); while (true) delay(1000); }
  • Give the sensor the time for a fine measurement. The noise is then 0.012 hPa. Without this line, it is 0.065 hPa:
ms5611.setOversampling(OSR_ULTRA_HIGH);
  • The altitude needs one number that the sensor cannot know, the present pressure at sea level in your area. Write it at the top of the sketch:
#define SEA_LEVEL_PRESSURE 1013.25
  • In loop(), take one measurement with read(). All values after that come from this one measurement, so you can ask for as many units as you like. The library has no mmHg, inHg and °F, but one multiplication is enough for each:
if (ms5611.read() == MS5611_READ_OK) { float hpa = ms5611.getPressure(); float pa = ms5611.getPressurePascal(); float mmhg = hpa * 0.750062; float inhg = hpa * 0.02953; float meters = ms5611.getAltitude(SEA_LEVEL_PRESSURE); float feet = ms5611.getAltitudeFeet(SEA_LEVEL_PRESSURE); float celsius = ms5611.getTemperature(); float fahrenheit = celsius * 1.8 + 32; }

Arduino Mega Code for MS5611 Pressure Sensor

Before we build the report, we check the sensor with a short sketch. It prints one line per second: the pressure in hPa and the temperature of the chip.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-ms5611-pressure-sensor */ // Arduino Mega reads the air pressure and the chip temperature from the MS5611 (GY-63) sensor // Wiring: VCC -> 5V, GND -> GND, SCL -> pin 21 (SCL), SDA -> pin 20 (SDA) // Library: "MS5611" by Rob Tillaart #include <Arduino.h> #include <Wire.h> #include <MS5611.h> MS5611 ms5611(0x77); // I2C address: 0x77, or 0x76 if CSB is wired to 3.3V void setup() { Serial.begin(9600); Wire.begin(); // I2C on pins 20 and 21 if (!ms5611.begin()) { Serial.println("No MS5611 at this I2C address - check the four wires"); while (true) delay(1000); // stop here } ms5611.setOversampling(OSR_ULTRA_HIGH); // slowest setting, smallest noise Serial.println("MS5611 is ready"); } void loop() { if (ms5611.read() == MS5611_READ_OK) { // one measurement takes about 20 ms Serial.print(ms5611.getPressure(), 2); // hPa Serial.print(" hPa, chip at "); Serial.print(ms5611.getTemperature(), 2); // degrees Celsius Serial.println(" °C"); } else { Serial.println("The MS5611 gave no answer - check the wires"); } delay(1000); // one line per second }

Detailed Instructions

  • If the Arduino IDE is not on your computer yet, download it from the Arduino website and install it. The Mega is already in its board list.
  • Wire the components as shown in the diagram.
  • Connect the Arduino Mega board to your computer using a USB cable.
  • Open the Arduino IDE, select the Arduino Mega or Mega 2560 board and the correct COM port.
  • Click the Libraries icon on the left side of the Arduino IDE.
  • Type MS5611 in the search box, then find the MS5611 library by Rob Tillaart. Names with something more, such as MS5611_SPI, are different libraries.
  • Click the Install button.
  • Search for MS5611 created by Rob Tillaart and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
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
Arduino Mega Or... on COM15
1
  • Copy the above code and paste it into the Arduino IDE.
  • Click the Upload button to upload the code to the Arduino Mega.
  • Open the Serial Monitor and set the baud rate to 9600.
  • Leave the board and the module on the table for some lines. Then put both on the floor, without pulling on the wires:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
MS5611 is ready 970.04 hPa, chip at 22.64 °C 970.03 hPa, chip at 22.65 °C 970.05 hPa, chip at 22.65 °C 970.04 hPa, chip at 22.66 °C 970.13 hPa, chip at 22.66 °C 970.12 hPa, chip at 22.67 °C 970.14 hPa, chip at 22.67 °C
Ln 11, Col 1
Arduino Mega Or... on COM15
2

The last three lines were printed on the floor, 75 cm lower. A little more air is above the sensor there, and the pressure goes up by about 0.09 hPa. If you see this small step, your sensor works well.

Do not worry if your own number is far from 1013 hPa. Our test house is 432 m above the sea, so 970 hPa is normal there.

Arduino Mega Code for a Weather Station Report

Now the Serial Monitor becomes the display of a real station. Every 5 seconds, the Mega prints a block with the pressure in hPa, Pa, mmHg and inHg, the altitude in meters and feet, and the temperature in °C and °F.

Who Uses Which Pressure Unit?

All four numbers describe the same air. Which one looks familiar depends on where you live and on your job:

Unit Who uses it Standard pressure at sea level 1 hPa is
hPa (the same number as mbar) weather reports in most countries, airports in most countries 1013.25 1
Pa science, the SI unit of pressure 101325 100
mmHg medicine (blood pressure), weather reports in Eastern Europe 760.0 0.750
inHg weather reports and aviation in the USA 29.92 0.0295

It is the same with the height. Pilots in most countries speak of feet, while a hiking map in Europe uses meters.

The Complete Report Sketch

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-ms5611-pressure-sensor */ // Arduino Mega as the first part of a home weather station // Every 5 seconds: one report with the pressure in 4 units, the altitude in m and ft, // and the chip temperature in °C and °F // Wiring: VCC -> 5V, GND -> GND, SCL -> pin 21 (SCL), SDA -> pin 20 (SDA) // Library: "MS5611" by Rob Tillaart #include <Arduino.h> #include <Wire.h> #include <MS5611.h> #define SEA_LEVEL_PRESSURE 1013.25 // hPa - put the present value of your area here (weather website) const unsigned long REPORT_INTERVAL = 5000; // milliseconds between two reports MS5611 ms5611(0x77); // I2C address: 0x77, or 0x76 if CSB is wired to 3.3V unsigned long lastReport = 0; // time of the last report unsigned long reportNumber = 0; // counts the reports void setup() { Serial.begin(9600); Wire.begin(); // I2C on pins 20 and 21 if (!ms5611.begin()) { Serial.println("No MS5611 at this I2C address - check the four wires"); while (true) delay(1000); // stop here } ms5611.setOversampling(OSR_ULTRA_HIGH); // slowest setting, smallest noise Serial.println("Weather station started"); } // prints one block, like the display of a station void printReport() { float hpa = ms5611.getPressure(); float mmhg = hpa * 0.750062; float inhg = hpa * 0.02953; float celsius = ms5611.getTemperature(); float fahrenheit = celsius * 1.8 + 32; reportNumber++; Serial.println(); Serial.print("===== Report "); Serial.print(reportNumber); Serial.println(" ====="); Serial.print("Pressure : "); Serial.print(hpa, 1); Serial.println(" hPa"); Serial.print(" "); Serial.print(ms5611.getPressurePascal(), 0); Serial.println(" Pa"); Serial.print(" "); Serial.print(mmhg, 1); Serial.println(" mmHg"); Serial.print(" "); Serial.print(inhg, 2); Serial.println(" inHg"); Serial.print("Altitude : "); Serial.print(ms5611.getAltitude(SEA_LEVEL_PRESSURE), 1); Serial.print(" m / "); Serial.print(ms5611.getAltitudeFeet(SEA_LEVEL_PRESSURE), 0); Serial.println(" ft"); Serial.print("Temperature : "); Serial.print(celsius, 1); Serial.print(" °C / "); Serial.print(fahrenheit, 1); Serial.println(" °F"); } void loop() { if (millis() - lastReport >= REPORT_INTERVAL) { lastReport = millis(); if (ms5611.read() == MS5611_READ_OK) { printReport(); } else { Serial.println("The MS5611 gave no answer - check the wires"); } } // the code for the next sensor of your station can go here }

The sketch does not wait with a long delay(). It compares millis() with the time of the last report. Between two reports, the Mega is free, and you can add the code of more sensors at the end of loop().

Detailed Instructions

  • Open the website of your weather service, or the report of the nearest airport, and look for the pressure at sea level. Airports call it QNH.
  • Put the number into this line. Our weather website showed 1021.4 hPa:
#define SEA_LEVEL_PRESSURE 1021.4
  • Does your website use inHg? Multiply by 33.864 to get hPa. For mmHg, multiply by 1.3332.
  • Upload the second sketch to the Arduino Mega. The wiring and the library stay the same.
  • Keep the Serial Monitor at 9600 baud. The first report comes after 5 seconds:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
Weather station started ===== Report 1 ===== Pressure : 970.0 hPa 97004 Pa 727.6 mmHg 28.65 inHg Altitude : 432.8 m / 1420 ft Temperature : 22.6 °C / 72.8 °F
Ln 11, Col 1
Arduino Mega Or... on COM15
2

The map says 432 m for this house, and the report says 432.8 m. That is very close. With the default value 1013.25, the same air pressure gives 365.9 m, which is 66 m too low. So the sea-level pressure is worth the short visit to the website: 1 hPa of error moves the altitude by about 8.2 m.

We let the station run. About three hours later, it printed this block:

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
===== Report 2161 ===== Pressure : 966.8 hPa 96681 Pa 725.2 mmHg 28.55 inHg Altitude : 460.7 m / 1511 ft Temperature : 24.1 °C / 75.4 °F
Ln 11, Col 1
Arduino Mega Or... on COM15
2

Of course, the house did not climb 28 m. The weather changed: the pressure fell by 3.2 hPa, but the sketch still used the sea-level value of the morning. Read such a block in this way:

  • The house does not move, so a change in the altitude line is really a change of the weather.
  • Falling pressure often brings clouds, wind or rain. A fall of 3 hPa or more within 3 hours, like here, is a sign of a strong weather change.
  • Pressure that rises steadily, or stays high, often means fair weather.

These rules are only a rule of thumb, not a forecast.

Let the Station Grow

The Arduino Mega has 54 digital pins and 16 analog inputs, and the MS5611 takes only two of them. Good next parts, with the pins that their tutorials use:

  • DHT22 on pin 2: the humidity, and the real temperature of the air.
  • Rain sensor on pin 3 and A0: tells you when the first drops fall.
  • UV sensor on A0: the strength of the sun. If the rain sensor is on A0 already, take A1 and change the pin in the code.
  • LCD 20x4 on pins 20 and 21: a display without the PC. It shares the I2C wires with the MS5611, because its address (0x27 or 0x3F) is a different one.
  • Micro SD card on pins 50 to 53: a file with all the reports.

One more hint for a wind meter that gives pulses: pins 20 and 21 can normally be interrupt pins, but not while I2C uses them. Take pin 18 or 19.

Troubleshooting

What you see Where to look
"No MS5611 at this I2C address" SDA must be on 20 and SCL on 21. Often the two are swapped, or a wire sits on A4 / A5. If CSB is wired to 3.3V, the code needs 0x76
The numbers are nonsense Another chip has the same address, for example a BMP280, BME280 or BMP180. Read the print on your module
The pressure is about 500 hPa Your module carries a MS5607 chip. Add ms5611.reset(1); after the begin() block
The altitude is wrong by 10 m or more SEA_LEVEL_PRESSURE is old. Look up the present value again
The last digits jump around Moving air from a window, a fan or a door. Put open foam on the metal can
The temperature is 1 to 3°C too high This is normal. The board warms the chip a little

Additional Knowledge

Station Pressure and Sea-Level Pressure

Weather stations in a shop often show two pressure values, ABS and REL. ABS (absolute) is the pressure at the place of the sensor. This is what our report shows. REL (relative) is the pressure that you would have at sea level. Weather websites and weather maps show this one, so that towns in the mountains and towns at the sea can be compared.

If you know the height of your home from a map, the library can also calculate the REL value. Add these lines to the printReport() function, with your own height in place of 432.0:

Serial.print("Sea level : "); Serial.print(ms5611.getSeaLevelPressure(hpa, 432.0), 1); Serial.println(" hPa");

For Report 1, this gives 1021.3 hPa, only 0.1 hPa away from the website. For Report 2161 it gives 1017.9 hPa. This line does not need SEA_LEVEL_PRESSURE at all.

How Many Digits Are Useful?

The report prints one digit after the point for hPa and mmHg, two for inHg, and none for Pa. More digits would look exact, but the accuracy of the sensor is ±1.5 hPa. Small steps are still real: if the value moves by 0.1 hPa, the air pressure has really changed, also when the total is off by 1 hPa. This is why a weather station is so good at showing a trend.

MS5611 and DHT22 in One Station

MS5611 DHT22
Air pressure yes no
Altitude yes no
Humidity no yes
Temperature of the chip, used to correct the pressure of the air
Connection to the Mega I2C, pins 20 and 21 one digital pin
Best place in the station indoors, next to the Mega outdoors, in the shade and dry

It is evident that the two sensors do different jobs. The MS5611 is the more precise tool for changes of the weather and of the height, the DHT22 is the right one for warm, cold, humid or dry. A complete weather station has both.

Video Tutorial

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