Sample Code for MakCode - Pressure
Last revision 2026/01/26
The article provides insights into measuring atmospheric pressure using sample code and explores the relationship between pressure and altitude, along with methods such as mercury barometers, metal membrane barometers, and pressure sensors.
Principle
Air pressure is the atmospheric pressure acting on the unit area, i.e. the gravity of the vertical air column that is numerically equal to the vertical air column that extends upward in the upper boundary of the atmosphere. The famous Magdeburg hemispheric experiment proved its existence.
The international unit of air pressure is Pascal, or Pa, and the symbol is Pa. In meteorology, people usually use kPa as a unit.
Atmospheric Pressure V.S. Altitude
The air pressure is related to altitude, atmospheric temperature, atmospheric density, etc. Generally, the atmospheric pressure will decrease as the altitude increases. And, during the year, winter is stronger than summer atmospheric pressure. E.g.
- Shanghai's altitude is about 0 meters, the atmospheric pressure is about 105kpa;
- Chengdu's altitude is about 520 meters, the atmospheric pressure is about 95kpa;
- The altitude of Mount Qomolangma is about 8848 meters, and the atmospheric pressure is about 30kpa.
From the data above, we can see that as the altitude increases, the greater the atmospheric pressure is lower.
There is a formula that can roughly calculate the altitude, which can be done to understand.
The relationship between atmospheric pressure and altitude:
- P=P0×(1-H/44300) ^5.256
The calculation height formula is:
- H=44300(1- (P/P0) ^ (1/5.256))*
P.s. H- Altitude, P0-Atmospheric (0°C, 101.325kPa)



How to measure Atmospheric Pressure
In the previous section, we learned the basics of atmospheric pressure and the relationship with altitude, so how do we measure atmospheric pressure?
There are three common ways to measure, mercury barometer, metal membrane barometer, and pressure sensor.

Mercury Barometer: the first mercury barometer was invented by Moreland in 1670. Then a German A. Sprung modified the original mercury barometer. The mercury barometer is a device to measure atmospheric pressure, the principle is based on the Torricelli experiment. Taking advantage of the principle that the mercury weight inverted in the glass tube column inside the mercury tank balances the surrounding atmospheric pressure, the height of the mercury column indicates atmospheric pressure. A standard atmospheric pressure (atm, standard atmosphere) is equal to the height of a 76 cm mercury column.
Metal Membrane Barometer: taking advantage of the difference between a standard atmospheric pressure filled internally and the air pressure of the outside world, which changes the metal form and reflected on the pointer.
Pressure Sensor: the air pressure sensor is an internal thin film that is sensitive to air pressure strength. When the pressure of the gas under test decreases or increases, the deformation of the film causes the internal resistance value to change, which causes the voltage to change. After an A/D conversion, it becomes a computer-readable air pressure data. The air pressure sensor is not only high-precision, but also small in size and easy to use. It is widely used in outdoor equipment, such as mountaineering watches, signal base stations, drones, etc.
Preparation
The sensor in the center of the cloud and sun can measure the atmospheric pressure.

For more information, refer to the Getting Started guide.
Sample Code
Now let's re-modify the program to test the local atmospheric pressure.
Please write the following code and copy it to micro: bit.
This program will automatically detect the local atmospheric pressure and display it on the display.

Result
Through the learning above, we have learned the following:
- The basic concept of atmospheric pressure strength.
- The relationship between atmospheric pressure and altitude.
- Atmospheric pressure measurement method
- The way to program to measure the atmospheric pressure.
Was this article helpful?
