(1) Logging barometric pressure with an I2C device and an Arduino SD shield
by Floris Wouterlood – Leiden – The Netherlands – June 16, 2017
In a previous project1 I discussed the construction of a weather station in an Arduino environment. That station employs a BMP280 sensor which measures ambient barometric pressure and air temperature. One aspect of the ingenuity of the BMP 280 is its I2C communication compliance. I2C is a ‘bus’ type of technology that allows multiple sensors and devices running on (the same) two wires: a data line and a clock line. I2C is implemented in Arduino microcontrollers. As I2C communication uses only pins A4 and A5 (next to power and GND necessary to provide energy to the devices), virtually all pins of an Arduino remain available to connect with other devices. This makes I2C devices irresistible for experimentation and application.
In the current project a BMP280 is wired according to the I2C standard to an Arduino Uno fitted with a logging shield that features a real time clock (RTC) and a SD card slot. The barometric pressure and air temperatures are displayed on a 20×4 LCD display that also runs on the I2C bus. Every five minutes these values are sampled and written to a .csv file on a SD card. The .csv file is imported in a spreadsheet to produce numerical and graphic representation of the measured values.
Barometric pressure is a good indicator for weather forecasting. In my country (The Netherlands), low pressure systems usually bring moist and wind while high pressure systems stand for sunny, warm weather in the summer or sunny and freezing conditions in the winter. Fast dropping mercury in a traditional barometer is a warning to take an umbrella with you and wear a good coat when you leave your home. We learnt at school that it is the rate of barometric pressure change that matters, not so much the amount of actual millibar at a certain time point. This feature of barometric pressure makes it so interesting for logging. Checking graphical representations of actual logging data, say the last few hours or so, or looking at logged data of the last 6 to 24 hours give insight in what the weather is going to do. Similar to the UK, everybody in my country always has the weather in mind, so let’s do some logging.
Sensor for temperature and barometric pressure: the BMP280
An accurate, affordable, modern digital sensor that very accurately measures ambient air pressure and temperature is the BMP280, manufactured by Bosch. This sensor is compliant with the I2C and SPI serial communication protocols supported by the Arduino family of microcontroller boards. Air pressure measurement of the BMP280 is reportedly so good and fast (~1 hPa; sampling rate max 182 readings per second) that this sensor can be implemented in altimeters, e.g. in drones or robots. Although the BMP 280 continuously measures temperature with an accuracy in the order of magnitude of one degree Celsius this feature is ignored in the present project, simply because this is an indoor project in a well insulated home with little variation in room temperature. For outdoor temperature measurements I have a Dallas DS18B20 sensor connected to another Arduino2. The BMP280 is available as a breakout device for the Arduino platform and is being marketed by various companies.
Real time clock (RTC) and logging to file
Next to the microcontroller platform in the heart of the construction, a SD card read/write slot and a display, an extra device is needed to supply real time because by itself an Arduino does not track the exact world time. The microcontroller platform has a timer though; every time an Arduino is powered on the variable millis() keeps track of the time elapsed (in milliseconds, hence the name of this function) after the device has come to life. An external real time clock (RTC) is sufficient to have the exact real world time at hand. Finally we need to record the samples over a daytime cycle, or longer. Among the several solutions to accomplish this I opted in the current project for writing data to a file on a SD card. On the market are Arduino Uno compatible shields that feature both a RTC and a SD card slot. Just stick the shield on top of the Uno, connect the wires, insert the SD card and that’s it.
Wiring: see figure 2.
Figure 2: Setup of the logging project. Pins SDA and SCL of the BMP280 are connected to pins A4 and A5, respectively on the shield. Also the pins SDA and SCL of the LCD display connect to pins A4 and A5 on the shield. The leds are control leds (see sketch).
1x Uno microcontroller board, 1x Uno SD shield with real time clock (RTC), prototyping breadboard, 1x 220Ω resistor, 1x green led, 1x yellow led, wires, I2C 20×4 LCD display, SD card (FAT32 formatted). Please note that the BMP280 is a 3.3V device while the LCD runs on 5V DC power.
The sketch: activity breakdown
Six libraries are necessary to compile the sketch: SPI.h, SD.h, Wire.h, RTClib.h, BMP280.h and LiquidCrystal_I2C.h. All these libraries are available on the Internet.
2. Initialization of all devices
Each device has its own initialization instruction: RTC, LCD, SD card writer, and BMP280. As air pressure decreases with altitude the BMP280 needs to know at which altitude above sea level the location of the device is to correct for this effect. My little workshop is at 5 meters above sea level which translates into the instruction ‘#define P0 1025.45’.
In I2C communication each device on the bus has its own address. Addresses of I2C devices can be recovered via a handy utility named ‘I2C_address_scanner.ino’. (public domain sketch, available at the arduino.cc forum; sketch added to the zip file). The BMP280 used here has address 0x68 while the LCD display has address 0x3F.
- Serial Monitor
It is advisable to have Serial Monitor running in the background, initially for debugging purposes, later just for monitoring actions. Visual indication of action is provided in the current design by two leds in the wiring circuit: a yellow led that lights during a write action to the SD card (it lights up continuously when the Arduino detects that there is no SD card in the card slot) and a green led that flashes at the end of each data sampling period. The yellow led flashes shortly after a write action to the SD card.
- LCD display
The LCD displays ambient barometric pressure (in millibar) and ambient room temperature. Background activity such as sampling, writing to file, and error situations, are reported by led activity. Writing to files is also reported on the LCD display. The 20×4 display is a standard 2004A 16-pins 20×4 LCD device with an I2C converter board soldered onto its pins.
There are two sketches.
I2C_address_scanner.ino. A public domain utility to find the device’s I2C address
This sketch does the following:
- prepares the Arduino Uno;
- initializes the BMP280;
- initializes the LCD display;
- initializes the RTC;
- initializes the SD card writer;
- calls a time stamp;
- stores the number of pulses recorded in 2-minute intervals;
- writes data comma delimited to file on the SD memory card;
- blinks the green led to report taking a sample;
- blinks the yellow led to report a flush operation or an error situation.
- displays on the LCD the ambient barometric pressure
- displays on the LCD the ambient temperature
The SD logger had been tested both for more than one month, with very satisfying results. The Arduino-RTC/SD shield were reliable and functioned without glitches, errors or aberrations. In a spreadsheet program graphs such as in figure 3 can be made.
Figure 3: Nearly 48 hrs of logging during the 15th and 16th June, 2017 of the barometric pressure with the current construction. X-axis: sample points (one sample point every 5 minutes; 288 sample points per day). Y axis: millibar of pressure.
In the current project a logging device was constructed that is based on I2C wiring and communication. Apart from power and GND there are only two wires involved and consequently only two pins on the shield are occupied: A4 and A5. Especially for the LCD display the I2C solution has advantages. As the classical wiring of a LCD display requires 6 pins of the Arduino, the I2C frees pins to connect with other devices. Also, less wires means lower complexity. A disadvantage of the I2C bus is that if during operation one device is disconnected, e.g. the LCD is disconnected to save operational life, the I2C communication freezes and is not restored when the device is reconnected.
While building the logging device can be considered a success, working with SD cards can be tedious. A next step is to connect the Arduino via a network shield to the home network to enjoy continuous logging without the hassle with removing, reading and replacing SD cards.
- An Arduino Nano weather station: air pressure, ambient temperature and relative humidity – February 2, 2017
- Multi temperature-humidity sensing with an Arduino Nano displayed on a 3.5″ color TFT screen – March 8, 2016
Sketches can be downloaded from:
The zip file is named ‘BMP280_Uno_SD_logger_I2C_LCD.zip‘
it contains two sketches: