RobTillaart / DHT20

Arduino library for DHT20 I2C temperature and humidity sensor.
MIT License
21 stars 6 forks source link

Compatible with old and new begin() #16

Closed countrysideboy closed 8 months ago

RobTillaart commented 8 months ago

Hi, I understand the PR request, however I removed the Wire parameters of begin with a number of reasons.

  1. The old method was error prone as there was no check upon validness of the pins. Now it is made a user responsibility.
  2. setting Wire attributes internally in libraries can conflict with settings of other libraries. OK, small chance but it caught me
  3. Design the new interface uses proper dependency injection. (at least in most libraries now, still ~20 to go)
  4. As the Wire interfaces of AVR, ESP, STM, RP2040 etc have minor differences the amount of conditional code increased. The same I have seen with the SPI interface.

The new interface is not difficult to patch in your sketch. You only need to call Wire.begin() or Wire.begin(sda, scl) before the DHT20.begin()** and your sketch should work.

countrysideboy commented 8 months ago

I use the DHT.begin(ada,scl) to test if the sensor is exisit, and swap the pin. it is not difficult to adapt the code to the new version. And the pr2040 do use the different method to change the default i2c pin.

RobTillaart commented 8 months ago

For the esp32 you could use the example DHT20_test_esp.ino

In this example you just swap the parameters in Wire.begin(sda, scl)


For the RP2040, there are two calls to change the pins

Wire.setSDA(pin);
Wire.setSCL(pin);

Option is to add a new example DHT20_test_RP2040 .ino

RobTillaart commented 8 months ago

Added an example - DHT20_test_rp2040.ino