Digital humidity sensor using PIC microcontroller

Digital humidity sensor with LCD display using PIC microcontroller

Digital humidity sensor with LCD display is used to measure the relative percentage of water vapors in air. HS1101 capacitive humidity sensor is interfaced with PIC16F877A microcontroller to measure humidity and LCD is used to display percentage humidity in the air. For humans, there is a certain limit for water vapors presence in the air. Above that limit relative humidity may cause problems to human health.

Applications of digital humidity sensors

Humidity sensors have much application in industry and domestic areas. Humidity means the presence of water vapors in air. Percentage of water vapors in the air should be within safety limit. Otherwise, It has harmful physical and chemical effects on human beings and also in industrial products. Humidity sensors have major applications in agriculture, chemical, oil, gas, and medical industry. For example in agriculture industry humidity sensor is used to measure moisture in fields. There are also many other application of humidity sensor. You can search for them on Google.

digital Humidity sensor selection

When you search on Google, you will come across many humidity sensors. All these humidity sensors have their advantage and disadvantage. But I used capacitive HS1101 humidity sensor in this project. What is meant by capacitive humidity sensor? All capacitive sensors give output in capacitive form. They change their capacitance with respect to change in sensing parameter like in HS1101 sensor sensing parameter is the number of water vapors in air. The reason why I used this humidity sensor?  Because

  • It can be used for highly sensitive applications.
  • less cost
  • easy to interface with a microcontroller with small extra circuitry
  • No calibration is required
  • It can be easily used for home appliances and industrial control system.

How to use HS1101 digital humidity sensor

HS1101 is a  capacitive humidity sensor, so it can be used with a 555 timer circuit to generate a square wave of different frequency. I assume you know about the 555 timer IC and its use.

humidity sensor circuit with external circuitry
humidity sensor circuit with external circuitry

As shown in the above figure, a variable capacitor is used in place of humidity sensor for simulation purpose. Because the HS1101 simulation model is not available in Proteus. Above circuit is used as a signal conditioning circuit to convert one form of a parameter to its other proportional parameter so that it can be easily interfaced with any digital system or microcontroller. It is not possible for any microcontroller to read the change in capacitance directly. That’s why the above circuit is used to convert changing capacitance of HS1101 into square wave whose frequency changed according to change in capacitance.

I have already posted a tutorial on how to measure square wave using PIC 16F877A microcontroller. I recommend you to go through the article before going further in this project.

Now you know we can easily measure square wave frequency with the microcontroller. But now the question is how we will convert frequency back into relative humidity?  There is a proper relationship between humidity and frequency. Go through data sheet of HS1101 humidity sensor to know more about HS1101 sensor.

components list for digital humidity sensor

Category,Reference,Value,Order Code
Resistors,"R1",1732k,
Resistors,"R2",549k,
Resistors,"R3",1k,
Resistors,"R4",49.9k,
Resistors,"R44",10k,
Integrated Circuits,"U1",NE555,
Integrated Circuits,"U2",PIC16F877A,
Miscellaneous,"LCD1",LM016L,
Miscellaneous,"SENSOR",humidity sensor HS1101,
crystal 20mhz

Circuit diagram of a digital humidity sensor

Complete circuit diagram of Digital humidity sensor with LCD display:

circuit diagram of digital humidity sensor
circuit diagram of digital humidity sensor

Simulation results using pulse generator in proteus:

simulation

 

As shown in above diagram humidity is shown in percentage. 27% humidity mean air contains 27% water vapors. so you can take control actions according to your requirement. By little bit modification in this project you can also control anything according to change in relative humidity. let me know what is your requirement I will be happy to provide you solution.

Video lecture digital humidity meter

Code of  digital humidity sensor measurement using pic

Code is written in mikro c for pic:

//***************microcontrollerslabhub@gmail.com***********
// LCD module connections
sbit LCD_D7 at RB2_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_RS at RB7_bit;

sbit LCD_D7_Direction at TRISB2_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_RS_Direction at TRISB7_bit;
// End LCD module connections
//******************************frequency meter variables*******
int value,freq,humidity;
char humid[7];
//**************************************************************
void data_converstion(void)
{
IntToStr(humidity, humid);
}
void display1(void)
{

lcd_out(1,1,"Humidity = ");
lcd_out(1,13, Ltrim(humid));
Lcd_Chr_Cp('%');
Lcd_Chr_Cp(' ');

}

