Humidity and Temperature Measurement with PIC16F877A using HSM-20G

HSM-20G is one of the most common humidity and Temperature sensor. This sensor is fully analog type. The outputs are analog voltages rather than any digital signal.
fol9yqugeg4mna1-medium.jpg


The datasheet indicates that the output pins need some extra devices with it. here is the connection diagram:
HSM-20G5.jpg



From the Datasheet we can see that the output is not linear but it is indicating a table. The table shows us the different voltages for different Humidity or Temperature. If you use this table to find the equation based on this table, you will find that,

Humidity = 3.71 X voltage^3 – 20.65 voltage^2 + 64.81voltage – 27.44;

and Temperature = 5.26 voltage^3 – 27.34 voltage^2 + 68.87 * voltage – 17.81;

Here ‘Voltage’ is the PIN voltage. So we know the equation. And the connection Diagram of HSM-20G with PIC will be:

connection-diagram1.png


Here is the code I did:

Code:
/*******************************************************************************
*                 Program For "HSM-20G Reading for Humidity and Temp."         *
*                    Program Written By_Engr. Mithun K. Das                    *
*                       MCU: PIC16F877A; X-Tal: 8MHz(Ex.)                      *
*                           Email: [email]mithun060@gmail.com[/email]                         *
*                               Call: 01722448270                              *
*                                 Date:06-08-14                                *
*******************************************************************************/
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

unsigned int Humidity = 0;
unsigned int Temperature = 0;
long adc_rd = 0;

void Get_Humidity(void);
void Get_Temperature(void);

void main()
{
TRISA = 0xFF;//all input
ADCON1 = 0x00;//all analog

// LCD Initialization...
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);//clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF);//Cusros Off
Lcd_Out(1,1,"HSM-20G Hum_Tmp.");
while(1)
{
Get_Humidity(void);
Get_Temperature(void);
}//while...(1)
}//void main

/*******************************************************************************
**********************         Get_Humidity         ****************************
*******************************************************************************/
void Get_Humidity(void)
{
char hum[] = "RH:  %";
int ii;
float voltage = 00.00;
ADCON0 = 0b00010001;// Select Channel 2
adc_rd = 0; //Clear Previous Data
for(ii=0;ii<20;ii++)
{
adc_rd += ADC_Read(2);  //Take sample and add
}
adc_rd /= 20;// Get the average data
voltage = adc_rd*0.004883;// 5/1023
Humidity = (int)((3.71 * voltage * voltage * voltage) - (20.65 * voltage * voltage) + (64.81 * voltage) - 27.44);
hum[3] = Humidity/10 + 48;
hum[4] = Humidity%10 +48;
Lcd_Out(2,1,hum);
}
/*******************************************************************************
**********************         Get_Temperature        **************************
*******************************************************************************/
void Get_Temperature(void)
{
char Tmp[] = "Temp:   C";
int jj;
float voltage = 00.00;
ADCON0 = 0b00011001;// Select Channel 3
adc_rd = 0; //Clear Previous Data
for(jj=0;jj<20;jj++)
{
adc_rd += ADC_Read(3);  //Take sample and add
}
adc_rd /= 20;// Get the average data
voltage = adc_rd*0.004883;// 5/1023
Temperature = (int)((5.26 * voltage * voltage * voltage) - (27.34 * voltage * voltage) + (68.87 * voltage) - 17.81);
Tmp[5] = Temperature /10 + 48;
Tmp[6] = Temperature %10 +48;
Tmp[7] = 223;
Lcd_Out(2,8,Tmp);
}

/*******************************************************************************
**********************       End Of The Program     ****************************
*******************************************************************************/

And the result:




Hope this will help you. If you need further information you can visit my personal blog:https://labprojectsbd.com/

Comments

There are no comments to display.