Tinkering TI MSP430F5529

One-Wire Communication with DHT11

DHT11 relative humidity & temperature sensor can be interfaced with a microcontroller via one wire. However, one-wire communication is not standard format of communication like UART, I2C and SPI. Every device has its own protocol that differs a lot with others. However, the basic trick in one-wire protocol is time-slotting mechanism. Ones and zeros are represented by pulses of different pulse widths or duty cycle.

DHT11 can be used to measure temperatures nominally from 0°C to 50°C and relative humidity from 20% to 95%. Though it is not very precise, it can be used for general uses.

A big advantage of this sensor is its ability to operate at 3.3V although it is recommended to apply supply voltages between 3.5V – 5.5V. 

Code Example

DHT11.h

#include "driverlib.h"
#include "delay.h"

#define HIGH                    1
#define LOW                     0

#define DHT11_PORT              GPIO_PORT_P3

#define DHT11_pin               GPIO_PIN7

#define DHT11_DIR_OUT()         GPIO_setAsOutputPin(DHT11_PORT, DHT11_pin)
#define DHT11_DIR_IN()          GPIO_setAsInputPin(DHT11_PORT, DHT11_pin)

#define DHT11_pin_HIGH()        GPIO_setOutputHighOnPin(DHT11_PORT, DHT11_pin)
#define DHT11_pin_LOW()         GPIO_setOutputLowOnPin(DHT11_PORT, DHT11_pin)

#define DHT11_pin_IN()          GPIO_getInputPinValue(DHT11_PORT, DHT11_pin)

void DHT11_init(void);
unsigned char get_byte(void);
unsigned char get_data(void);

DHT11.c

#include "DHT11.h"

unsigned char values[0x05];

void DHT11_init(void)
{
   DHT11_DIR_IN();
   delay_ms(1000);
}

unsigned char get_byte(void)
{
   unsigned char s = 0;
   unsigned char value = 0;

   DHT11_DIR_IN();

   for(s = 0; s < 8; s++) 
   {
      value <<= 1;
      while(DHT11_pin_IN() == LOW);
      delay_us(30);

      if(DHT11_pin_IN() == HIGH)
      {
          value |= 1;
      }
      
      while(DHT11_pin_IN() == HIGH);
   }
   return value;
}

unsigned char get_data(void)
{
   short chk = 0;
   unsigned char s = 0;
   unsigned char check_sum = 0;

   DHT11_DIR_OUT();

   DHT11_pin_HIGH();
   DHT11_pin_LOW();
   delay_ms(18);
   DHT11_pin_HIGH();
   delay_us(26);
   
   DHT11_DIR_IN();

   chk = DHT11_pin_IN();
   
   if(chk)
   {
      return 1;
   }
   delay_us(80);
   
   chk = DHT11_pin_IN();

   if(!chk)
   {
      return 2;
   }
   delay_us(80);

   for(s = 0; s <= 4; s += 1)
   {
       values[s] = get_byte();
   }

   DHT11_DIR_OUT();
  
   DHT11_pin_HIGH(); 
   
   DHT11_DIR_IN();

   for(s = 0; s < 4; s += 1)
   {
       check_sum += values[s];
   }
   
   if(check_sum != values[4])
   {
      return 3;
   }
   else
   {
      return 0;
   }
}

main.c

#include "driverlib.h"
#include "delay.h"
#include "DHT11.h"
#include "lcd.h"
#include "lcd_print.h"

extern unsigned char values[0x05];

void clock_init(void);

void main(void)
{
    unsigned char state = 0;

    WDT_A_hold(WDT_A_BASE);

    clock_init();

    LCD_init();
    load_custom_symbol();

    DHT11_init();

    while(1)
    {
        state = get_data();

        switch(state)
        {
            case 1:
            {
            }
            case 2:
            {
                 LCD_clear_home();
                 LCD_putstr("No Sensor Found!");
                 break;
            }
            case 3:
            {
                 LCD_clear_home();
                 LCD_putstr("Checksum Error!");
                 break;
            }
            default:
            {
                 LCD_goto(0, 0);
                 LCD_putstr("R.H/ %:       ");

                 print_C(13, 0, values[0]);

                 LCD_goto(0, 1);
                 LCD_putstr("Tmp/");
                 print_symbol(4, 1, 0);
                 LCD_goto(5, 1);
                 LCD_putstr("C:");

                 if((values[2] & 0x80) == 1)
                 {
                     LCD_goto(13, 1);
                     LCD_putstr("-");
                 }
                 else
                 {
                     LCD_goto(13, 1);
                     LCD_putstr(" ");
                 }

                 print_C(13, 1, values[2]);
                 break;
            }
        }

        delay_ms(1000);
    };
}

void clock_init(void)
{
    PMM_setVCore(PMM_CORE_LEVEL_3);

    GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5,
                                               (GPIO_PIN4 | GPIO_PIN2));

    GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P5,
                                                (GPIO_PIN5 | GPIO_PIN3));

    UCS_setExternalClockSource(XT1_FREQ,
                               XT2_FREQ);

    UCS_turnOnXT2(UCS_XT2_DRIVE_4MHZ_8MHZ);

    UCS_turnOnLFXT1(UCS_XT1_DRIVE_0,
                    UCS_XCAP_3);

    UCS_initClockSignal(UCS_MCLK,
                        UCS_XT2CLK_SELECT,
                        UCS_CLOCK_DIVIDER_1);

    UCS_initClockSignal(UCS_SMCLK,
                        UCS_REFOCLK_SELECT,
                        UCS_CLOCK_DIVIDER_1);

    UCS_initClockSignal(UCS_ACLK,
                        UCS_XT1CLK_SELECT,
                        UCS_CLOCK_DIVIDER_1);
}

Hardware Setup

Explanation

One-wire sensors and devices use time-slotting mechanism to communicate with host micro. Time-slotting and communication methodology differ from device-to-device and have no standards like I2C, SPI, UART, etc. Despite this fact, communicating with one-wire devices is not very difficult. All we have to do is to generate timed pulse or read pulse transitions. 

Shown below is the timing diagram for DHT11 relative humidity & temperature sensor. We have to keep the one-wire data line floating unless there is any communication. A floating pin is basically an externally pulled-up input pin unless used otherwise. The pull-up is achieved using an external resistor connected between the host micro’s pin and power supply’s positive line.  

A host micro has to pull-down the floating communication low for about 18ms and then keep it high for about 30µs to command DHT11 to send out its temperature and humidity readings. This is done as follows in the code below.

DHT11_pin_HIGH();
DHT11_pin_LOW();
delay_ms(18);
DHT11_pin_HIGH();
delay_us(26);

It is very straight forward. DHT11 upon receiving these logic-level transitions starts to serially send out its data readings one-by-one. During reads, we have to check timings in order to determine if the sent pulse is denoting a one or zero. If an incoming pulse is high for more than 30µs, it is treated as a one or else it is treated otherwise. Again, it is pretty simple. 

value <<= 1;
while(DHT11_pin_IN() == LOW);
delay_us(30);

if(DHT11_pin_IN() == HIGH)
{
   value |= 1;
}
      
while(DHT11_pin_IN() == HIGH);

Demo

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

Related Posts

14 comments

Leave a Reply

Your email address will not be published. Required fields are marked *