Arduino MicroPython MS5611 Pressure Sensor

Many mountain huts have a wooden sign beside the door, with the name of the hut and its altitude, for example 1560 m. Somebody painted this number a long time ago. In this project, the hut measures the number itself. An MS5611 pressure sensor feels the air, an Arduino Giga R1 WiFi runs a short MicroPython script, and every two seconds a new line shows the altitude in meters and in feet. No hut near you? The same script works on your desk, too. These are the steps:

Arduino Giga R1 WiFi with MicroPython and MS5611 pressure sensor module, wiring and pinout

Hardware Preparation

1×Arduino Giga R1 WiFi
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
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega/Giga
1×Recommended: Breadboard Shield for Arduino Mega/Giga
1×Recommended: Enclosure for Arduino Giga
1×Recommended: Power Splitter for Arduino Giga

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 Pinout

People who watch the weather call it a barometer, hikers call it an altimeter, and shops often write GY-63 or altitude sensor. It is the same small blue board. The part that measures is the silver can on it, the MS5611-01BA03 from TE Connectivity. A 3.3V regulator, a level shifter and some resistors are on the board as well, so you only need wires.

A membrane of thin silicon is hidden in the can. The pressure of the air bends it, but only a tiny bit, and the chip measures this with 24 bits. The driver turns the result into hPa (hectopascal). 1 hPa is the same as 1 mbar. And why does this tell the altitude? When you walk up a mountain, some of the air stays below you. There is less air above the sensor, and the pressure drops. Near the sea, it drops by about 0.12 hPa for every meter.

GY-63 MS5611 pressure sensor module pinout - VCC, GND, SCL, SDA, CSB, SDO, PS

Of the seven pins in the row, the altimeter needs the first four:

  • VCC pin: gets its power from the 3.3V pin of the Giga. (Alone, the module would also run on 5V.)
  • GND pin: goes to a GND pin, 0V.
  • SCL pin: I2C clock, goes to D8.
  • SDA pin: I2C data, goes to D9.

The other three stay empty:

  • CSB pin: the address switch for I2C. Empty or on GND, the module answers at 0x77. On 3.3V, it answers at 0x76. SPI uses this pin as chip select.
  • SDO pin: an output that only SPI uses.
  • PS pin: the switch between I2C and SPI. The module pulls it HIGH by itself and works with I2C. On GND, it works with SPI.

For CSB and PS, HIGH always means 3.3V. Never put 5V on them, because they are pins of the 3.3V chip.

What Value
Board about 19 x 13 mm, seven pins
Power for the module 3.3V to 5V
Bus I2C with up to 400 kHz, or SPI
I2C address 0x77, changed to 0x76 by CSB
Pressure 10 to 1200 hPa (450 to 1100 hPa with full accuracy)
Accuracy of the pressure ±1.5 hPa at 25°C, between 700 and 1100 hPa
Finest pressure step 0.012 hPa, which is around 10 cm of height
Temperature -40°C to 85°C, accuracy ±0.8°C at 25°C
Converter 24 bit, about 9 ms for a pressure value in the finest mode

The sensor gives a temperature, too. It belongs to the chip and is used to correct the pressure. Because the board makes a little heat, it can read 1°C to 3°C more than a thermometer in the room. If the hut also needs a room thermometer, add the sensor from Arduino MicroPython DHT22 Temperature Humidity Sensor.

The Air Pressure on a Mountain Walk

This is a walk on a "standard day", when the pressure at the sea is 1013.25 hPa:

Stop Altitude Air pressure 1 hPa less means
Beach 0 m (0 ft) 1013.3 hPa 8.3 m up
Valley station 640 m (2100 ft) 938.7 hPa 8.9 m up
Forest inn 1120 m (3675 ft) 885.8 hPa 9.3 m up
Our hut 1560 m (5118 ft) 839.4 hPa 9.7 m up
Pass 1985 m (6512 ft) 796.5 hPa 10.1 m up
Summit 2430 m (7972 ft) 753.5 hPa 10.6 m up