void interrupt(void) // high portD
{
if(T1CON.TMR1ON==0)
{
T1CON.TMR1ON=1; // turn on the timer1
INTCON.INTF = 0; // clear the interrupt flag
}
else if(T1CON.TMR1ON==1)
{
T1CON.TMR1ON=0; // turn off the timer1
value=(TMR1H<<8)|(TMR1L);
INTCON.INTE = 0; //Enable RB0/INT external Interrupt
freq=(5018035/value);
humidity = 565 - freq/13;
// freq = freq /13.18;
//freq = 100 - freq;
TMR1H=0;
TMR1L=0;
INTCON.INTE = 1; //Enable RB0/INT external Interrupt
INTCON.INTF = 0; // clear the interrupt flag
//freq=0;
}
}

void main()
{
long count;

count=0;
TMR1H=0;
TMR1L=0; // intialization of timer one prescalar and internal clock
INTCON.GIE = 1; //Enable Global Interrupt
INTCON.INTE = 1; //Enable RB0/INT external Interrupt
OPTION_REG.INTEDG = 0; //Interrupt on rising edge
ADC_Init();
Lcd_Init(); // Initialize LCD
freq=0;
Lcd_Cmd(_LCD_CLEAR); // Clear display
lcd_cmd(_LCD_CURSOR_OFF);
lcd_out(1,4,"GREEN HOUSE");
lcd_out(2,6,"SYSTEM");
delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR); // Clear display

while(1)
{

data_converstion();
display1();
}
}

If you have any problem after reading this article, comment on this post with your problem and if you need complete circuit diagram, simulation and hex file of this project, comment on this post with your email address.

If you feel this article helps you in getting some knowledge kindly don’t forget to share it with your friends on social media.

enjoy coding 🙂

56 thoughts on “Digital humidity sensor using PIC microcontroller”

  1. I have used the formula which is given in HS1101 humidity sensor data sheet. Formula is used to convert humidity % into frequency and vice versa

    Reply
  2. Hi!
    Bilal I need your help can you help me to make codes for count down timer, temperature and humidity in one micro-controller I tried to do that but there is interference one not working when all reads at the same time please you can email me the hex file for that.

    Thanks and kind regards
    Ally

    Reply
    • Hi!
      I downloaded the code from Bilal Project for humidity sensor but is not working is not giving me 27%
      Please can any one give me the formula for calculating frequency in this circuit.
      and Bilal can you tell me how you get 27%?

      Thanks
      Ally

      Reply
    • Hi Reeza!
      Sorry I generate codes from different projects and I build it in one micro controller so please if you know to separate codes help me.
      I need to make Incubator to control temperature,humidity and turning of the eggs automatic so if you any one can help me here the codes I will be thankfully to him.

      Regards
      Ally

      Reply
  3. Assalamoalikum
    brother what is the capacitence of hs1101 sensor at 0% humidity and at 100% humidity??
    and what is the maximum frequency generated by 555 at 100% humidity??

    Reply
  4. Hello! Mr. Bilal
    I need to make an Incubator to control temperature,humidity and turning of the eggs automatically using PIC16F877A.I tiered to write the correct code and also i used the code you attached, but it does not work .so if you are pleasured to help me; please email me complete code specially for humidity. another thing in which Port can i connect the out put of the timer?

    Reply
    • hello.. i am making the same incubator as you but i dont know where to start it.. can you help me with the components???

      Reply
  5. Dear BILAL,
    Well done for another beautiful article. Indeed I have benefited a lot from your articles. I am still learning the ropes in this field.
    I have a question. I do not know how you came about the constant 5018035 in this formula:
    Freq=5018035/value.
    Could you please shed more light on this aspect of your code?
    Thanks.
    Yusuf

    Reply
  6. Hi Bilal,
    I want to know how you get this value in the program?
    “Freq=5018035/value”
    Waiting for an reply from you.
    Thank you.

    Reply
  7. hi bilal , i want to make television circuit, but first i need to study in detail each part ; so can you help me or can you give suggestion please. I will have thesis report in this year on master level . my email is: kuk12980@gmail .com

    Reply
  8. i have a code for power measurment using pic16f887 mikroc,,
    but there on lcd showning values are in ,as= 220.34578 v..
    i want to measure vaalue as =220v

    Reply
  9. Connect the Humidity sensor to any anolog pin of PIC microcontroller , if the Humidity sensor reaches a voltage level
    >=2.5V , Shift 8 LED right to left connectrd to PORTB.

    Reply

Leave a Comment