CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

HS1101 capacitance to humidity formula
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ömer Faruk



Joined: 15 Nov 2018
Posts: 42
Location: Çanakkale

View user's profile Send private message Send e-mail

HS1101 capacitance to humidity formula
PostPosted: Sun Sep 13, 2020 3:07 pm     Reply with quote

Hello Everybody,

I am trying to measure humidity with HS1101 sensor. I am using CTMU unit on pic18f46k22. I have calibrated the constant current supply as 6.1 uA. I am getting reasonable readings from the sensor. The results are in pF. For example, I get 180pF but I can't turn it into humidity. I don't want to use a look up table. Here is the formula to get humidity with capacitance readings from sensor. When I use this formula I don't get 55% humidity.

Could you hep me on this issue ? Could you please check if the formula is true ?

https://ibb.co/82nbMtP
PCM programmer



Joined: 06 Sep 2003
Posts: 21197

View user's profile Send private message

PostPosted: Sun Sep 13, 2020 3:30 pm     Reply with quote

Post your C source code that you wrote to implement that formula.

Or post the complete test program that you used. That would be better.
temtronic



Joined: 01 Jul 2010
Posts: 7627
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Sep 13, 2020 4:22 pm     Reply with quote

re:
Quote:
I don't want to use a look up table.

I have to ask why not ? PICs are not great at doing 'complicated' math and it'll take a LOT of time to compute the answer.
A lookup table, in this case just 100 entries will be very, very FAST... AND be very efficent in terms of code space (memory used). Doing FP math, even scaled integer math will take a LOT of memory space.
We need to see your code though.....we can't say 'why' you're not getting the correct answer until then.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 16338

View user's profile Send private message

PostPosted: Mon Sep 14, 2020 12:42 am     Reply with quote

You realise you will need the reading at 55RH, to put into the formula.
This needs to be calibrated for each sensor.
Ttelmah



Joined: 11 Mar 2010
Posts: 16338

View user's profile Send private message

PostPosted: Mon Sep 14, 2020 3:14 am     Reply with quote

The way it works is:

Have the capacitance reading at 55RH. Call this STD.

Then
X=current reading/STD.

RH=-3562.2*(X^3)+11037*(X^2)-10776*X+3356.5

It'd only give 55RH, if current reading == STD, which then makes X = 1

X=1
RH = -3562.2 + 11037 -10776 +3356.5 = 55.3

I've actually tweaked the parameters a little.
temtronic



Joined: 01 Jul 2010
Posts: 7627
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Sep 14, 2020 5:33 am     Reply with quote

I got curious about the sensor...got the datasheet.

There's 2 possible ways for 'bad' readings.

1) the 180pf@55RH is +-3pf ( spec is 177-183). So if your sensor was really 177pf, then the %RH calculations will be slightly off ( about 2% ?)

2) PCB layout/wiring can add 'stray capacitance' according to datasheet. This can be a huge problem. If PCB traces aren't perfect and clean then coated, any moisture in the air can affect the reading.

Also might need to read the power spec as well, in case a small drop in VDD might cause bad readings. VDD can drop when sa an LED is turned on..this might affect the sensor reading.
Ttelmah



Joined: 11 Mar 2010
Posts: 16338

View user's profile Send private message

PostPosted: Mon Sep 14, 2020 6:24 am     Reply with quote

No, not 2%.

This is the critical point. This value is massively critical. If you look at the
formula, and recalculate using the limit values, you get 45RH to 65RH.
So the +/-3% in capacitance value, gives +/-10%RH. This is why you
need to start with a 'known' figure for the capacitance at a particular RH
level, for the values to be useable....
temtronic



Joined: 01 Jul 2010
Posts: 7627
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Sep 14, 2020 7:52 am     Reply with quote

yikes.....I didn't 'do the math', just a 'gut' feeling the number WOULD be off.
Didn't realize it'd be that bad..... Crying or Very sad
Ttelmah



Joined: 11 Mar 2010
Posts: 16338

View user's profile Send private message

