The Adafruit BME280 sensor library supports passing in alternative GPIO pins from the default D2 (GPIO 4) and D1 (GPIO 5) for SDA and SCL respectively for the Wemos D1 Mini. Since the I2C is software driven on the D1 Mini, it is reasonably straight forwards to assign different ports. Ports D6 and D7 line up well with the particular BME280 carrier board I’m using, which allows me to directly attach the BME280 over the D1 Mini, attaching SDA to D6, SCL to D7 and VIN to 3v.
In order to change ports, include the Wire library and define the ports:
#include <Wire.h> #define SDA_1 12 #define SCL_1 13
Then call Wire.begin
to initialise the ports, passing in the default sensor address space 0x76
and the wire object into the bme call.
Wire.begin(SDA, SCL) unsigned status; status = bme.begin(0x76, &Wire);
The complete sample code looks like this:
/*************************************************************************** This is a library for the BME280 humidity, temperature & pressure sensor Designed specifically to work with the Adafruit BME280 Breakout ----> http://www.adafruit.com/products/2650 These sensors use I2C or SPI to communicate, 2 or 4 pins are required to interface. The device's I2C address is either 0x76 or 0x77. Adafruit invests time and resources providing this open source code, please support Adafruit andopen-source hardware by purchasing products from Adafruit! Written by Limor Fried & Kevin Townsend for Adafruit Industries. BSD license, all text above must be included in any redistribution See the LICENSE file for details. ***************************************************************************/ #include <Wire.h> #include <SPI.h> #include <Adafruit_Sensor.h> #include <Adafruit_BME280.h> #define SDA_1 12 #define SCL_1 13 #define SEALEVELPRESSURE_HPA (1013.25) Adafruit_BME280 bme; // I2C unsigned long delayTime; void setup() { Serial.begin(9600); while(!Serial); // time to get serial running Serial.println(F("BME280 test")); // Init the ports for BME280 Wire.begin(SDA_1, SCL_1); // Capture the BME init status, passing in custom Wire GPIO pins unsigned status; status = bme.begin(0x76, &Wire); if (!status) { Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!"); Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16); Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n"); Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n"); Serial.print(" ID of 0x60 represents a BME 280.\n"); Serial.print(" ID of 0x61 represents a BME 680.\n"); while (1) delay(10); } Serial.println("-- Default Test --"); delayTime = 1000; Serial.println(); } void loop() { printValues(); delay(delayTime); } void printValues() { 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(); }
Leave a Reply