Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix si7021 temperature/humidity sensor reading #737

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions extras/dht/dht.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@
*
* Initializing communications with the DHT requires four 'phases' as follows:
*
* Phase A - MCU pulls signal low for at least 18000 us
* Phase B - MCU allows signal to float back up and waits 20-40us for DHT to pull it low
* Phase A - MCU pulls signal low for a period (see below)
* Phase B - MCU allows signal to float back up and waits 20-200us for DHT to pull it low
* Phase C - DHT pulls signal low for ~80us
* Phase D - DHT lets signal float back up for ~80us
*
* The length of Phase A is dependent on the sensor type:
* DHT_TYPE_DHT11 : at least 18000 us
* DHT_TYPE_DHT22 : 800 us to 20000 us, typically 1100 us
* DHT_TYPE_SI7021 : 300 us to 500 us
* For a DHT21 use DHT_TYPE_DHT22
* For an AM2301/AM2302 use DHT_TYPE_SI7021
*
* After this, the DHT transmits its first bit by holding the signal low for 50us
* and then letting it float back high for a period of time that depends on the data bit.
* duration_data_high is shorter than 50us for a logic '0' and longer than 50us for logic '1'.
Expand Down Expand Up @@ -91,8 +98,8 @@ static inline bool dht_fetch_data(dht_sensor_type_t sensor_type, uint8_t pin, bo
sdk_os_delay_us(sensor_type == DHT_TYPE_SI7021 ? 500 : 20000);
gpio_write(pin, 1);

// Step through Phase 'B', 40us
if (!dht_await_pin_state(pin, 40, false, NULL)) {
// Step through Phase 'B', 200us
if (!dht_await_pin_state(pin, 200, false, NULL)) {
debug("Initialization error, problem in phase 'B'\n");
return false;
}
Expand Down Expand Up @@ -131,7 +138,7 @@ static inline int16_t dht_convert_data(dht_sensor_type_t sensor_type, uint8_t ms
{
int16_t data;

if (sensor_type == DHT_TYPE_DHT22) {
if (sensor_type == DHT_TYPE_DHT22 || sensor_type == DHT_TYPE_SI7021) {
data = msb & 0x7F;
data <<= 8;
data |= lsb;
Expand Down