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.
The
datasheet indicates that the output pins need some extra devices with it. here is the connection diagram:
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:
Here is the code I did:
Code C - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
| /*******************************************************************************
* 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 ****************************
*******************************************************************************/ |
Hope this will help you. If you need further information you can visit my personal blog: **broken link removed**
And facebook page:
www.facebook.com/mlabsbd. Thank you all.