Atmospheric Pressure Sensor with micro:bit
Get readings from an atmospheric pressure sensor and the micro:bit
Written By: Cherie Tan


Difficulty
Easy

Steps
11
Introduction
A barometric pressure sensor can measure the air pressure and temperature around you.
In this guide, we will connect a BMP280 barometric pressure sensor to the micro:bit, and program it using MakeCode.
Complete this guide to start getting pressure and altitude readings with the BMP280 and micro:bit.
Step 1 The Module

-
Voltage is the difference in potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to.
-
Before we begin, let's take a closer look at the atmospheric pressure sensor module. It has 6 pins, but we will only be using 4 of them today:
3.3V : 'VCC' stands for Voltage Common Collector. We'll connect the VCC pin to 3.3V on the micro:bit
GND: In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called ground or GND.
Serial Clock (SCL)
Serial Data (SDA)
Step 2 Connect module to breadboard
Step 3 Connect 3.3V to VCC
Step 4 Connect GND to GND
Step 5 Connect P19 to SCL
Step 6 Connect P20 to SDA
Step 7 Add the BMP280 package

-
Click on Extensions
-
Type this in the search box: bmp280
-
Let's get started with coding! Open up the MakeCode editor.
-
Then, click on 'New Project ...'
-
Click on Advanced
-
Click on the magnifying glass icon to search
-
Click on BMP280 package and it will be automatically added to the MakeCode editor
Step 8 The MakeCode
let pressure = 0 let temp = 0 basic.forever(function () { temp = BMP280.temperature() pressure = BMP280.pressure() basic.showNumber(temp) basic.showNumber(pressure) basic.pause(1000) })
-
Copy and paste this code into the Javascript interface
Step 9 Making sense of the readings
let pressure = 0 let temp = 0 basic.forever(function () { temp = BMP280.temperature() pressure = BMP280.pressure() basic.showNumber(temp) basic.showString("degree Celsius") basic.showNumber(pressure) basic.showString("Pa") basic.pause(1000) })
-
Replace the existing code in the Javascript interface with the following.
-
The temperature reading here will be in Degree Celsius (°C)
-
The pressure reading will be in Pascals (Pa).
-
When you upload this code, there will be two readings on the micro:bit's LED screen, first the temperature in degree Celsius, and the second will be the atmospheric pressure in Pascals.
Step 10 Pressure and Altitude
let t = 0 let prev_p = 0 let p = 0 let damping = 0 damping = 15 prev_p = Math.round(BMP280.pressure() / damping) basic.forever(function () { p = Math.round(BMP280.pressure() / damping) t = BMP280.temperature() if (p == prev_p) { basic.showArrow(ArrowNames.East) } else { if (p < prev_p) { basic.showArrow(ArrowNames.North) } else { basic.showArrow(ArrowNames.South) } } prev_p = p basic.pause(500) })
-
We've talked about air pressure and temperature. Now let's look at the relationship between air pressure and height or altitude.
-
Air pressure decreases with increasing altitude or height. To see this for yourself, first, paste this code into the Javascript interface.
-
Air pressure is also how drones detect their height and stay stable in the air!
-
See how air pressure changes as you move the Micro:bit up and down.
When it is not raised, an arrow pointing east will be shown
Raise it about a meter high up and the arrow will be pointing north
Bring it back down and the arrow will be pointing south
Step 11 Upload the code

-
To upload the MakeCode to the micro:bit, first connect the micro:bit to your computer using a microUSB cable
-
Over in MakeCode, click on the Download button on the bottom left-hand corner
-
Find the hex file in the Downloads folder or wherever you might have moved it to
-
Open up Finder on the MacOS or Explorer on Windows, and drag the hex file into MICROBIT under 'Devices' on the macOS.
-
While the code is uploading, the micro:bit will flash for a few seconds. You should then see the temperature and pressure readings scrolling across the 5x5 LED matrix!