Outputs humidity to the serial terminal.

Dependencies:   mbed

Committer:
atravieso
Date:
Tue Jun 23 18:40:14 2015 +0000
Revision:
0:a8fcc4098c24
Child:
1:e59c75779477
Working for the HIH-5030 series humidity sensor.  Written for 16-bit A to D.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
atravieso 0:a8fcc4098c24 1
atravieso 0:a8fcc4098c24 2
atravieso 0:a8fcc4098c24 3 #include "HIH_4010.h"
atravieso 0:a8fcc4098c24 4 #include "mbed.h"
atravieso 0:a8fcc4098c24 5
atravieso 0:a8fcc4098c24 6
atravieso 0:a8fcc4098c24 7 Serial pc(USBTX, USBRX);
atravieso 0:a8fcc4098c24 8
atravieso 0:a8fcc4098c24 9 AnalogIn ain_UseA(p15);
atravieso 0:a8fcc4098c24 10 AnalogIn ain_UseB(p16);
atravieso 0:a8fcc4098c24 11 AnalogIn ain_UseC(p17);
atravieso 0:a8fcc4098c24 12 AnalogIn ain_UseD(p18);
atravieso 0:a8fcc4098c24 13 AnalogIn ain_UseE(p19);
atravieso 0:a8fcc4098c24 14 DigitalOut myled(LED1);
atravieso 0:a8fcc4098c24 15
atravieso 0:a8fcc4098c24 16
atravieso 0:a8fcc4098c24 17 /*
atravieso 0:a8fcc4098c24 18
atravieso 0:a8fcc4098c24 19 Constructor
atravieso 0:a8fcc4098c24 20 dataPin: the IO pin connected to the sensor's data pin (pin 15)
atravieso 0:a8fcc4098c24 21 supplyVoltage: the voltage supplying the humidity sensor (pins 1,3 - typ. 5.0)
atravieso 0:a8fcc4098c24 22 referenceVoltage: motor controller's reference voltage
atravieso 0:a8fcc4098c24 23 */
atravieso 0:a8fcc4098c24 24
atravieso 0:a8fcc4098c24 25 HIH4010::HIH4010(float dataPin, float supplyVoltage, float referenceVoltage){
atravieso 0:a8fcc4098c24 26
atravieso 0:a8fcc4098c24 27 pin = dataPin;
atravieso 0:a8fcc4098c24 28 vSupply = supplyVoltage;
atravieso 0:a8fcc4098c24 29 vRef = referenceVoltage;
atravieso 0:a8fcc4098c24 30
atravieso 0:a8fcc4098c24 31 /*
atravieso 0:a8fcc4098c24 32 Relative Humidity is calculated using the following equations taken from the datasheet:
atravieso 0:a8fcc4098c24 33 (1) Vout = (VSupply)(0.00636(sensorRH) + 0.1515)
atravieso 0:a8fcc4098c24 34 (2) sensorRH = (Vout - zeroOffset) / slope
atravieso 0:a8fcc4098c24 35
atravieso 0:a8fcc4098c24 36 Solving (1) for sensorRH:
atravieso 0:a8fcc4098c24 37 sensorRH = (Vout - (0.1515)VSupply) / (0.00636)VSupply
atravieso 0:a8fcc4098c24 38
atravieso 0:a8fcc4098c24 39 Equate result with (2):
atravieso 0:a8fcc4098c24 40 zeroOffset = (0.1515)VSupply
atravieso 0:a8fcc4098c24 41 slope = (0.00636)VSupply
atravieso 0:a8fcc4098c24 42 */
atravieso 0:a8fcc4098c24 43
atravieso 0:a8fcc4098c24 44 slope = 0.00636 * vSupply;
atravieso 0:a8fcc4098c24 45 zeroOffset = 0.1515 * vSupply;
atravieso 0:a8fcc4098c24 46
atravieso 0:a8fcc4098c24 47 }
atravieso 0:a8fcc4098c24 48
atravieso 0:a8fcc4098c24 49
atravieso 0:a8fcc4098c24 50 /*
atravieso 0:a8fcc4098c24 51 Convert sensor reading into relative humidity using equation (2)
atravieso 0:a8fcc4098c24 52 */
atravieso 0:a8fcc4098c24 53
atravieso 0:a8fcc4098c24 54 float HIH4010::getSensorRH() {
atravieso 0:a8fcc4098c24 55 return ((vout() - zeroOffset) / slope);
atravieso 0:a8fcc4098c24 56
atravieso 0:a8fcc4098c24 57 }
atravieso 0:a8fcc4098c24 58
atravieso 0:a8fcc4098c24 59 /*
atravieso 0:a8fcc4098c24 60 Get temperature-compensated relative humity. From data sheet:
atravieso 0:a8fcc4098c24 61 trueRH = sensorRH / (1.0546 - 0.00216T)
atravieso 0:a8fcc4098c24 62 */
atravieso 0:a8fcc4098c24 63
atravieso 0:a8fcc4098c24 64 float HIH4010::getTrueRH(float temperature) {
atravieso 0:a8fcc4098c24 65 return getSensorRH() / (1.0546 - (0.00216 * temperature));
atravieso 0:a8fcc4098c24 66
atravieso 0:a8fcc4098c24 67 }
atravieso 0:a8fcc4098c24 68
atravieso 0:a8fcc4098c24 69 /*
atravieso 0:a8fcc4098c24 70 Get sensor output voltage.
atravieso 0:a8fcc4098c24 71 Assumes 12-bit (2^16 = 4096) A/D resolution.
atravieso 0:a8fcc4098c24 72 */
atravieso 0:a8fcc4098c24 73
atravieso 0:a8fcc4098c24 74 float HIH4010::vout() {
atravieso 0:a8fcc4098c24 75 return (float)(ain_UseA.read_u16()) * 3.3 / 65536;
atravieso 0:a8fcc4098c24 76
atravieso 0:a8fcc4098c24 77 }
atravieso 0:a8fcc4098c24 78
atravieso 0:a8fcc4098c24 79
atravieso 0:a8fcc4098c24 80 int main()
atravieso 0:a8fcc4098c24 81 {
atravieso 0:a8fcc4098c24 82 float ad[5];
atravieso 0:a8fcc4098c24 83 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
atravieso 0:a8fcc4098c24 84 while( 1 ){
atravieso 0:a8fcc4098c24 85 ad[0] = (float)ain_UseA.read_u16() * vdiv; //returns 12 bit value representing the current input voltage normalized
atravieso 0:a8fcc4098c24 86 ad[1] = (float)ain_UseB.read_u16() * vdiv;
atravieso 0:a8fcc4098c24 87 ad[2] = (float)ain_UseC.read_u16() * vdiv;
atravieso 0:a8fcc4098c24 88 ad[3] = (float)ain_UseD.read_u16() * vdiv;
atravieso 0:a8fcc4098c24 89 ad[4] = (float)ain_UseE.read_u16() * vdiv;
atravieso 0:a8fcc4098c24 90 pc.printf("%5.3f,%5.3f,%5.3f,%5.3f,%5.3f\r\n ", ad[0],ad[1],ad[2],ad[3],ad[4]);
atravieso 0:a8fcc4098c24 91 HIH4010 humidity(ad[0], 3.3, 3.3);
atravieso 0:a8fcc4098c24 92 //pc.printf("%5.3f\r\n" , humidity.vout());
atravieso 0:a8fcc4098c24 93 pc.printf("%5.3f\r\n", humidity.getSensorRH());
atravieso 0:a8fcc4098c24 94 myled = 1;
atravieso 0:a8fcc4098c24 95 wait(2.0);
atravieso 0:a8fcc4098c24 96 myled = 0;
atravieso 0:a8fcc4098c24 97 wait(2.0);
atravieso 0:a8fcc4098c24 98 }
atravieso 0:a8fcc4098c24 99 }