logo elektroda
logo elektroda
X
logo elektroda

DHT12 and BMP280 sensor conflict on I2C in ESP32 DevKit V1

Askabius 798 15
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18538017
    Askabius
    Level 5  
    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
    khoam
    Level 42  
    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?
  • ADVERTISEMENT
  • #3 18538340
    Askabius
    Level 5  
    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
    khoam
    Level 42  
    Try instead of:
    Code: C / C++
    Log in, to see the code
    using:
    Code: C / C++
    Log in, to see the code
    if the DHT12 address is other than 0x5C, then you need to use:
    Code: C / C++
    Log in, to see the code
    .
    https://github.com/xreef/DHT12_sensor_library...master/examples/esp32TwoWire/esp32TwoWire.ino


    Conversely, if you are using the default SDA(21) and SCL(22) pins on the ESP32, then Wire.begin(21, 22) is redundant in this code - bmp280.begin() already performs this operation.
  • #5 18538446
    Askabius
    Level 5  
    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  
    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  
    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  
    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.
  • #9 18538623
    Askabius
    Level 5  
    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
    khoam
    Level 42  
    With such a constructor initiation:
    Code: C / C++
    Log in, to see the code
    Try the following code to initiate sensor operation:
    Code: C / C++
    Log in, to see the code
    .
  • #11 18538695
    Askabius
    Level 5  
    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
    khoam
    Level 42  
    The problem is with the DHT12 library, which "tacitly" assumes that the operating frequency of the I2C bus must be 100000 kHz. The same, is true of the RTC library. However, the BMP280_DEV constructor sets a default speed of 400000 kHz, so this must be changed later :) .
  • ADVERTISEMENT
  • #13 18538718
    n6210
    Level 29  
    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  
    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!
  • #15 18538770
    khoam
    Level 42  
    n6210 wrote:
    fact I even saw that, but somehow I didn't look into the others
    .
    In these others there is simply Wire.begin(), which in the case of the Arduino HAL for ESP32 means that the default speed of 100000 kHz will be assumed. In the code of the DHT12 and RTClib libraries themselves, there is no trace of information that their authors tested them with the ESP32. Should the user get the urge to change the SDA/SCL pins to something other than the default, these libraries would not work and would need to be reworked.

    It is possible to restore the I2C bus speed to 400000 kHz, but only after initiation DHT and RTC:
    Code: C / C++
    Log in, to see the code
    .

    Askabius wrote:
    as if between SETUP and LOOP a few pins change state to HIGH, so completely without reason. Of course I set pinMode(pin, OUTPUT); and digitalWrite(LOW) in SETUP and no more other operations on these pins in SETUP, yet they enter the loop with a high state.
    .
    What GPIOs are these specifically?
  • #16 18539333
    Askabius
    Level 5  
    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.
Summary generated by the language model.
ADVERTISEMENT