Environmental
Atmospheric Sensing
The BME280 Atmospheric Sensor provides barometric pressure, humidity and temperature. These and similar barometric sensors are often utilised in drones to sense changes in altitude, and can also be found in GPS tracking devices, and even some smartphones.
A word of warning though: the barometric sensor is only as accurate as the environment in which it is attempting to sense. Indoor spaces with variances in air ventilation may cause the values to shift slightly, and even though the sensor compensates for temperatue and humidity, you will still get some variances in readings when the weather shifts.
We are using Core Electronics’ version of the BME280 sensor for this recipe (follow the link to learn more details)
I2C connection standards?
An increasing number and variety of sensors now use the I2C communication bus. Simply put, I2C sensors provide an easy way to connect it to a microcontroller.
The PiicoDev wiring is similar to a growing class of sensor interconnect standards such as Adafruit’s STEMMA, the identical pin order of Sparkfun’s QWIIC, or the slightly larger SeeedStudio’s Grove. Most of them are pin-compatible, but require specialty connector ends to fit. The healthy competition keeps innovation alive, but it can be slightly annoying finding the right cables to fit.
For PiicoDev sensors, you can connect the cable into either side of the sensor board. However the cable plug itself only goes into the socket one way, so exercise caution when connecting them.
Wiring & Code
Select a microcontroller platform in the tabs below to view the wiring and code meant for the platform:
-
Breadboard diagram
Download Atmospheric Fritzing file
Libraries
Install the following libraries via your Arduino IDE’s Library Manager, if you haven’t already:
- Adafruit BME280 Library
Code
For a refresher on how to use the code in this recipe, click here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/* Atmospheric Sensor We are using the Core Electronics version of the BME280, but the code library is referencing an Adafruit Library. It's a good point to know – that sensors can be assembled by various manufacturers, but so long as the sensor model number is the same, you are likely to get compatibility with code libraries written for the same sensor. This sensor returns temperature, humidity, and most significantly the barometric pressure. Interestingly, an algorithm to convert barometric pressure to altitude allows us to get an approximate altitude of the sensor! Check the Serial port to see how accurate this is – there are a lot of other factors that might sway the barometric pressure. (Read the comments below for more details) */ #include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BME280.h> #define SEALEVELPRESSURE_HPA (1013.25) Adafruit_BME280 bme; // I2C unsigned long delayTime; void setup() { Serial.begin(115200); while (!Serial) ; // wait until the Serial port is ready unsigned status = bme.begin(); if (!status) { Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!"); while (1) delay(10); } delayTime = 1000; } void loop() { printValues(); delay(delayTime); } void printValues() { // here's an example of a custom function that is called from loop(). // this allows our code to become more compartmentalised and (hopefully) easier to read! Serial.print("Temperature: "); Serial.print(bme.readTemperature()); Serial.println("°C"); Serial.print("Pressure: "); Serial.print(bme.readPressure() / 100.0F); Serial.println("hPa"); Serial.print("Approx. Altitude: "); Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); Serial.println("m"); Serial.print("Humidity: "); Serial.print(bme.readHumidity()); Serial.println("%"); Serial.println(); } /* Please note that the code provided here is licensed under the MIT license. The MIT License (MIT) Copyright © 2024 Chuan Khoo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-
COMING SOON…
More code samples for this component for other platforms will be added here over time.