📜 ⬆️ ⬇️

STM32 + DHT11

I got a DHT11 temperature and humidity sensor in my hands. Measures humidity in the range of 20-90% and temperature from 0 to 50 ° C. Error of measurement of humidity of 5%, temperatures of 2 ° C. Capture time 1 sec. Single wire ( datashit ) communication interface. Such modest parameters limit the scope of the sensor only for household conditions.
image
I wanted to compare the device readings on the HCH1000 + DS18B20 with the DHT11.

I mistakenly concluded that the DHT11 and DS18B20 get along on the same bus. It turned out they have completely different protocols. After an unsuccessful experience, hastily assembled a device to establish the truth.
image
I did not find the finished DHT11 library for STM32. For Arduino here . The single wire communication protocol has nothing to do with 1-wire. The difference between 0 and 1 is obtained from the difference in pulse duration.
image
First, we keep about 20 ms low bus level, then let it go, after 20 ms the sensor clamps it to 0 itself and holds 80 µs and then releases it for 80 µs (generates a presence signal), followed by 40 data bits, with the same start - by pinching the tire on 50 μs and a bit. bit 0 is about 28 μs, bit 1 is 70 μs. Well, at parting, the sensor clamps and releases the tire. 40 data bits is 5 bytes, of which the first two are humidity, the next 2 is temperature and parity is byte. The parity byte is equal to the sum of the previous bytes. The 1st byte and 3rd byte transfer the values, 2 and 4 I understand are reserved for the tenths.
The easiest way to read the duration of the pulses and write them into an array. Then transform this array into an array of bytes. This is how the dht11.c library was born.
uint16_t read_cycle(uint16_t cur_tics, uint8_t neg_tic){ uint16_t cnt_tics; if (cur_tics < MAX_TICS) cnt_tics = 0; if (neg_tic) { while (!GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3)&&(cnt_tics<MAX_TICS)){ cnt_tics++; } } else { while (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3)&&(cnt_tics<MAX_TICS)){ cnt_tics++; } } return cnt_tics; } uint8_t read_DHT11(uint8_t *buf){ uint16_t dt[42]; uint16_t cnt; uint8_t i, check_sum; //reset DHT11 Delay(500); GPIO_LOW(GPIOA,GPIO_Pin_2); Delay(20); GPIO_HIGH(GPIOA,GPIO_Pin_2); //start reading cnt = 0; for(i=0;i<83 && cnt<MAX_TICS;i++){ if (i & 1){ cnt = read_cycle(cnt, 1); } else { cnt = read_cycle(cnt, 0); dt[i/2]= cnt; } } //release line GPIO_HIGH(GPIOA,GPIO_Pin_2); if (cnt>=MAX_TICS) return DHT11_NO_CONN; //convert data for(i=2;i<42;i++){ (*buf) <<= 1; if (dt[i]>20) (*buf)++; if (!((i-1)%8) && (i>2)) buf++; } //calculate checksum buf -= 5; check_sum = 0; for(i=0;i<4;i++){ check_sum += *buf; buf++; } if (*buf != check_sum) return DHT11_CS_ERROR; return DHT11_OK; } 

The project containing the dht11.c library is here .

')

Source: https://habr.com/ru/post/160017/


All Articles