Anda di halaman 1dari 12

MICROCONTOLLER LAB

PROJECT
HUMIDITY SENSOR USING 8051

Submitted by: Kunal Bansal (ue135044)


Lakhwinder Pal (ue135045)
Neeraj Garg (ue135054)
BE ECE Section 1

This project is about a simple humidity sensor based on 8051 microcontroller. Humidity sensor is also
called hygrometer. This circuit can sense relative humidity (RH) from 20% to 95% at an accuracy of 5%.
The humidity information is displayed on a 162 LCD display. A relay is also provided which is set to be
active when the humidity crosses a certain trip point. The circuit is mains operated and it is very easy to
install. DHT11 is the humidity sensor used here. The details and working of the DHT11 humidity sensor
is given below.
DHT11 HUMIDITY SENSOR
DHT11 is a low cost humidity cum temperature sensor which has a digital output. Capacitive method is
used for sensing the humidity and a thermistor is used for measuring the temperature. The sensor can
sense relative humidity from 20% to 95% at a resolution of 5%. Temperature measurement is up to 50C
at a resolution of 2C. The communication with the microcontroller is through a single wire. The basic
communication scheme is given in the image below.

The to and fro communication with DHT11 sensor is very easy. Pin 2 of the DHT11 is connected to the
port pin of the microcontroller. The connection scheme is shown in the image below. The data pin (pin2)
of the DHT11 requires an external 10K pull-up resistor.
The communication protocol is explained as follows. The MCU (microcontroller unit) first sends a low
signal of width 18mS to the DHT11. After this signal, the MCU pulls up the communication line and
waits for the response from DHT11. It makes take up to 2 to 40uS. Then the DHT11 pulls down the
communication line and keeps it low for 80uS. Then DHT11 pulls up the line and keeps it high for 80uS.
Then the DHT pulls down the line for 50uS and the next high pulse will be the first bit of the data. The
data is sent in bursts of 8 bits. Each high pulse of the burst indicates a data signal. The 50uS low signals
between the data bits are just spacers. The logic of the data bit is identified by measuring the width of it.
A 26 to 28uS wide pulse indicates a LOW and 70uS wide pulse indicates a HIGH. In simple words,
a pulse narrower than 50uS can be taken as a LOW and wider than 50us can be taken as a HIGH. The
first 8 bits of the data burst represents the integral value of the relative humidity, second 8 bits represent
the decimal value of the relative humidity, third 8 bits represent the integral value of the temperature data,
and the last 8 bits represent the decimal value of the temperature data, For DHT11 the decimal values are
always zero and we are measuring the relative humidity only in this project. So we need to just concern
about the first 8 bits of data that is the integral part of the relative humidity data.

CIRCUIT DIAGRAM

The humidity sensor DHT11 is connected to P3.5 of the 8051 microcontroller. R8 pulls up the
communication line between DHT11 and 8051. The relay is driven using P2.0 of the microcontroller.
Transistor Q1 switches the relay. R0 is a pull up resistor and R7 limits the base current of Q1. D5 is just a
free-wheeling diode. Data lines of the LCD display is interfaced to Port 0 of the microcontroller. Control
lines RS, R/E and E are connected to P2.7, P2.6 and P2.5 pins of the microcontroller respectively. R4 sets
the contrast of the display. R5 limits the current through the back light LED. C9 is a by-pass capacitor.
C8, C10 and X1 are associated with the clock circuitry. C11, R6 and S2 form the reset circuit.
PROGRAM
RS EQU P2.7
RW EQU P2.6
E EQU P2.5
ORG 000H
MOV DPTR,#LUT

//move the contents of the first LUT entry into DPTR

SETB P3.5

//set pin P3.5 as output pin

CLR P2.0

//clear the first bit of the output port

MOV TMOD,#00100001B

//timer 1 in mode 2, timer 0 in mode 0

MOV TL1,#00D

//value 0 in TL1

ACALL DINT
ACALL TEXT1
MAIN: MOV R1,#8D

//counter set to 8 bits for a byte

SETB P3.5

//initially pin high

CLR P3.5

//first low signal for 18 ms

ACALL DELAY1

//18 ms delay subroutine

SETB P3.5

//pull up by microcontroller

HERE:JB P3.5,HERE

// microcontroller waits for low signal from the DHT11 for 80us

HERE1:JNB P3.5,HERE1