PostPosted: Mon Sep 14, 2020 8:00 am     Reply with quote

The other thing is that he has not actually said how he is measuring the
capacitance. All solutions have potential errors, and some could be really
bad. Their example circuit uses a 0.5% resistor and a trim pot, to allow
the value to be tweaked to read correctly.
Ömer Faruk



Joined: 15 Nov 2018
Posts: 42
Location: Çanakkale

View user's profile Send private message Send e-mail

PostPosted: Tue Sep 15, 2020 2:30 pm     Reply with quote

Code:
#include <18f46k22_mine.h>
#fuses HSH,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,PUT,NOCPD,NOWRT,NODEBUG,INTRC_IO,NOIESO,CCP2C1,CCP3E0,NOHFOFST,MCLR,STVREN,NOWRTB,NOEBTR,NOXINST,TIMER3C0
#device ADC=10

#use delay (clock=16000000)

#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)


#include <flexy_lcd.c>
#include <my_math.h>
#include <stdlib.h>

#define KESME 65535


#define DELAY    delay_us(100);


unsigned int counter=0;




float max ;
float min ;
float cap;

char yazi[8];

float nem;
float  humid;
float current;
float toplam2;

unsigned int16 digital;

#int_timer0
void  timer0_kesme(void)
{
set_timer0(6);
}






#INT_RB                                     
void detect_rb_change(void)
{
}

void nem_olc1()
{


min = 1024;
max = 0;
ADCON1 = 0x08;
ADON = 1;

CHS0 = 0;
CHS1 = 0;
CHS2 = 0;
CHS3 = 0;
CHS4 = 0;

delay_us(10);
CTMUEN = 1;
EDG1STAT = 0;
EDG2STAT = 0;

while(counter<18)
{



IDISSEN = 1;
DELAY
IDISSEN = 0;
                                             
EDG1STAT = 1;
DELAY
EDG1STAT = 0;

 GO_DONE = 1;
 while(GO_DONE);
 digital = make16(ADRESH,ADRESL);
 toplam2 = toplam2 + digital;

 
    if (digital>max)
        max=digital; //update max stored
    if (digital<min)
        min=digital; //update min stored
    if (++counter==18)
    {
     toplam2 = toplam2  -max - min;
    }
}
 counter=0;
 nem = toplam2/16 ;
 toplam2=0;
 CTMUEN = 0;


}

void main()
{

setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256 | RTCC_8_bit);
setup_timer_1(T1_INTERNAL  | T1_DIV_BY_8);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED);
setup_timer_5(T5_DISABLED);

setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);
setup_CCP3(CCP_OFF);
setup_CCP4(CCP_OFF);
setup_CCP5(CCP_OFF);
setup_SPI(SPI_SS_DISABLED);

set_tris_a(0x03); set_tris_b(0xFF);  set_tris_c(0x01); set_tris_d(0x00); set_tris_e(0x00);
ANSELA=0x03;
ANSELB=0x02;
port_b_pullups(0xD);
port_e_pullups(0xFF);
output_d(0x00);

setup_timer_3(T3_EXTERNAL);
set_timer3(KESME);

enable_interrupts(INT_RB);
disable_interrupts(int_timer0);
disable_interrupts(int_timer3);
disable_interrupts(GLOBAL);
set_timer0(6);

VREFCON0 = 0xB0; // Fixed voltage reference module 4.096V
ADCON1 = 0x08;   // Positive edge on FVR negative edge on Vss
ADCON2 = 0xE2;   // 8 TAD time and Fosc/32
CTMUICON  = 0x2E; // 5.5 uA current source selected and calibrated to 6.1uA  475K resistor used on the channel AN10 to calibrate the current source

