DHT11 Sensor Interfacing with ARM LPC2148

Introduction

DHT11 Sensor

DHT11 Sensor

DHT11 is a single wire digital humidity and temperature sensor, which provides humidity and temperature values serially.

It can measure relative humidity in percentage (20 to 90% RH) and temperature in degree Celsius in the range of 0 to 50°C.

It has 4 pins of which 2 pins are used for supply, 1 is not used and the last one is used for data.

The data pin is the only pin used for communication. Pulses of different TON and TOFF are decoded as logic 1 or logic 0 or start pulse or end of the frame.

For more information about DHT11 sensor and how to use it, refer the topic DHT11 sensor in the sensors and modules topic.

 

Interfacing Diagram

Interfacing DHT11 Sensor with LPC2148 Interfacing DHT11 Sensor with LPC2148

 

Example

Let’s see the example for Reading temperature and humidity from DHT11 sensor.

 

Here, the Data Pin of DHT11 is connected to P0.4 of LPC2148. UART0 is used for displaying data on a serial monitor on a PC/laptop.

 

Programming Steps

  • Initialize UART0
  • Make the pin connected to the sensor data pin as an output pin and transmit a Start pulse.
  • Make the pin connected to the sensor data pin as an input pin and receive the Response pulse from the sensor.
  • Once the response pulse is received, receive 40-bit data from the sensor.
  • Display the received data on a serial terminal using UART0. In the case of checksum error, an error indication is also displayed.

 

Program

/*
  DHT11 interfacing with LPC2148(ARM7)
  http://www.electronicwings.com/arm7/dht11-sensor-interfacing-with-lpc2148
*/

#include <lpc214x.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

void delay_ms(uint16_t j)
{
    uint16_t x,i;
	for(i=0;i<j;i++)
	{
    for(x=0; x<6000; x++);    /* loop to generate 1 milisecond delay with 60MHz Cclk */
	}
}

void delay_us(uint16_t j)
{
    uint16_t x,i;
	for(i=0;i<j;i++)
	{
    for(x=0; x<7; x++);    /* loop to generate 1.04 microsecond delay with 60MHz Cclk */
	}
}

void UART0_init(void)
{
	PINSEL0 = PINSEL0 | 0x00000005;	/* Enable UART0 Rx0 and Tx0 pins of UART0 */
	U0LCR = 0x83;	/* DLAB = 1, 1 stop bit, 8-bit character length */
	U0DLM = 0x00;	/* For baud rate of 9600 with Pclk = 15MHz */
	U0DLL = 0x61;	/* We get these values of U0DLL and U0DLM from formula */
	U0LCR = 0x03; /* DLAB = 0 */
}

void UART0_TxChar(char ch) /* A function to send a byte on UART0 */
{
	U0THR = ch;
	while( (U0LSR & 0x40) == 0 );	/* Wait till THRE bit becomes 1 which tells that transmission is completed */
}

void UART0_SendString(char* str) /* A function to send string on UART0 */
{
	uint8_t i = 0;
	while( str[i] != '\0' )
	{
		UART0_TxChar(str[i]);
		i++;
	}
}

unsigned char UART0_RxChar(void) /* A function to receive a byte on UART0 */
{
	while( (U0LSR & 0x01) == 0);	/* Wait till RDR bit becomes 1 which tells that receiver contains valid data */
		return U0RBR;
}

void dht11_request(void)
{
	IO0DIR = IO0DIR | 0x00000010;	/* Configure DHT11 pin as output (P0.4 used here) */
	IO0PIN = IO0PIN & 0xFFFFFFEF; /* Make DHT11 pin LOW for minimum 18 seconds */
	delay_ms(20);
	IO0PIN = IO0PIN | 0x00000010; /* Make DHT11 pin HIGH and wait for response */
}

void dht11_response(void)
{
	IO0DIR = IO0DIR & 0xFFFFFFEF;	/* Configure DHT11 pin as output */
	while( IO0PIN & 0x00000010 );	/* Wait till response is HIGH */
	while( (IO0PIN & 0x00000010) == 0 );	/* Wait till response is LOW */
	while( IO0PIN & 0x00000010 );	/* Wait till response is HIGH */	/* This is end of response */
}

uint8_t dht11_data(void)
{
	int8_t count;
	uint8_t data = 0;
	for(count = 0; count<8 ; count++)	/* 8 bits of data */
	{
		while( (IO0PIN & 0x00000010) == 0 );	/* Wait till response is LOW */
		delay_us(30);	/* delay greater than 24 usec */
		if ( IO0PIN & 0x00000010 ) /* If response is HIGH, 1 is received */
			data = ( (data<<1) | 0x01 );
		else	/* If response is LOW, 0 is received */
			data = (data<<1);
		while( IO0PIN & 0x00000010 );	/* Wait till response is HIGH (happens if 1 is received) */
	}
	return data;
}

int main (void)
{
	uint8_t humidity_integer, humidity_decimal, temp_integer, temp_decimal, checksum; 
	char data[7]; 
	UART0_init();
	while(1)
	{
		dht11_request();
		dht11_response();
		humidity_integer = dht11_data();
		humidity_decimal = dht11_data();
		temp_integer = dht11_data();
		temp_decimal = dht11_data();
		checksum = dht11_data();
		if( (humidity_integer + humidity_decimal + temp_integer + temp_decimal) != checksum )
			UART0_SendString("Checksum Error\r\n");
		else
		{			
			UART0_SendString("Relative Humidity : ");
			memset(data, 0, 7);
			sprintf(data, "%d.", humidity_integer);
			UART0_SendString(data);
			memset(data, 0, 7);
			sprintf(data, "%d\r\n", humidity_decimal);
			UART0_SendString(data);
			UART0_SendString("Temperature : ");
			memset(data, 0, 7);
			sprintf(data, "%d.", temp_integer);
			UART0_SendString(data);
			memset(data, 0, 7);
			sprintf(data, "%d\r\n", temp_decimal);
			UART0_SendString(data);
			UART0_SendString("Checksum : ");
			memset(data, 0, 7);
			sprintf(data, "%d\r\n", checksum);
			UART0_SendString(data);			
			delay_ms(1000);
		}
	}
}

 

Video

 


Components Used

ARM7 LPC2148
ARM7 LPC2148
1
DHT11
DHT11 is a single wire digital humidity and temperature sensor, which gives relative humidity in percentage and temperature in degree Celsius.
1
CP2103 USB TO UART BRIDGE
CP2103 is single chip USB to UART Bridge. It supports USB 2.0 protocol.
1
Breadboard
Breadboard
1

Downloads

DHT11_uVision_Project Download
Ad