Atmospheric Pressure Sensor with micro:bit
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.
|Parts Used in This Guide|
Step 1 The Module
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.
- Note: 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.
- 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
Let's get started with coding!
- Open up the MakeCode editor
- Then, click on 'New Project ...'
- Click on Advanced
- Click on Extensions
- Type this in the search box: bmp280
- 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!
- From the variables tab, click on 'Make a variable ...'
- Name the variable, 'temp'
- Now drag and drop a 'get temperature' block from BMP280 tab into the editor
- Assign it to the variable, 'temp'.
- Create another variable, but this time name it 'pressure'.
- Again, click on the BMP280 tab but this time, drag and drop a 'get pressure' block into the editor.
- Assign it to the variable, 'pressure'.
- To show the temperature and pressure readings, we use 'show number' blocks.
- Then we assign their values to 'temp' and 'pressure'.
- Finally, add a 'pause (ms) 1000' block at the end of the forever loop.
By adding this, the micro:bit will display a value and pressure reading every 1 second. You will see the readings scroll across the micro:bit's 5x5 LED matrix!
Step 9 Making sense of the readings!
- The temperature reading here will be in Degree Celsius (°C)
- The pressure reading will be in Pascals (Pa).
- So add a 'show string' block from 'Basic' tab, and change its value to 'degree Celsius'.
- Place this under 'show number temp' block.
- Duplicate the 'show string' block by right-clicking it and selecting 'Duplicate'.
- Now change its value to 'Pa', move this block under 'show number pressure'
- 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!
- 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, grab this MakeCode by clicking the Download button below.
- 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, follow these steps:
- 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 now see the temperature and pressure readings scrolling across the 5x5 LED matrix!