logo elektroda
logo elektroda
X
logo elektroda

DHT12 and BMP280 sensor conflict on I2C in ESP32 DevKit V1

Askabius 1326 15
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18538017
    Askabius
    Level 5  
    Posts: 30
    Rate: 20
    Board Language: polish
    Welcome,

    I have a problem with I2C communication on the ESP32 DevKit V1 board. Well, I wanted to connect several sensors supporting this standard to one I2C bus. Namely: DHT12 with library <DHT12.h>, BMP280 with library <BMP280_DEV.h> and RTC module with library <RTClib.h>. (I don't physically have the RTC module and the BMP280 yet, but I've already written code to support them). The problem is that the function :

    Code: C / C++
    Log in, to see the code
    .

    causes the DHT12 sensor to malfunction - it shows NaN (Not a Number) values. When this function is removed, the data shown is correct. However, obviously this function is necessary during operation. I have included a code snippet below:

    Code: C / C++
    Log in, to see the code
    .

    Could someone explain to me how to make this connection correctly?
    PS. I would prefer not to change the libraries used to other libraries as this would involve changing quite a lot of the program. I can edit the libraries if that helps and someone could tell me what to change.

    Thank you in advance for your help!
  • ADVERTISEMENT
  • #2 18538293
    Anonymous
    Level 1  
  • #3 18538340
    Askabius
    Level 5  
    Posts: 30
    Rate: 20
    Board Language: polish
    khoam wrote:
    Askabius wrote:
    DHT12 with library <DHT12.h>
    .
    Which specific bilbibliotheca are you referring to? By the name of the header file alone, it will be hard to guess.

    How are the bmp280 and dht12 constructors initialised;
    .

    Here is the link to the library: https://github.com/xreef/DHT12_sensor_library

    And the constructors are defined like this:

    Code: C / C++
    Log in, to see the code
    .

    EDIT: I'll add that in the libraries, the DHT has an address of 0x5C and the BMP has 0x77 or 0x76 (you can choose an alternative)
  • ADVERTISEMENT
  • #4 18538400
    Anonymous
    Level 1  
  • #5 18538446
    Askabius
    Level 5  
    Posts: 30
    Rate: 20
    Board Language: polish
    I have done as instructed, but still the same error - when bmp.begin() is plugged in, the value of the sensors from the DHT12 takes the form NaN, and when the bmp.begin() function is commented out, the readings are correct :/.

    EDIT: After removing Wire.begin() and adding an address (same or different) also no change
  • #6 18538521
    n6210
    Level 29  
    Posts: 1000
    Help: 126
    Rate: 168
    Board Language: polish
    Is this the lib you are using? https://github.com/MartinL1/BMP280_DEV ?
    Because if I take a quick look, I think it only supports SPI for ESP32 - the same in the example.

    Code: C / C++
    Log in, to see the code
    .
    EDIT
    And not one I missed the builder for I2C :oops: .
    but maybe you missed it:
    Code: C / C++
    Log in, to see the code
  • #7 18538554
    Askabius
    Level 5  
    Posts: 30
    Rate: 20
    Board Language: polish
    Yes, exactly from this one. It has examples of use with I2C: (this is what I relied on)
    Code: C / C++
    Log in, to see the code
    .

    But if you could recommend any reasonably simple library for communicating with this chip that will 100% work then I'll be picky and throw this non-working crap away....
  • #8 18538574
    n6210
    Level 29  
    Posts: 1000
    Help: 126
    Rate: 168
    Board Language: polish
    Not my mistake though, I corrected the question at the top if you have the conversion start in the code as in the example. I understand you fired up the example itself? I don't have anything better unfortunately.
  • ADVERTISEMENT
  • #9 18538623
    Askabius
    Level 5  
    Posts: 30
    Rate: 20
    Board Language: polish
    n6210 wrote:
    .

    Code: C / C++
    Log in, to see the code
    .

    Indeed I didn't have that, but after adding unfortunately no change :/.
  • Helpful post
    #10 18538649
    Anonymous
    Level 1  
  • #11 18538695
    Askabius
    Level 5  
    Posts: 30
    Rate: 20
    Board Language: polish
    khoam wrote:
    bmp280.begin(0x77);
    bmp280.setClock(100000);
    dht12.begin();
    .

    I almost fell off my chair....
    It works! I hope it will also work after the physical connection of the BMP and the RTC, but already a big success!
    I'm keeping the topic open for now, I'll let you know when the Post Office graciously delivers the sensors to me and have a chance to test them.
  • #12 18538714
    Anonymous
    Level 1  
  • #13 18538718
    n6210
    Level 29  
    Posts: 1000
    Help: 126
    Rate: 168
    Board Language: polish
    is there a problem with 400khz? @khoam class :spoko: .

    Added after 2 [minutes]:

    khoam wrote:
    BMP280_DEV sets a default speed of 400000 kHz, so you need to change this later
    fact I even saw this, but somehow I didn't look into the others
  • #14 18538753
    Askabius
    Level 5  
    Posts: 30
    Rate: 20
    Board Language: polish
    Well, then all that's left for me to do is to make sure that this wretched bus laughs without too many problems, add some sensible support for the Blynk terminal and eliminate one more small bug and I'll be able to consider the project finished!

    And, this "little bug" is that at some point during startup, as if between SETUP and LOOP, a few pins change state to HIGH, for no reason at all. Of course, I set pinMode(pin, OUTPUT); and digitalWrite(LOW) in SETUP and there are no more operations on these pins in SETUP, yet they enter the loop with a high state. And the best thing is that when I give pinMode(pin, INPUT) to these pins in SETUP, a voltage of 0.67V appears on them. Anticipating questions - according to the documentation, there should be no voltage on these pins during Loss/Boot or other cases. I'll have a look at it and if I can figure out what I mean, I'll post the code.

    Thanks again for your help!
  • ADVERTISEMENT
  • #15 18538770
    Anonymous
    Level 1  
  • #16 18539333
    Askabius
    Level 5  
    Posts: 30
    Rate: 20
    Board Language: polish
    khoam wrote:
    What GPIO is this specifically?


    GPIOS: 13, 23, 2, 12, 25 and maybe some others I haven't picked out, but these 5 for sure. But first I'll make sure it's not a bug in the program, which to myself may be calling functions that change the state of these pins for some reason.

    EDIT: It turned out that the Blynk.run() function is responsible for this change; But interestingly, it runs all the time in a loop, and only sets the pins to high in its first execution.

    EDIT2: SOLVED!

    It turned out that the Blynk.syncAll(); function makes all the Blynk.write(); events execute; That is, in this case, as if I had pressed all the buttons that can turn something on at startup.