Look at the last column. The air gets thinner, so you have to climb a little more for the same 1 hPa.

Wiring Diagram

Four jumper wires join the two boards. The red wire comes from the power header of the Giga. The other three start on the opposite side of the board, in the header with the pins D8 to D13.

The wiring diagram between Arduino Giga R1 WiFi MicroPython MS5611 pressure sensor

This image is created using Fritzing. Click to enlarge image

MS5611 Module Arduino Giga R1 WiFi
VCC 3.3V
GND GND (next to pin 13)
SCL D8
SDA D9
CSB, SDO, PS no wire

※ NOTE THAT:

  • Use 3.3V for VCC, not 5V. The level shifter lifts SCL and SDA to the voltage of VCC, and the pins of the Giga are made for 3.3V only. 5V is the next hole on the power header, so check the red wire again before you plug in the USB cable.
  • Pull-up resistors for SCL and SDA are already on the module. Do not add any.
  • In the drawing, D8 and D9 also have the names SCL2 and SDA2. In MicroPython, these two pins are I2C(1). The pins named SDA1 and SCL1, or SDA 20 and SCL 21, belong to other I2C buses, and the scripts of this page do not look there.

Choose a place on an inside wall of the hut, far from the stove, the window and the sun. Some open foam in front of the can keeps moving air away. Do not close its two small holes, and keep them dry. Please do not use the module for water or for tires, it is only made for air.

How To Program MS5611 With MicroPython

A driver file with the name DIYables_MicroPython_MS5611.py talks to the chip and does all the calculations. You find its code in the next section. These are the parts you need:

  • Open the I2C bus and create the sensor. No address is needed here, because the driver tries 0x77 and 0x76 by itself:
from machine import I2C from DIYables_MicroPython_MS5611 import MS5611 i2c = I2C(1) sensor = MS5611(i2c)
  • Take a measurement. read() gives back two numbers, °C first and hPa second. It also stores them in sensor.temperature and sensor.pressure:
temperature, pressure = sensor.read()
  • The last reading is also there in other units:
print(sensor.temperature_f(), sensor.pressure_mmhg(), sensor.pressure_inhg())
  • Ask for the altitude. The number in the brackets is the pressure at sea level at this moment, in hPa:
meters = sensor.altitude(1021.4) feet = meters * 3.28084
  • Make the pressure calm. At level 20, the stored pressure goes only 1/20 of the way to a new reading each time:
sensor.set_smoothing(20)

Behind altitude() is this formula:

meters = 44330 * (1 - (pressure / sea_level_pressure) ** 0.190295)

The MS5611 Driver for Arduino MicroPython

