Amanda Travieso
/
HumiditySenor
Outputs humidity to the serial terminal.
Diff: main.cpp
- Revision:
- 0:a8fcc4098c24
- Child:
- 1:e59c75779477
diff -r 000000000000 -r a8fcc4098c24 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Jun 23 18:40:14 2015 +0000 @@ -0,0 +1,99 @@ + + +#include "HIH_4010.h" +#include "mbed.h" + + +Serial pc(USBTX, USBRX); + +AnalogIn ain_UseA(p15); +AnalogIn ain_UseB(p16); +AnalogIn ain_UseC(p17); +AnalogIn ain_UseD(p18); +AnalogIn ain_UseE(p19); +DigitalOut myled(LED1); + + +/* + +Constructor +dataPin: the IO pin connected to the sensor's data pin (pin 15) +supplyVoltage: the voltage supplying the humidity sensor (pins 1,3 - typ. 5.0) +referenceVoltage: motor controller's reference voltage +*/ + +HIH4010::HIH4010(float dataPin, float supplyVoltage, float referenceVoltage){ + +pin = dataPin; +vSupply = supplyVoltage; +vRef = referenceVoltage; + +/* +Relative Humidity is calculated using the following equations taken from the datasheet: +(1) Vout = (VSupply)(0.00636(sensorRH) + 0.1515) +(2) sensorRH = (Vout - zeroOffset) / slope + +Solving (1) for sensorRH: +sensorRH = (Vout - (0.1515)VSupply) / (0.00636)VSupply + +Equate result with (2): +zeroOffset = (0.1515)VSupply +slope = (0.00636)VSupply +*/ + +slope = 0.00636 * vSupply; +zeroOffset = 0.1515 * vSupply; + +} + + +/* +Convert sensor reading into relative humidity using equation (2) +*/ + +float HIH4010::getSensorRH() { +return ((vout() - zeroOffset) / slope); + +} + +/* +Get temperature-compensated relative humity. From data sheet: +trueRH = sensorRH / (1.0546 - 0.00216T) +*/ + +float HIH4010::getTrueRH(float temperature) { +return getSensorRH() / (1.0546 - (0.00216 * temperature)); + +} + +/* +Get sensor output voltage. +Assumes 12-bit (2^16 = 4096) A/D resolution. +*/ + +float HIH4010::vout() { +return (float)(ain_UseA.read_u16()) * 3.3 / 65536; + +} + + +int main() +{ + float ad[5]; + float vdiv = (3.3 / 65536); //3.3 is the reference voltage (AnalogIn measures from 0V to 3.3V) and 4096 is the highest number that can be represented by a 12 bit unsigned + while( 1 ){ + ad[0] = (float)ain_UseA.read_u16() * vdiv; //returns 12 bit value representing the current input voltage normalized + ad[1] = (float)ain_UseB.read_u16() * vdiv; + ad[2] = (float)ain_UseC.read_u16() * vdiv; + ad[3] = (float)ain_UseD.read_u16() * vdiv; + ad[4] = (float)ain_UseE.read_u16() * vdiv; + pc.printf("%5.3f,%5.3f,%5.3f,%5.3f,%5.3f\r\n ", ad[0],ad[1],ad[2],ad[3],ad[4]); + HIH4010 humidity(ad[0], 3.3, 3.3); + //pc.printf("%5.3f\r\n" , humidity.vout()); + pc.printf("%5.3f\r\n", humidity.getSensorRH()); + myled = 1; + wait(2.0); + myled = 0; + wait(2.0); + } +}