Skip to content

SDLoggerSPIDemo.ino

Arnd edited this page Dec 12, 2020 · 5 revisions

Arduino sketch to read measurements from a Bosch BME680 sensor sensor using SPI and to log that data to a SD Card, also using SPI. This example was added at Version v1.0.6 of the library.

Compatibility

No special processor-dependent calls are used and thus this sketch should run on any of the Arduino systems. The hardware SPI lines for SCK, MISO and MOSI are shared on both devices while the BME680 uses default "SS" Arduino pin and the SD-Card is connected to the "A4" (or pin 24) of the example Arduino Micro; this value can be changed depending upon which pin is to be used on whatever microprocessor has been selected.

Breadboard example above using an Arduino Micro, an Adafruit 4-channel I2C-Safe Bi-directional logic level converter, a Sparkfun BME680 Breakout board and a Sparkfun SD-Card breakout board. Since the Arduino Micro is a 5V microprocessor and the BME680 breakout board is a 3V device, the select pins for both devices as well as the MOSI and SCK pins need to be level-shifted in both directions and care has to be taken to choose a logic level converter which is compatible with I2C as most are not. The MISO line does not need to be level shifted, as a 3V output from either device will be registered as an "ON" signal by a 5V microprocessor.

The wires I used are color-coded as follows:

Color Name Description
White CS Chip-Select for BME680, attached to Arduino "SS" pin
Yellow MOSI "Master Out, Slave In" SPI pin
Orange MISO "Master In, Slave Out" SPI pin. Direct attachment skipping level shifter
Blue SCK SPI System Clock pin
Green CS Chip-Select for SD-Card, attached to Arduino A4 pin

Description

This demo program has been kept straightforward and simple and does not perform much, if any, error checking. It is being used for a battery-powered refrigerator or freezer monitor. The standard speed is to take a low-quality (and therefore low-energy) measurement every 10 seconds. A running average of the last 10 measurements for temperature, humidity and pressure is kept and if one of the 3 measurements deviates from that average by more a set amount (using program constants), then the device switches to 1 measurement a second at a higher accuracy. Once all 3 measurements go below that trigger threshold for at least a minute the measurement accuracy and rate is again reduced until another event occurs.

Function

The BME680 and SD-Card are instantiated and initialized in the setup() method with the following explicit values:

  • "normal" values of 2x oversampling for temperature and humidity, and no oversampling for humidity
  • the gas sensor is disabled
  • IIR filtering is turned on to 2x
  • One measurement is done every 10 seconds

After initialization the program loops infinitely. It displays the temperature in degrees Celsius, the humidity in % and the pressure in hPA along with the running averages for all 3 data streams. Once a trigger event occurs, the BME680 is switched to

  • "normal" values of 8x oversampling for temperature and humidity, and no oversampling for humidity
  • the gas sensor remains disabled
  • IIR filtering is turned on to 8x
  • One measurement is done every second

Once all 3 measurements fall back within the accepted tolerances, the program will continue to report at the higher rate for one minute, then revert to the initial state.