From ebfce9115da2a28c4e3de46b006c0ee6692af5ea Mon Sep 17 00:00:00 2001 From: minomy13 Date: Wed, 13 Nov 2024 16:48:52 +0100 Subject: [PATCH] added C++ logic for enabling and disabling internal pull-up resistor from config --- esphome/components/dht/dht.cpp | 13 ++++++++++++- esphome/components/dht/dht.h | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/esphome/components/dht/dht.cpp b/esphome/components/dht/dht.cpp index 7e61c3869a..58285e324f 100644 --- a/esphome/components/dht/dht.cpp +++ b/esphome/components/dht/dht.cpp @@ -24,6 +24,12 @@ void DHT::dump_config() { ESP_LOGCONFIG(TAG, " Model: DHT22 (or equivalent)"); } + if (this->use_internal_pullup_ == true) { + ESP_LOGCONFIG(TAG, " Internal pull-up resistor enabled.") + } else { + ESP_LOGCONFIG(TAG, " Internal pull-up resistor disabled.") + } + LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); @@ -101,7 +107,12 @@ bool HOT IRAM_ATTR DHT::read_sensor_(float *temperature, float *humidity, bool r } else { delayMicroseconds(800); } - this->pin_->pin_mode(gpio::FLAG_INPUT); + + if (this->use_internal_pullup_ == true) { + this->pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP); + } else { + this->pin_->pin_mode(gpio::FLAG_INPUT); + } { InterruptLock lock; diff --git a/esphome/components/dht/dht.h b/esphome/components/dht/dht.h index 327e8a4f5c..2b1411de76 100644 --- a/esphome/components/dht/dht.h +++ b/esphome/components/dht/dht.h @@ -42,6 +42,7 @@ class DHT : public PollingComponent { void set_model(DHTModel model) { model_ = model; } void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; } void set_humidity_sensor(sensor::Sensor *humidity_sensor) { humidity_sensor_ = humidity_sensor; } + void set_internal_pullup(bool use_internal_pullup) { this->use_internal_pullup_ = use_internal_pullup; } /// Set up the pins and check connection. void setup() override; @@ -59,6 +60,7 @@ class DHT : public PollingComponent { bool is_auto_detect_{false}; sensor::Sensor *temperature_sensor_{nullptr}; sensor::Sensor *humidity_sensor_{nullptr}; + bool use_internal_pullup_{true}; }; } // namespace dht