lcd_init();
setup_oscillator(OSC_16MHZ  |  OSC_PLL_OFF  |  OSC_INTRC);




 while(true)
{
   nem_olc1();
   itoa(CTMUICON ,2,yazi);
   cap = (6.2*100)/(nem*0.004) -30;
   humid = -0.000946337*cap*cap*cap + 0.491461*cap*cap- 81.9203*cap + 4395.84 ; 
   lcd_gotoxy(1,1);
   printf(lcd_putc,"V=%f,C=%0.1fpF",nem*0.004,cap);
   lcd_gotoxy(1,2);
   printf(lcd_putc,"%s  %.0f%c   ",yazi,humid,37);
   delay_ms(1000);

}
}




Sorry for the delay. The formula works well. But it was wrong one and i have found the right formula. It is in the code.


Last edited by Ömer Faruk on Tue Sep 15, 2020 2:37 pm; edited 1 time in total
Ömer Faruk



Joined: 15 Nov 2018
Posts: 42
Location: Çanakkale

View user's profile Send private message Send e-mail

PostPosted: Tue Sep 15, 2020 2:35 pm     Reply with quote

temtronic wrote:


2) PCB layout/wiring can add 'stray capacitance' according to datasheet. This can be a huge problem. If PCB traces aren't perfect and clean then coated, any moisture in the air can affect the reading.


There is a stray capacitance on the pad and the traces. But i haven't found the stray capacitance. But it can be calculated with CTMU model as stated in the datasheet. But i haven't figured it out yet. I subtract 30pf as stray capacitance to get reasonable results. The sensor responds quickly. When i blow my breath on the sensor the humidity rises.

https://ibb.co/m4cm7KK
Ttelmah



Joined: 11 Mar 2010
Posts: 16338

View user's profile Send private message

PostPosted: Wed Sep 16, 2020 12:50 am     Reply with quote

Two things here.
First your INT_RB routine, _must_ read the port B bits. Currently, since this
routine does not read the port, if this interrupt triggers, the chip will hang
permanently in this interrupt. INT_RB cannot be cleared till the port is read.
Result disaster. Either leave this interrupt disabled, or add code to the
handler to read the port.

Then your formula is wrong. No wonder it doesn't work!...

humid = -0.000946337*cap*cap*cap + 0.491461*cap*cap- 81.9203*cap + 4395.84 ;

Where are you getting this from?. This is not the formula at the site.
I suspect you are trying to scale the factors by the 180pF default for
the 55RH value?. This won't work. Because the factor is applied before
the cube and square calculations, it's effect will change as the value
changes.

The formula is:
Code:

   itoa(CTMUICON ,2,yazi);
   cap = (6.2*100)/(nem*0.004) -30;
   cap=cap/180;
   humid = -(3465.6*cap*cap*cap) + (10732*cap*cap) - (10457*cap) + 3245.9 ;


Totally different from what you have posted.....

For your maths to 'work', your sensor would have to be reading about 128pF
at 55RH. This has to imply there is something radically wrong with the
capacitance measurement. Post the circuit used, and where the constants
you are using in the conversion come from.
Ömer Faruk



Joined: 15 Nov 2018
Posts: 42
Location: Çanakkale

View user's profile Send private message Send e-mail

PostPosted: Mon Sep 21, 2020 1:17 pm     Reply with quote

Ttelmah wrote:

cap=cap/180;
humid = -(3465.6*cap*cap*cap) + (10732*cap*cap) - (10457*cap) + 3245.9 ;


I just have realized that when i put 201pF in the equation it results in 125%.
Are you sure that the formula is correct ? I have checked the datasheet. It must be 100% when the capacitance is 201pF.
temtronic



Joined: 01 Jul 2010
Posts: 7627
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Sep 21, 2020 1:37 pm     Reply with quote

The formula is based on 10KHz @25*C.
They supply a formula for frequency influence.
Ugh, more 'math'...
I don't know what the effect would be, but a spreadsheet might be the fastest way to 'see the numbers'.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 16338

View user's profile Send private message

PostPosted: Tue Sep 22, 2020 5:55 am     Reply with quote

The formula is correct. Your capacitance reading is wrong, as I suspected
a long time ago. Their data gives the capacitance at 100RH as 193.1pF
201pF is well above this.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group