Copy this code once to the Giga. It stays there next to your scripts, and both scripts of this page import it. No line of it depends on your hut.

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-ms5611-pressure-sensor """ # Driver of the MS5611 (GY-63) pressure sensor, by DIYables (https://diyables.io/ms5611) # File name on the Arduino Giga: DIYables_MicroPython_MS5611.py from time import sleep_ms _CMD_RESET = 0x1E _CMD_CONVERT_D1 = 0x40 # pressure _CMD_CONVERT_D2 = 0x50 # temperature _CMD_READ_ADC = 0x00 _CMD_READ_PROM = 0xA0 # oversampling: (command offset, conversion time in ms) _OSR = { 256: (0, 2), 512: (2, 3), 1024: (4, 4), 2048: (6, 6), 4096: (8, 11), } def _crc4(prom): # 4-bit CRC of the calibration data (application note AN520) remainder = 0 for i in range(16): word = prom[i >> 1] if i % 2 == 0: remainder ^= word >> 8 elif i < 15: # the low byte of the last word holds the CRC itself remainder ^= word & 0x00FF for _ in range(8): if remainder & 0x8000: remainder = ((remainder << 1) ^ 0x3000) & 0xFFFF else: remainder = (remainder << 1) & 0xFFFF return (remainder >> 12) & 0x0F class MS5611: ADDRESS_77 = 0x77 # CSB not connected or connected to GND ADDRESS_76 = 0x76 # CSB connected to 3.3V STANDARD_SEA_LEVEL = 1013.25 # hPa def __init__(self, i2c, address=None, oversampling=4096, ms5607=False): self._i2c = i2c self._ms5607 = ms5607 self._smoothing = 1 self._reference = None self.temperature = None # degree Celsius self.pressure = None # hPa self.set_oversampling(oversampling) if address is not None: found = self._connect(address) else: found = self._connect(self.ADDRESS_77) if not found: error = self._error found = self._connect(self.ADDRESS_76) if not found and "not found" in self._error: self._error = error if not found: raise RuntimeError(self._error) def set_oversampling(self, value): # 256, 512, 1024, 2048 or 4096: higher = less noise, but slower if value not in _OSR: raise ValueError("oversampling must be 256, 512, 1024, 2048 or 4096") self._offset, self._wait = _OSR[value] def set_smoothing(self, level): # 1 = no smoothing, a bigger level gives a steadier but slower value self._smoothing = max(1, int(level)) def read(self): # take one reading, return (temperature in Celsius, pressure in hPa) d1 = self._convert(_CMD_CONVERT_D1) d2 = self._convert(_CMD_CONVERT_D2) temperature, pressure = self._calculate(d1, d2) if self._smoothing > 1 and self.pressure is not None: pressure = self.pressure + (pressure - self.pressure) / self._smoothing self.temperature = temperature self.pressure = pressure return self.temperature, self.pressure def temperature_f(self): return self.temperature * 1.8 + 32 def pressure_mmhg(self): return self.pressure * 0.7500617 def pressure_inhg(self): return self.pressure * 0.02953 def altitude(self, sea_level=STANDARD_SEA_LEVEL): # meters above sea level; sea_level = the present sea level pressure in hPa return self._altitude(self.pressure, sea_level) def sea_level_pressure(self, altitude): # sea level pressure in hPa, from the last reading and a known altitude in meters return self.pressure / pow(1 - altitude / 44330, 5.255) def zero_height(self, samples=20): # make the present height the zero point (the average of some readings) smoothing, self._smoothing = self._smoothing, 1 total = 0 try: for _ in range(samples): total += self.read()[1] finally: self._smoothing = smoothing self.pressure = self._reference = total / samples def height(self): # meters above (+) or below (-) the zero point if self._reference is None: self.zero_height() return self._altitude(self.pressure, self._reference) @staticmethod def _altitude(pressure, reference): # international barometric formula return 44330 * (1 - pow(pressure / reference, 0.190295)) def _connect(self, address): self.address = address try: self._command(_CMD_RESET) except OSError: self._error = "MS5611 not found, check the wiring" return False sleep_ms(10) # the sensor reloads its calibration data prom = [] for i in range(8): self._command(_CMD_READ_PROM + 2 * i) data = self._i2c.readfrom(address, 2) prom.append((data[0] << 8) | data[1]) # a BMP280 or BMP180 can have the same address, the check code tells them apart valid = _crc4(prom) == (prom[7] & 0x0F) for value in prom[1:7]: if value == 0 or value == 0xFFFF: valid = False if not valid: self._error = "The device at 0x{:02X} is not a working MS5611".format(address) return False self._c = prom return True def _command(self, command): self._i2c.writeto(self.address, bytes([command])) def _convert(self, command): self._command(command + self._offset) sleep_ms(self._wait) self._command(_CMD_READ_ADC) data = self._i2c.readfrom(self.address, 3) value = (data[0] << 16) | (data[1] << 8) | data[2] if value == 0: raise OSError("MS5611 gives no result") return value def _calculate(self, d1, d2): # the integer math of the datasheet, with second order temperature compensation c = self._c dt = d2 - (c[5] << 8) temp = 2000 + ((dt * c[6]) >> 23) if self._ms5607: off = (c[2] << 17) + ((c[4] * dt) >> 6) sens = (c[1] << 16) + ((c[3] * dt) >> 7) else: off = (c[2] << 16) + ((c[4] * dt) >> 7) sens = (c[1] << 15) + ((c[3] * dt) >> 8) if temp < 2000: # below 20 degree Celsius low = (temp - 2000) ** 2 if self._ms5607: off2 = (61 * low) >> 4 sens2 = 2 * low else: off2 = (5 * low) >> 1 sens2 = (5 * low) >> 2 if temp < -1500: # below -15 degree Celsius very_low = (temp + 1500) ** 2 if self._ms5607: off2 += 15 * very_low sens2 += 8 * very_low else: off2 += 7 * very_low sens2 += (11 * very_low) >> 1 temp -= (dt * dt) >> 31 off -= off2 sens -= sens2 p = ((d1 * sens) >> 21) - off return temp / 100, p / 32768 / 100

Arduino MicroPython Code - MS5611 Pressure and Temperature

Start with the small script. It prints the address of the sensor once, and then a line per second: the air in hPa and mmHg, and the chip in °C and °F.

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-ms5611-pressure-sensor """ from machine import I2C from DIYables_MicroPython_MS5611 import MS5611 import time i2c = I2C(1) # Arduino Giga R1 WiFi I2C1 pins: SCL-D8, SDA-D9 # Tries the addresses 0x77 and 0x76, and raises a RuntimeError if no working MS5611 is there sensor = MS5611(i2c) print("MS5611 answers at address 0x{:02X}".format(sensor.address)) while True: try: sensor.read() # Measure now, the results are kept in the sensor object except OSError: print("The MS5611 does not answer. Check the four wires.") else: print("Air {:.2f} hPa = {:.1f} mmHg | Chip {:.1f} °C = {:.1f} °F".format( sensor.pressure, sensor.pressure_mmhg(), sensor.temperature, sensor.temperature_f())) time.sleep(1) # About one line per second