// microcontroller waits for high signal from the DHT11 for 80us

HERE2:JB P3.5,HERE2

//the last low signal after which transmission begins

LOOP:JNB P3.5,LOOP
RL A

//rotate accumulator left one position

MOV R0,A

//create a copy A in R0

SETB TR1

//start timer 1

HERE4:JB P3.5,HERE4

//wait until signal from DHT11 goes low

CLR TR1

//stop timer 1

MOV A,TL1

//move the contents of TL1 to the accumulator

SUBB A,#50D

//subtract 50 from contents of TL1

MOV A,R0

//move content of R0 to A

JB PSW.7, NEXT

//jump to NEXT if the carry flag is set

SETB ACC.0

//set the first bit of accumulator if carry flag in not set

SJMP ESC

//unconditional jump to ESC

NEXT:CLR ACC.0

//clear the first bit of the accumulator

ESC: MOV TL1,#00D

//reload the contents of TL1 with 0

CLR PSW.7

//clear the carry flag for the next operation

DJNZ R1,LOOP

//decrement the contents of R1, jump to LOOP if R1 not 0

ACALL DINT

//initialize LCD

ACALL TEXT1

//display hygrometer

ACALL LINE2

//move to line 2 of LCD

ACALL TEXT2

//display humidity reading

ACALL HMDTY
ACALL CHECK

//compare reading with 65

ACALL DELAY2
LJMP MAIN

DELAY1: MOV TH0,#0B9H

//repeat process

//delay subroutine for 18ms

MOV TL0,#0B0H
SETB TR0
HERE5: JNB TF0,HERE5
CLR TR0
CLR TF0
RET
DELAY2:MOV R1,#112D
BACK:ACALL DELAY1
DJNZ R1,BACK

//second delay subroutine of 2s

RET

CHECK:MOV A,R0
MOV B,#65D

//subroutine to compare value of humidity to 65


//and turn on LED if it higher than 65%

SUBB A,B
JB PSW.7,NEXT1
ACALL TEXT3

//display message ON

SETB P2.0
SJMP ESC1
NEXT1:ACALL TEXT4

//display message OFF

CLR P2.0
ESC1:CLR PSW.7
RET

CMD: MOV P0,A

//command subroutine to initialize the LCD

CLR RS
CLR RW
SETB E
CLR E
ACALL DELAY
RET
DISPLAY:MOV P0,A

//display subroutine to display various characters on LCD

SETB RS
CLR RW
SETB E
CLR E
ACALL DELAY
RET

HMDTY:MOV A,R0
MOV B,#10D
DIV AB
MOV R2,B
MOV B,#10D
DIV AB
ACALL ASCII
ACALL DISPLAY
MOV A,B
ACALL ASCII
ACALL DISPLAY
MOV A,R2

//subroutine to display the value of humidity by sending the ASCII


//code of the 8 bit value in the accumulator

ACALL ASCII
ACALL DISPLAY
MOV A,#"%"
ACALL DISPLAY
RET

TEXT1: MOV A,#"H"


ACALL DISPLAY

//subroutine to display hygrometer on the first line of the


//LCD

MOV A,#"y"
ACALL DISPLAY
MOV A,#"g"
ACALL DISPLAY
MOV A,#"r"
ACALL DISPLAY
MOV A,#"o"
ACALL DISPLAY
MOV A,#"m"
ACALL DISPLAY
MOV A,#"e"
ACALL DISPLAY
MOV A,#"t"
ACALL DISPLAY
MOV A,#"e"
ACALL DISPLAY
MOV A,#"r"
ACALL DISPLAY
RET

TEXT2: MOV A,#"R"

//subroutine to display tag for the humidity value

ACALL DISPLAY
MOV A,#"H"
ACALL DISPLAY
MOV A,#" "
ACALL DISPLAY
MOV A,#"="
ACALL DISPLAY
MOV A,#" "
ACALL DISPLAY
RET
TEXT3: MOV A,#" "

//subroutine to display message ON

ACALL DISPLAY
MOV A,#" "
ACALL DISPLAY
MOV A,#"O"
ACALL DISPLAY
MOV A,#"N"
ACALL DISPLAY
RET

TEXT4:MOV A,#" "

//subroutine to display message OFF

ACALL DISPLAY
MOV A,#"O"
ACALL DISPLAY
MOV A,#"F"
ACALL DISPLAY
MOV A,#"F"
ACALL DISPLAY
RET

DINT:MOV A,#0CH

//subroutine to initialize the LCD

ACALL CMD
MOV A,#01H

//clear screen

ACALL CMD
MOV A,#06H

//shift cursor right

ACALL CMD
MOV A,#83H

//move cursor to 4th location on the first row

ACALL CMD
MOV A,#3CH

//display on, cursor off

ACALL CMD
RET

LINE2:MOV A,#0C0H

//transfer cursor to first position of second row

ACALL CMD
RET

DELAY: CLR E
CLR RS
SETB RW
MOV P0,#0FFH
SETB E
MOV A,P0

//busy flag subroutine to check if the LCD has finished


// processing last instruction or not

JB ACC.7,DELAY
CLR E
CLR RW
RET
ASCII: MOVC A,@A+DPTR

//use lookup table to obtain value of ASCII code

RET

LUT: DB 48D
DB 49D
DB 50D
DB 51D
DB 52D
DB 53D
DB 54D
DB 55D
DB 56D
DB 57D
END

ABOUT THE PROGRAM


The communication protocol with the sensor and the microcontroller is already explained. Data out pin of
the DHT11 is connected to P3.5 of the microcontroller. At the start of the MAIN loop, P3.5 is held high.
Then it is made low and an 18mS delay routine (DELAY1) is called. Then P3.5 is made high. This forms
the first 18mS wide start signal for the DHT11. Now the communication line is high and the
microcontroller polls the status of this line and waits there until it is low. It becomes low when the
DHT11 sends back the response signal. Then the microcontroller waits for the second response signal
which is a high signal. When this high signal is received the microcontroller waits for the next low signal
and after this low signal, data transmission starts.
Each data bit is represented by a high signal and the width of it determines the logic. So whenever the
microcontroller receives a data pulse, Timer1 is started and the program waits there until the data pulse
vanishes. Then the timer is stopped. Now the TL1 i.e. Timer1 low register contains the width of the data
pulse. Then the width is compared with 50 by subtracting 50 from the TL1 count. If carry flag (PSW.7) is
set it means that the width is less than 50uS and if carry flag is not set it means that the width is higher
than 50uS.
If width is less than 50uS it indicates a low signal and ACC.0 is cleared. If the width is higher than 50uS
it indicates a high signal and ACC.0 is set. Then TL1 register and PSW.7 bit are cleared. Then the LOOP
is iterated 8 times and accumulator is rotated left during the beginning of each iteration. The rotate left
(RL) instruction is used because during each iteration you update ACC.0 only and you have to save that
bit before the next update. This is achieved by shifting it to left each time.
The last update of ACC.0 does not require a position shift because it is in the right place it is supposed to
be. That is the reason behind placing the RL A instruction at the beginning of the LOOP. After the 8th
iteration the LOOP is exited without rotating the accumulator. Now the content of the accumulator is
equal to the integral value of the current relative humidity in percentage.

Then subroutine DINT is called for initializing the display. Then subroutine TEXT1 is called which
displays Hygrometer. Then subroutine LINE2 is called for shifting the cursor to 2nd line. Then
subroutine HMDTY is called for displaying the humidity value in percentage. Then subroutine CHECK is
called for checking whether the humidity value is above or below 65%. If humidity is above 65%, relay is
activated and else the relay is deactivated. The 2s delay subroutine DELAY2 is called. 2 second delay is
given because you can take readings from the DHT11 once in every 2 seconds only. This also makes the
display stable. Then the program jumps to MAIN label and the entire process is repeated.

LIST OF COMPONENTS

COMPONENT

SPECIFICATION

QUANTITY

General Purpose Printed Circuit Board

DHT11 (Humidity and Temperature sensor)

JHD 162A (LCD Display)


LED
A103J
DC supply
DC Voltage Converter 7508
Resistances

16 x 2

Red

10K pull-up resistor network

9V

9V to 5V

330, 4.7K, 10K

1 each

Connecting Wires

According to requirement

Female Headers

According to requirement

LIST OF COMPONENTS

DHT11 humidity and temperature sensor

10K pull-up resistor network

9V DC Battery

JHD 162A LCD

9V to 5V DC Converter

Resistance

Female Connector

Red LED

FLOWCHART

Anda mungkin juga menyukai