2

I am building a weather station with an Arduino slave (dedicated to a rain gauge - depicted below as a reed switch) and a NodeMCU master with some additional sensors.

Arduino slave NodeMCU master

One such sensor, a BMP280 uses D1 and D2 as SCL and SDA, while slave control is using D3 and D4 for communication with Arduino.

When I enable Wire.begin for slave communication, my BMP280 readings go nuts

#include <Adafruit_BMP280.h>

#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11 
#define BMP_CS 10

Adafruit_BMP280 bmp;

// masterSlave
#include <Wire.h>
#define I2CAddressESPWifi 8
...

void setup() {

    if (!bmp.begin()) {  
        Serial.println("Could not find a valid BMP280 sensor, check wiring!");
        while (1);
    }

    // masterSlave - generates problem with BMP280...
    Wire.begin(0,2);//Change to Wire.begin() for non ESP. D3 SDA  D4 SCL

BMP280 Temp = -136.42 *C

Pressure = 125136.25 Pa

as opposed to this reading when Wire.begin is commented out.

    // COMMENTED OUT FOR DEBUGGING
    //masterSlave - generates problem with BMP280...
    //Wire.begin(0,2);//Change to Wire.begin() for non ESP. D3 SDA  D4 SCL

BMP280 Temp = 20.01 *C

Pressure = 93464.36 Pa

I attempted setting D3 and D4 to LOW while reading from BMP280, but it had no effect on readings.

6
  • 3
    Hint#1: I2C pullup resistors. May 18, 2019 at 12:13
  • 2
    share your full sketch here.
    – Vaibhav
    May 18, 2019 at 12:20
  • 2
    The I2C is designed for short distances on a PCB. How far will be the slave?
    – Juraj
    May 18, 2019 at 12:26
  • @Juraj some few centimeters - same board (there's a 10m extension cable for the rain gauge - that part is working perfectly).
    – tony gil
    May 18, 2019 at 12:36
  • @MikaelPatel I'll play some more with the pullup resistor and get back to you.
    – tony gil
    May 18, 2019 at 12:37

3 Answers 3

2
+50

The Adafruit BMP280 library uses the Wire library. It calls begin() at default pins (D1, D2). And then you begin() the same Wire object at different pins. From that moment the BMP280 library can't communicate with the sensor.

The I2C (a.k.a. TwoWire or Wire) is a bus. Many devices can be wired to the same pair of pins. They must listen on different addresses. So wire both devices to the D1, D2 and use in slave Arduino an address different then the address of the sensor.

The esp8266 and the sensor are 3.3 V devices. The Uno is a 5 V device. You should use logic level conversion on SDA/SCL to Uno.

13
  • 1
    not "to be addressed". but "having an address". some sensors have fixed address, some have a choice from a few addreses
    – Juraj
    May 18, 2019 at 12:43
  • 1
    the default I2C address of the Adafruit BMP280 is 0x77
    – Juraj
    May 18, 2019 at 14:14
  • 1
    this is not a solution but an explanation. what is wrong after you use the Wire library on one set of pins?
    – Juraj
    May 21, 2019 at 10:54
  • 1
    on D1, D1 and you removed Wire.begin(0, 2)?
    – Juraj
    May 21, 2019 at 16:50
  • 1
    I added a note about logic level conversion. and thank you.
    – Juraj
    May 27, 2019 at 12:58
1

As per your descriptions, I'm answering here. There is a fact we forget about the load capacitance calculation for selecting I2C pull-ups. It's calculated when more than one slave and variant to distance. Check the jumpers and for pullups try 10k or 4.7K or better you can calculate. It's better to provide a small delay after the wire.begin() function to stabilize the module

1
  • would you care to scribble that on the fritzing diagram in the question?
    – tony gil
    May 21, 2019 at 15:42
0

The solution I implemented uses the BMP280 sensor in SPI mode, connected as follows:

Breadboard scheme

with this code before setup

#include <Adafruit_BMP280.h>
#define BMP_SCK D5
#define BMP_MISO D8
#define BMP_MOSI D6 
#define BMP_CS D7

//Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);
1
  • which doesnt EXACTLY answer my answer, though it solves the practical problem (just in case anyone wants a workaround). ;)
    – tony gil
    May 28, 2019 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.