Detailed Instructions

All the work is done in Thonny IDE:

  • If this is your first MicroPython script for the Giga, begin with Getting Started with Arduino and MicroPython. There you install Thonny and the firmware.
  • Connect the four wires while the Giga has no power. Then use the USB Type-C cable to connect it to the computer.
  • Open Tools Options in Thonny. The Interpreter tab must show MicroPython (generic) and the COM port of your Giga (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Paste the driver into an empty editor tab. Save it with Ctrl+S on the MicroPython device, and call it DIYables_MicroPython_MS5611.py. A different name, or one wrong letter, and the import fails.
  • Paste the first script into another empty editor tab. Save it with Ctrl+S on the MicroPython device under the name main.py.
  • Run it with F5 or with the green Run button, and watch the Shell.
  • After three lines, put the module into a small plastic bag with some air in it. Hold the bag closed around the wires with one hand, and press it softly with the other hand.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot MS5611 answers at address 0x77 Air 846.62 hPa = 635.0 mmHg | Chip 19.7 °C = 67.5 °F Air 846.60 hPa = 635.0 mmHg | Chip 19.7 °C = 67.5 °F Air 846.61 hPa = 635.0 mmHg | Chip 19.8 °C = 67.6 °F Air 849.87 hPa = 637.5 mmHg | Chip 19.8 °C = 67.7 °F Air 853.42 hPa = 640.1 mmHg | Chip 19.9 °C = 67.8 °F Air 851.05 hPa = 638.3 mmHg | Chip 20.0 °C = 67.9 °F Air 846.59 hPa = 635.0 mmHg | Chip 19.9 °C = 67.9 °F Air 846.61 hPa = 635.0 mmHg | Chip 19.9 °C = 67.8 °F
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

These lines come from the hut at 1560 m. Near the sea, you see around 1000 hPa. The three higher values are the hand on the bag: less room for the same air means more pressure. When the bag is open again, the old value comes back.

Arduino MicroPython Code - Altitude of the Hut

The second script has only one job, the altitude. The first constant, SEA_LEVEL_PRESSURE, is the one you change. It starts with the standard value 1013.25.

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-ms5611-pressure-sensor """ from machine import I2C from DIYables_MicroPython_MS5611 import MS5611 import time # CHANGE THIS NUMBER: the sea-level pressure (QNH) of your area now, in hPa SEA_LEVEL_PRESSURE = 1013.25 SMOOTHING = 20 # 1 = off, a bigger number = a calmer but slower altitude PRINT_EVERY_MS = 2000 # Time between two lines, in milliseconds FEET_PER_METER = 3.28084 # 1 meter = 3.28084 feet i2c = I2C(1) # Arduino Giga R1 WiFi I2C1 pins: SCL-D8, SDA-D9 sensor = MS5611(i2c) # Ends with a RuntimeError if no MS5611 is found sensor.set_smoothing(SMOOTHING) print("Hut altimeter started, sea-level pressure = {:.2f} hPa".format(SEA_LEVEL_PRESSURE)) last_print = time.ticks_ms() while True: try: sensor.read() # Read all the time: the smoothing needs many readings if time.ticks_diff(time.ticks_ms(), last_print) >= PRINT_EVERY_MS: last_print = time.ticks_ms() meters = sensor.altitude(SEA_LEVEL_PRESSURE) # Height above the sea feet = meters * FEET_PER_METER print("{:.2f} hPa | this hut is at {:.1f} m = {:.0f} ft".format( sensor.pressure, meters, feet)) except OSError: print("The MS5611 does not answer. Check the four wires.") time.sleep(1)

There is no time.sleep(2), because the smoothing works with many readings. The loop reads without a break, more than 80 times between two lines, and only the print() waits for its two seconds. The noise of single readings is gone, and a move to a higher place still shows up in the next line.

Detailed Instructions

  • Look up the sea-level pressure of your area. Weather websites show it, and airport reports call it QNH. A value in inHg becomes hPa when you multiply it by 33.864.
  • Type this number into SEA_LEVEL_PRESSURE. For the lines below, it was 1021.4.
  • Stop the first script. Then store the second one on the Giga under the same name, main.py. The driver is already there.
  • Click Run (F5) and leave the sensor still for some lines. Then carry it to a higher place, such as the floor above.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Hut altimeter started, sea-level pressure = 1021.40 hPa 846.60 hPa | this hut is at 1555.4 m = 5103 ft 846.61 hPa | this hut is at 1555.4 m = 5103 ft 846.60 hPa | this hut is at 1555.5 m = 5103 ft 846.60 hPa | this hut is at 1555.4 m = 5103 ft 846.46 hPa | this hut is at 1556.8 m = 5108 ft 846.34 hPa | this hut is at 1558.0 m = 5111 ft 846.34 hPa | this hut is at 1558.0 m = 5112 ft 846.34 hPa | this hut is at 1558.0 m = 5111 ft
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

The sign at the door says 1560 m, and the sensor says about 1555 m. In the line with 846.46 hPa, the Giga was on the way up the ladder to the beds under the roof, about 2.5 m higher. The smoothed number is calm in both places.

※ NOTE THAT:

Because the file is named main.py, the Giga starts the altimeter at every power-up, without a computer. Remember that the sea-level pressure in the file is then the one of the day you typed it.

How Exact Is the Altitude?

The sensor knows the pressure in the hut, but it cannot know the pressure at the sea. That number changes every day with the weather. Here is the same reading, 846.60 hPa, with four different values in the script:

SEA_LEVEL_PRESSURE Altitude in the Shell Compared with the sign (1560 m)
1005.0 1423.5 m (4670 ft) 136.5 m too low
1013.25 1490.2 m (4889 ft) 69.8 m too low
1021.4 1555.5 m (5103 ft) 4.5 m too low
1030.0 1623.7 m (5327 ft) 63.7 m too high

So 1 hPa more or less in this constant moves the hut by about 8 m. Three more things have an effect:

  • The sensor: ±1.5 hPa means about ±14 m at the height of the hut. This part stays nearly the same from one reading to the next. That is why small height changes are much more exact, about 10 to 20 cm with smoothing.
  • The time: an altitude that was right in the morning can be many meters wrong in the evening. Use a fresh value.
  • The air temperature: the formula uses the air of a standard day. On a very hot or a very cold day, a place as high as our hut can be some tens of meters off.

Teach the Altimeter With the Sign

If you know the real altitude, you can turn the calculation around and let the sensor find the matching sea-level pressure. MicroPython makes this easy, because you can talk to the sensor in the Shell. Interrupt the script with Ctrl+C in the Shell. (The red Stop button is not the same, it clears the memory.) After the lines that end with KeyboardInterrupt, type the round line:

Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 846.60 hPa | this hut is at 1555.4 m = 5103 ft KeyboardInterrupt: >>> round(sensor.sea_level_pressure(1560), 2) 1021.97
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

With 1021.97 in SEA_LEVEL_PRESSURE, the script shows 1560.0 m. This one number removes the error of the sensor, of the weather report and of the air temperature at the same time. Start your walk within the next hour, and the altitudes around the hut are good, too.

When the Number Moves but the Hut Does Not

Leave the altimeter on the wall, do not change the constant, and write the altitude down from time to time. The hut cannot climb. So if its altitude goes up, the air pressure goes down, and this is a weather sign:

Altitude on the wall Air pressure What it often means
Goes up by about 29 m or more in 3 hours falls by 3 hPa or more the weather changes strongly, stay near the hut
Goes up slowly all day falls slowly clouds, wind or rain can come
Goes down slowly, or stays low rises, or stays high fair weather

This is a simple rule, not a forecast. Before a mountain tour, always read the official weather report.

If Something Goes Wrong

Words in quotes are from the red text in the Shell:

  • "no module named 'DIYables_MicroPython_MS5611'": the script runs on the Giga, so the driver must be on the Giga, too. Save it again on the MicroPython device and compare the name with DIYables_MicroPython_MS5611.py.
  • "MS5611 not found, check the wiring": no chip answers on the bus. Maybe SCL and SDA are swapped, or VCC has no 3.3V, or PS has a wire to GND.
  • "The device at 0x77 is not a working MS5611": the wires are fine, but the board is probably a BMP180, BMP280 or BME280. They share their addresses with the MS5611. The driver compares a check code (CRC) in the chip and refuses to show wrong numbers.
  • The pressure is only about half, near 500 hPa at the sea: your module has a MS5607 chip on it. Create the sensor with MS5611(i2c, ms5607=True).
  • The altitude jumps when somebody opens the door: wind changes the pressure in the room for a moment. Move the sensor, add foam, or make SMOOTHING bigger.

More Ideas for the Hut

  • A display beside the door: the OLED 128x64 can share D8 and D9 with the MS5611, because the two modules have different I2C addresses. Print meters and feet in big numbers.
  • A round dial: let the pointer from Arduino MicroPython Stepper Motor A4988 Driver show the pressure or the altitude.
  • Cellar and roof: connect CSB of a second module to 3.3V, and create it with MS5611(i2c, 0x76). The weather moves both values together, so the difference depends only on the height between them. Before you start, lay them side by side and note how much they differ.
  • How high did we climb? Call sensor.zero_height(50) at the door, then print sensor.height() on the way. It starts at 0 m and needs no sea-level pressure.

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!