1. Required hardware and knowledge:
- Bosch BME280 sensor on breakout board.
- ESP-12E development board
- (Optional) Waterproof enclosure
- (Optional) SX1278 Lora radio module and antenna
You should already know how to compile and upload a program to your ESP-12E - see our tutorial here if you can't do this yet.
2. Understand alternative temperature + humidity + pressure sensors
Aside from the BME280, the DHT11 and DHT22 are frequently cited in hobbyist projects, but we don't like them as much because:
- They're bulkier
- They're not as durable
- The accuracy of both are inferior to the BME280
- The accuracy of the DHT11 is terrible
- They don't measure atmospheric pressure
3. Buying a BME280
Warning: The vendors of the cheapest BME280 breakout boards on sites such as ebay and aliexpress are actually selling Bosch BMP280 sensors instead! The BMP280 sensors are identical to the BME280 in memory structure, but they don't have humidity sensing. You can check to make sure you have the right sensor because the I2C address will be
0x60 (the example code does this check for you).
You can tell the difference because the BME280 is available for around $3-$4, whereas the BMP280 is roughly $1. Finally, the BMP280 board (left) typically looks different to the BME280 (right) - notice that the metal BMP sensor casing is rectangular, whereas the BME sensor is square.
Some BME280 breakout boards don't offer the choice of SPI pins; they only have the 4 pins, and the I2C device address is fixed. If you intend to attach multiple sensors to the same microcontroller (for example, for two adjacent animal enclosures), you need a board with SPI pins available too. The next section explains why.
4. SPI or I2C?
They BME280 is tiny (approx 2mm square) and originally intended for wearable devices, so it is available mounted on a breakout board on ebay or aliexpress. The BME280 supports both I2C and SPI protocols, and they both have pros and cons. We want the flexibility of mounting the sensor far away from the ESP8266 board, so we will use I2C. (SPI is designed for faster communication along shorter wires).
Gotcha: Though the sensor's I2C address is 7 bits (
0x77 by default), only the last bit can be changed. This means that only a maximum of two BME280 slave devices can be connected to the master microcontroller in I2C mode. The
SDO pin must be connected to either
GND, otherwise the sensor's I2C address is undefined. Many other online tutorials overlook this and let that pin float, which can lead to unpredictable difficulty communicating with the sensor. The
SDO pin determines the I2C address;
5. A few notes on the BME280
BME280 has 3 modes: sleep, forced, and normal. Taking measurements consumes power, and so in sleep mode the chip data registers can be read, but no measurements are taken. In forced mode, a single measurement is taken, and then the chip is returned to sleep mode. In normal mode, measurements are taken in a continuous loop with a small configurable delay between measurements (from 0.5 to 1000ms). In normal mode, the signal is filtered using an infinite impulse response (IIR) filter to cancel overpressure effects of disturbances (such as wind blowing onto the sensor or opening windows). See section 3.4.4 of Bosch's datasheet for configuring the filter.
What if you want to change your sensor's parameters according to complicated logic conditions once deployed? If you plan to do a lot of dynamic configuration, and hence you would prefer an abstracted API to do these tasks, you may like to look at Adafruit's library, which has a lot of logic to work with many Arduindo boards and adjust sensor settings. However, from an engineering perspective, we would caution against this. If your application doesn't allow you to know the required settings in advance, you should probably try to redefine the problem or redesign the solution in a more reliable way that doesn't involve reprogramming your sensor chip on the fly.
6. Connecting the BME280
We'll first create a test system with the ESP-12E development board so that we understand how it works. If you're not familiar with the ESP-12E, our introductary tutorial is here. At your option, you can then proceed to more advanced configurations such as multiple sensors, and battery powered long-range wireless systems in outdoor enclosures. Instructions for these configurations are further down the page.
Warning: The first thing we must draw your attention to is this warning from Bosch's datasheet (ch 3.2): It is prohibited to keep any interface pin (SDI, SDO, SCK or CSB) at a logical high level when VDDIO is switched off. Such a configuration can permanently damage the device due an excessive current flow through the ESD protection diodes. This means you must never connect your communications cables without connecting a power source (VCC) first, otherwise you will fry your chip. (As a general rule with electronics; don't power on your entire system until everything is connected.)
Warning: The BME280 is a 3.3V device, and most versions of breakout board don't have voltage regulators, so don't connect it to 5V sources!
Connect your BME280 to your development board as per the following diagram. The BME280 draws very little power, so you can power it directly off the 3V bin on the ESP8266 board. Wire
Install our BME280 library, and compile and flash the basic example program from the
examples directory. When you open your serial monitor (Ctrl + M in the Arduino IDE) you should see a stream of temperature, pressure, and humidity values that update every second.
7. Advanced projects