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. The error in measuring humidity is 5%, and temperature is 2 ° C. Capture time 1 sec. Communication interface single wire ( datashit ). Such modest parameters limit the scope of the sensor only to domestic even room conditions.
    image
    I wanted to compare the readings of the device on the HCH1000 + DS18B20 with DHT11.

    I mistakenly concluded that the DHT11 and DS18B20 get along on the same bus. It turned out they have very different protocols. After a bad 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 the bus level low for about 20 ms, then release it, after 20 ms the sensor clamps it to 0 by itself and holds 80 μs then releases it for 80 μs (generates a presence signal), it is followed by 40 bits of data, with the same start - holding the bus to 50 μs and bit. bit 0 is somewhere around 28 μs, bit 1 is 70 μs. Well, goodbye, the sensor clamps and releases the bus. 40 data bits are 5 bytes of which the first two are humidity, the next 2 are temperature and the parity byte. The parity byte is the sum of the previous bytes. 1st byte and 3rd byte transmit values, 2 and 4, as I understand it, are reserved for tenths.
    The easiest way to read the duration of pulses and write them to an array. Then transform this array into a byte array. So 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) 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;	
    }
    

    A project containing the dht11.c library is here .

    Also popular now: