Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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 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 micrcontroller. 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 make 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 send 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, an 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 of the humidity sensor is shown in the
image below.
Circuit diagram.
The humidity sensor DHT11 is connected to P3.1 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 forms
the reset circuit.
Program.
RS EQU P2.7
RW EQU P2.6
E EQU P2.5
ORG 000H
MOV DPTR,#LUT
SETB P3.5
CLR P2.0
MOV TMOD,#00100001B
MOV TL1,#00D
ACALL DINT
ACALL TEXT1
MAIN: MOV R1,#8D
SETB P3.5
CLR P3.5
ACALL DELAY1
SETB P3.5
HERE:JB P3.5,HERE
HERE1:JNB P3.5,HERE1
HERE2:JB P3.5,HERE2
LOOP:JNB P3.5,LOOP
RL A
MOV R0,A
SETB TR1
HERE4:JB P3.5,HERE4
CLR TR1
MOV A,TL1
SUBB A,#50D
MOV A,R0
JB PSW.7, NEXT
SETB ACC.0
SJMP ESC
NEXT:CLR ACC.0
ESC: MOV TL1,#00D
CLR PSW.7
DJNZ R1,LOOP
ACALL DINT
ACALL TEXT1
ACALL LINE2
ACALL TEXT2
ACALL HMDTY
ACALL CHECK
ACALL DELAY2
LJMP MAIN
DELAY2:MOV R1,#112D
BACK:ACALL DELAY1
DJNZ R1,BACK
RET
CHECK:MOV A,R0
MOV B,#65D
SUBB A,B
JB PSW.7,NEXT1
ACALL TEXT3
SETB P2.0
SJMP ESC1
NEXT1:ACALL TEXT4
CLR P2.0
ESC1:CLR PSW.7
RET
CLR RW
SETB E
CLR E
ACALL DELAY
RET
DISPLAY:MOV P0,A
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
ACALL ASCII
ACALL DISPLAY
MOV A,#"%"
ACALL DISPLAY
RET
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
MOV A,#"N"
ACALL DISPLAY
RET
DINT:MOV A,#0CH
ACALL CMD
MOV A,#01H
ACALL CMD
MOV A,#06H
ACALL CMD
MOV A,#83H
ACALL CMD
MOV A,#3CH
ACALL CMD
RET
LINE2:MOV A,#0C0H
ACALL CMD
RET
DELAY: CLR E
CLR RS
SETB RW
MOV P0,#0FFH
SETB E
MOV A,P0
JB ACC.7,DELAY
CLR E
CLR RW
RET
LUT: DB 48D
DB 49D
DB 50D
DB 51D
DB 52D
DB 53D
DB 54D
DB 55D
DB 56D
DB 57D
END
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 the 2S delay subroutine DELAY2 is called. 2
second delay is given because you can take radings 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.
The mains power supply is applied to the primary winding of transformer TR1 and is stepped down to
12V AC , 300mA by the transformer. The bridge rectifier comprising diodes D1 through D4 rectifies
the AC voltage. The linear voltage regulator IC 7805 is used to provide 5V regulated DC voltage to
drive the circuit including Microcontroller , ADC and LCD display. The circuit has two applications :
First it can be used as data display by using Bluetooth technique
Software program.
The circuit operation is performed using the software program loaded into the internal memory of IC1
microcontroller AT89C51. The program implements all required functionalities including user inputs
through the smartphones, laptops etc by displaying messages on LCD. The program is written in
embedded C language and is given below for reference:
#include
sbit soc=P2^0;
sbit eoc=P2^1;
sbit oe=P2^2;
sbit clk=P2^3;
sbit ad_a=P2^4;
sbit ad_b=P2^5;
sbit ad_c=P2^6;
sbit ale=P2^7;
sbit rs=P3^6;
sbit e=P3^7;
void clock()
{
int v;
for(v=0;v<500;v++)
clk=~clk;
}
void delay()
{
int a;
for(a=0;a<300;a++);
}
void cd(unsigned char w)
{
P1=w;
rs=0;
e=1;
delay();
e=0;
}
void dd(unsigned char w)
{
P1=w;
rs=1;
e=1;
delay();
e=0;
}
void main()
{
unsigned char a,b,c=0,d;
cd(0x38);
cd(0x06);
cd(0x0c);
cd(0x01);
soc=0;
eoc=1;
oe=0;
ale=0;
SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TR1=1;
ad_a=0;
ad_b=0;
ad_c=0;
while(1)
{
if(RI==1)
{
if(c==16)
{
cd(0x01);
c=0;
}
cd(0x80+c);
d=SBUF;
dd(d);
c=c+1;
RI=0;
}
ale=1;
soc=1;
clock();
ale=0;
soc=0;
clock();
while(eoc==0);
oe=1;
b=P0;
cd(0xc1);
dd(T);
dd(e);
dd(m);
dd(p);
dd(-);
a=b*2.0;
dd((a/100)+0x30);
dd(((a/10)%10)+0x30);
dd((a%10)+0x30);
dd( );
dd(^);
dd(C);
}
}
PARTS LIST
Semiconductors
IC1
AT89C51 Microcontroller
IC2
IC3
IC4
LCD
Bluetooth Module
Bluetooth Module
D1-D4
1N4007 Diode
Resistors
R1
10k, 0.5 W
R2
1k, 0.5 W
Capacitors
C1
C2
C3
C4
Miscellaneous
X1
Push Button
Push Button
TR1
12V/300mA Transformer
/////////////
{
unsigned char x;
for(x=0;disp[x]!=0;x++)
{
lcd_data(disp[x]);
}
}
void lcd_ini()
{
lcd_command(0x38); // for using 8-bit 2 row LCD
delay(50);
lcd_command(0x0F);
// for display on cursor blinking
delay(50);
lcd_command(0x0C);
delay(50);
lcd_command(0x80);
delay(50);
}
void lcd_display(unsigned int val)
{
unsigned char flg=0;
lcd_command(0xC7);
if(val==0)
lcd_data('0');
while(val>0)
{
lcd=val%10;
val=val/10;
lcd_command(0xC7-flg);
lcd_data(lcd+'0');
flg++;
}
}
void display()
{
key++;
number=number+input_port;
if(key==11)
{
number=number/key;
number=number*10;
number=number/25;
humidity=number-3;
lcd_display(humidity);
key=0;
number=0;
}
}
void adc()
{
rd=1;
wr=0;
delay(2);
wr=1;
while(intr==1);
rd=0;
display();
delay(2);
intr=1;
}
void main()
{
lcd_ini();
lcd_dataa("%Rel.Humidity:");
while(1)
{
adc();
}
}
Physical quantities like Humidity, temperature, pressure etc. are monitored to get
information about the environmental conditions. Various sensors are being used to
measure these quantities in analog form. This article demonstrates the principle and
operation of interfacing the humidity sensor with 8051 microcontroller (AT89C51). The
humidity sensor is widely used in applications like weather forecast, air conditioner,
Hygrometers etc.Click to read more about humidity sensor and its working.
Humidity sensor works on the principle of relative humidity and gives the output in the
form of voltage. This analog voltage provides the information about the percentage
relative humidity present in the environment.
The relative humidity is defined as:
The analog output of sensor is connected to ADC to get its corresponding digital value.
For calibration of digital values, the reference voltage of ADC is set to 1.5 volts. The
digital values are received at port P0 of microcontroller. These digital values are used
to calculate percentage relative humidity of environment. The calculated data is sent to
LCD to display the
LCD with the 8051 microcontroller (AT89C51) are shown in circuit diagram.
Algorithm
{C. Define the input and output port of microcontroller.
{2.
Initialize LCD.
{C}. Monitor the value of ADC 0804.
{C}{ Calculate the relative humidity
{C5. Convert the calculated value in their corresponding ASCII codes.
6.
Display the converted value on the LCD.
{C}{7.
Repeat steps 4 to 7 in an infinite loop.
Combined program:
#include<reg51.h>
#include<string.h>
sfr lcd_data_pin=0x80;//p0 port
sbit rs=P3^0;
sbit rw=P3^1;
sbit en=P3^2;
sbit wr= P3^3;
sbit rd= P3^4;
sbit intr= P3^5;
sfr input_port=0x90; //p1 port
unsigned int number=0;
unsigned char humidity,lcd,key=0;
rs=0;
rw=0;
delay(1);
en=0;
}
void lcd_ini()
{
lcd_command(0x38); // for using 8-bit 2 row LCD
delay(50);
lcd_command(0x0F);
delay(50);
lcd_command(0x0C);
delay(50);
lcd_command(0x80);
delay(50);
}
void display1()
{
key++;
number=number+input_port;
if(key==11)
{
number=number/key;
number=number*10;
number=number/25;
humidity=number-3;
lcd_display(humidity);
key=0;
number=0;
}
}
void adc()
{
rd=1;
wr=0;
delay(2);
wr=1;
while(intr==1);
rd=0;
display1();
delay(2);
intr=1;
}
/*
void main()
{
lcd_ini();
lcd_dataa("%Rel.Humidity:");
while(1)
{
adc();
}
} */
sbit ale=P3^3;
sbit oe=P3^6;
sbit sc=P3^4;
sbit eoc=P3^5;
sbit a=P3^0;
sbit b=P3^1;
sbit c=P3^2;
//sbit rs=P2^7;
//sbit en=P2^6;
sbit led1=P2^0;
sbit led2=P2^1;
sbit led3=P2^2;
sbit led4=P2^3;
unsigned char data byt1,byt2;
unsigned int c1=0;
void delay1()
// 2 sec delay
{
int x,y;
for(x=0;x<40;x++)
for(y=0;y<10000;y++);
}
void busy()
{
int x;
for(x=0;x<1500;x++);
}
{
busy();
rs = 0;
P0 = a;
en = 1;
en = 0;
}
{
busy();
rs = 1;
P0 = b;
en = 1;
en = 0;
}
}
}
t1=t%10;
asci[2]=t1+0x30;
t=t/10;
asci[1]=t+0x30;
asci[0]=0x30;
}
writedata(asci[0]);
writedata(asci[1]);
writedata(asci[2]);
}
void main()
{
lcd_ini();
lcd_dataa("%Rel.Humidity:");
//
while(1)
//
{
adc();
//
}
//unsigned char data byt1,byt2;
P2=0x0F;
P3=0x20;
writecmd(0x3C);
writecmd(0x0E);
writecmd(0x01);
writestr(" Temperature");
writecmd(0xC3);
writestr("controller");
delay();
writecmd(0x01);
while(1)
{
c1++;
if(c1==1) {a=0;b=0;c=0;led1=0;led2=1;} // select ADC
else {a=1;b=0;c=0;led1=1;led2=0;} // channels alternatively
ale=1;
sc=1;
// start conversion
ale=0;
sc=0;
while(eoc==1);
while(eoc==0);
oe=1;
if(c1==1) byt1=P1 ;
else byt2=P1;
oe=0;
// enable output
// get digital value
if(c1==1)
{
writecmd(0x80);
writestr("set temp:");
// display set
display(byt1);
// temperature
}
else
{
writecmd(0xC0);
writestr("current temp:");
// display current
// temperature
display(byt2);
if(byt2<=byt1) {led3=0;led4=1;} // indicate error
else {led3=1;led4=0;}
// on LEDs
c1=0;
}
delay();
}
}