Topic summary

✨ The discussion revolves around an I2C communication issue on the ESP32 DevKit V1 when connecting multiple sensors: DHT12, BMP280, and an RTC module. The user experiences a malfunction with the DHT12 sensor, which returns NaN values when the BMP280's initialization function is called. Various suggestions are made, including modifying the constructor for the DHT12 sensor to include the Wire object and ensuring the I2C bus speed is set correctly. The solution involves initializing the BMP280 with a specific address and setting the I2C clock speed to 100 kHz before calling the DHT12's begin function. The user confirms that the solution works, and further issues related to GPIO pin states during startup are also addressed, revealing that the Blynk library functions were causing unexpected behavior.
Generated by the language model.

FAQ

TL;DR: DHT12 returns NaN because BMP280_DEV switches the I2C bus from 100 kHz to 400 kHz; as one expert notes, the DHT12/RTClib "must be 100000". Fix by setting 100 kHz after bmp280.begin, then dht12.begin. [Elektroda, khoam, post #18538714]

Why it matters: For ESP32 users combining DHT12, BMP280, and RTClib on one I2C bus, this eliminates NaN readings and boot-time conflicts.

Quick Facts

How do I fix the DHT12 and BMP280 I2C conflict on ESP32?

Match I2C speed to the slowest device during init. Initialize BMP280 first, then lower the I2C clock, then start DHT12. This prevents DHT12 from returning NaN. How-To: 1. bmp280.begin(0x77 or 0x76). 2. bmp280.setClock(100000). 3. dht12.begin(). [Elektroda, khoam, post #18538649]

Why does DHT12 show NaN after calling bmp280.begin()?

BMP280_DEV sets the I2C clock to 400 kHz by default. DHT12 and RTClib assume 100 kHz and fail at 400 kHz, causing NaN. As noted, they "must be 100000" during operation. Lower the speed after bmp280.begin, then start DHT12. [Elektroda, khoam, post #18538714]

Do I need to pass the Wire instance or address to DHT12?

Yes. Use DHT12(&Wire) to ensure all devices share the same I2C bus. If your sensor uses a non-default address, pass DHT12(&Wire, address). This avoids hidden Wire instances that break multi-device operation. [Elektroda, khoam, post #18538400]

Are there I2C address conflicts between DHT12 and BMP280?

No. DHT12 uses 0x5C. BMP280 uses 0x77 or 0x76, depending on the module’s solder bridge or configuration. These addresses do not overlap on the same bus. [Elektroda, Askabius, post #18538340]

Should I call Wire.begin() explicitly on ESP32 DevKit V1?

Not when you use the default pins. bmp280.begin() already performs Wire.begin internally, so an extra Wire.begin(21,22) is redundant. Keep SDA=21 and SCL=22 for simplicity. [Elektroda, khoam, post #18538400]

What I2C speed should I use with DHT12, BMP280, and RTClib?

Use 100 kHz while initializing DHT12 and RTC to ensure stable transfers. After their begin calls, you can restore fast-mode. "It is possible to restore the I2C bus speed to 400000" using Wire.setClock(400000). [Elektroda, khoam, post #18538770]

Do I need startNormalConversion() to get BMP280 readings?

Yes. After bmp280.begin(), set a standby time, then call startNormalConversion(). This starts continuous measurements. Example: setTimeStandby(TIME_STANDBY_1000MS); startNormalConversion(). It complements the speed fix but is not the root cause of NaN. [Elektroda, n6210, post #18538521]

Will BMP280 work at 100 kHz if I lower the I2C speed?

Yes. The BMP280 still reports valid readings at 100 kHz. The user confirmed success after lowering the bus and then starting DHT12. "It works!" [Elektroda, Askabius, post #18538695]

How do I set the BMP280 to address 0x76 instead of 0x77?

Pass the address to bmp280.begin(). Example: bmp280.begin(0x76). The library accepts either 0x77 or 0x76, matching common BMP280 modules. [Elektroda, Askabius, post #18538340]

Can I change ESP32 I2C pins and still use these libraries?

DHT12 and RTClib rely on Wire defaults. If you change SDA/SCL pins, those libraries may fail without modification. Keep defaults or update the libraries to use a passed-in TwoWire instance. [Elektroda, khoam, post #18538770]

Why did some GPIOs go HIGH once at boot when using Blynk?

Blynk.syncAll triggers all BLYNK_WRITE handlers on connect, toggling outputs as if buttons were pressed. The user observed GPIO 13, 23, 2, 12, and 25 going HIGH once. Remove syncAll or guard first-run logic to prevent unintended activation. [Elektroda, Askabius, post #18539333]

Does BMP280_DEV support I2C on ESP32, or only SPI?

It supports both. The library provides I2C examples, and constructing BMP280_DEV without SPI parameters uses I2C. Use begin(), setTimeStandby(), and startNormalConversion() as shown in the example. [Elektroda, Askabius, post #18538554]
Generated by the language model.
ADVERTISEMENT