As I mentioned in a recent post, I recently purchased more than a dozen of sensors from Adafruit for about $10 apiece. Many (but importantly, not all) have both I2C and SPI (Serial Peripheral Interface). Also as I mentioned, you will need to solder on pins or wires if you’re going the SPI route.

Adafruit BMP280 Sensor with SPI and I2C Connectors

Adafruit Libraries and Test Software on Arduino-based PLCs

Using the SPI interface on the Arduino-based PLCs is somewhat less painless than using I2C. First, as mentioned you’ll have to solder on pins or ribbon cable to the PCB. If you haven’t already installed the software for the I2C interface (it’s the same code for both) simply click on the Tools | Manage Libraries… option from the drop-down menu, and wait. Once all the installed libraries have been scanned, you can search for your new sensor, and install the Adafruit library. For example, if you search for “BMP280”, you should find a half-dozen or so options from Adafruit and a variety of other suppliers. Select the Adafruit option (the install button should appear in the lower right corner when you hover on the Adafruit option; you may be given an option to select a particular version.

M-Duino 21+ with BMP280 Sensor and Ribbon Cable

Connect the Sensors, Modify and Load the Test Software

Next, connect the sensor to your PLC: red goes to 5V or 3V, and black goes to GND. You also need to connect the CS, MISO, MOSI, and SCK pins – I used white for CS, purple for MISO, orange for MOSI, and yellow for SCK. If your PLC supports the SPI bus, you’ll see the MISO, MOSI, and SCK labels on case. These three seem to be consistently pins 50, 51, and 52. The one you won’t see printed on your case is CS. There are a variety of pins to choose from (see section 7 in your User’s Guide). The two that I found to work most reliable are SCL (21) and SDA (20). Unfortunately, using either of those means that you can’t use I2C sensors at the same time.

Click on File | Examples | Adafruit BMP280 Library | bmp280_sensortest to open a basic example sketch. Make the changes highlighted in red to use SPI instead of I2C:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

#define CS   21
#define MISO 50
#define MOSI 51
#define SCK  52

//Adafruit_BMP280 bmp();                    // use I2C interface
Adafruit_BMP280 bmp( CS, MOSI, MISO, SCK ); // use SPI interface

Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor();
Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor();

Using a text editor, make the changes highlighted in red below to Adafruit_BMP280.cpp (two folders up from the sketch in Adafruit_BMP280_Library). Do not use the Arduino IDE editor; if you make the changes in the bmp280_sensortest sketch, your build will fail!

 * @brief  BMP280 constructor using bitbang SPI
 * @param  cspin
 *         The pin to use for CS/SSEL.
 * @param  mosipin
 *         The pin to use for MOSI.
 * @param  misopin
 *         The pin to use for MISO.
 * @param  sckpin
 *         The pin to use for SCK.
Adafruit_BMP280::Adafruit_BMP280(int8_t cspin, int8_t mosipin, int8_t misopin,
                                 int8_t sckpin)
    : _cs(cspin), _mosi(mosipin), _miso(misopin), _sck(sckpin)
  temp_sensor = new Adafruit_BMP280_Temp(this);
  pressure_sensor = new Adafruit_BMP280_Pressure(this);

Load the sketch and open the Serial Monitor (at 9600 baud). The local temperature is reported 25°C (77°F) and barometric pressure is 29.8 in Hg (1010 hPa). The BMP280 reports the temperature is 27°C (81°F) and 969 hPa (28.7 in Hg).

Last updated: 2021 May 21