Sei sulla pagina 1di 41

Humidity sensor using 8051 (Hygrometer)

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 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

DELAY1: MOV TH0,#0B9H


MOV TL0,#0B0H
SETB TR0
HERE5: JNB TF0,HERE5
CLR TR0
CLR TF0
RET

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

CMD: MOV P0,A


CLR RS

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

TEXT1: MOV A,#"H"


ACALL DISPLAY
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"


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

MOV A,#"N"
ACALL DISPLAY
RET

TEXT4:MOV A,#" "


ACALL DISPLAY
MOV A,#"O"
ACALL DISPLAY
MOV A,#"F"
ACALL DISPLAY
MOV A,#"F"
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

ASCII: MOVC A,@A+DPTR


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.
I have already mentioned that, each data bit is represented by a high signal and the width of it
determines the logic. So when ever 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 ie
Timer1 low register contains the width of the data pulse. Then the width is compared with 50 by
substracting 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 begining of each iteration.
The rotate left (RL) instruction is used beacause during each iteration you updates 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 doesnot 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 begining of the LOOP.
After the 8 th 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 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.

Microcontroller Based Programmable Data-cumTemperature Display.


LCD display are most widely used in applications like token display machine in malls, supermarkets,
restaurants, cafe-houses etc. The project presented here demonstrates the working through wireless
by use of Bluetooth technique. This circuit is designed by Mr. Vivek Kumar Kamboj from Haryana.

Circuit and working.


This circuit uses microcontroller that accepts data via Bluetooth from various types of gadgets like
android based smartphones, laptop, etc. The IC1 AT89C51 is the heart of this programmable display
whose circuit is shown in the figure below.

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

Second it can be used as a digital temperature display


For the application of data display, the Bluetooth module used in the circuit that acts as a transmitter
and receiver which is compatible with the smartphones , PCs , laptops etc. An 11.0592MHz crystal is
connected to the crystal input pins 18 and 19 of the IC1 to provide an internal clock for operation.
Port pin 1.0 to 1.7 of the IC1 are used to drive the LCD 16X2.The message typed by the user on the
android based smartphone or window based laptop is received by the port pin P3.0/RXD of IC1 via
Bluetooth module. In this manner data communication takes place between the user and
microcontroller.
To display the temperature, the IC3 LM35 is used as a temperature sensor. It is a precision
temperature sensor, whose output voltage is linearly proportional to the Celsius temperature with +
10.0 mV/C scale factor. The pin 2 of LM35 is connected to the port pin IN0 of IC2 ADC0809.
The IC2 ADC0809 is a data acquisition device which has an 8-bit analog -to- digital converter that
uses successive approximation as the conversion technique. The analog data converted by ADC in
digital form at the output port pins OUT1 to OUT8 is connected to the port pins P0.0 to P0.7 of IC1.
The IC1 drives the LCD 16X2 which results in display of temperature.

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);
}
}

Operation and testing.


The operation of the circuit is as explained below:
1- Operation with LM35 to display the temperature :
When the circuit is switched ON, initially the LCD shows the temperature sensed by IC3 LM35. The
user can reset the operation.
2- Operation with Bluetooth module :
To display the data using android based smartphone, the user has to download android based
Bluetooth terminal application from playstore. The user can type the desired message on their
smartphone in this application after connecting it with the circuit via Bluetooth. The desired message
would be displayed on the LCD.
In similar way to display data using laptop, the user is required to download the Hyper terminal
software application on their laptops having windows as operating system. The prototype is shown
below:

PARTS LIST
Semiconductors
IC1

AT89C51 Microcontroller

IC2

ADC0809 Analog to Digital Convertor

IC3

LM35 Temperature Sensor

IC4

7805 Voltage Regulator

LCD

16X2 Alpha Numeric Display

Bluetooth Module

Bluetooth Module

D1-D4

1N4007 Diode

Resistors
R1

10k, 0.5 W

R2

1k, 0.5 W

Capacitors
C1

33pF Ceramic disk

C2

33pF Ceramic disk

C3

10uF, 6.3V Electrolytic

C4

1000uF, 25V Electrolytic

Miscellaneous
X1

11.0592 MHz Crystal Oscillator

Push Button

Push Button

TR1

12V/300mA Transformer

/////////////

New code for humidity sensor /////////////////////////////////////////

// Program to Interface Humidity sensor with 8051 Microcontroller


#include<reg51.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;
void delay(unsigned char count)
{
unsigned char i;
unsigned int j;
for(i=0;i<count;i++)
for(j=0;j<1275;j++);
}
void lcd_command(unsigned char comm)
{
lcd_data_pin=comm;
en=1;
rs=0;
rw=0;
delay(1);
en=0;
}
void lcd_data(unsigned char disp)
{
lcd_data_pin=disp;
en=1;
rs=1;
rw=0;
delay(1);
en=0;
}
lcd_dataa(unsigned char *disp)

{
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

percentage relative humidity. The connections of ADC0804 and

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;

void delay(unsigned char count)


{
unsigned char i;
unsigned int j;
for(i=0;i<count;i++)
for(j=0;j<1275;j++);
}

void lcd_command(unsigned char comm)


{
lcd_data_pin=comm;
en=1;

rs=0;
rw=0;
delay(1);
en=0;
}

void lcd_data(unsigned char disp)


{
lcd_data_pin=disp;
en=1;
rs=1;
rw=0;
delay(1);
en=0;
}

lcd_dataa(unsigned char *disp)


{
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 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();
}
} */

// This program is written in C language. It is compiled, simulated and


debugged using KEIL IDE. Here is the program
//#include<reg51.h >

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()

// lcd busy function

{
int x;
for(x=0;x<1500;x++);
}

void writecmd(unsigned char a)

// send command byte to LCD

{
busy();
rs = 0;
P0 = a;
en = 1;
en = 0;
}

void writedata(unsigned char b)

// send data byte to LCD

{
busy();
rs = 1;
P0 = b;
en = 1;
en = 0;
}

void writestr(unsigned char *s)


{
unsigned char l,i;
l = strlen(s);
for(i=0;i<l;i++)
{
writedata(*s);
s++;

// write string / message to LCD

}
}

void display(unsigned char z)


{

// convert HEX to decimal and

//decimal to ASCII for display


unsigned int tmp1,tmp2,t,t1,a;
unsigned char asci[3];
tmp1 = (z & 0x0F);
tmp2 = z >> 4;
tmp2 = tmp2*16;
t = tmp1+tmp2;
if(t>=100)
{
a=2;
while(t>=10)
{
t1=t%10;
asci[a]=t1+0x30;
t=t/10;
a--;
}
asci[0]=t+0x30;
}
else
{

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;

//unsigned int c1=0;


P0=0x00;
P1=0xFF;
P2=0x00;

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);

// wait for end of conversion

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();
}
}

// delay for 2 second

Potrebbero piacerti anche