Tempareture sensor driver edit Original maker zchen311

Dependents:   IoT_Weather_Station

Fork of TMP36 by ze chen

Committer:
hippi345
Date:
Tue Mar 08 14:16:59 2016 +0000
Revision:
1:ae41c2cdfd94
Parent:
0:ab3d7d0c34ce
first commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zchen311 0:ab3d7d0c34ce 1 class TMP36
zchen311 0:ab3d7d0c34ce 2 {
zchen311 0:ab3d7d0c34ce 3 public:
zchen311 0:ab3d7d0c34ce 4 TMP36(PinName pin);
zchen311 0:ab3d7d0c34ce 5 TMP36();
zchen311 0:ab3d7d0c34ce 6 operator float ();
zchen311 0:ab3d7d0c34ce 7 float read();
hippi345 1:ae41c2cdfd94 8 float readFah();
zchen311 0:ab3d7d0c34ce 9 private:
hippi345 1:ae41c2cdfd94 10 //sets up the AnalogIn pin
zchen311 0:ab3d7d0c34ce 11 AnalogIn _pin;
zchen311 0:ab3d7d0c34ce 12 };
zchen311 0:ab3d7d0c34ce 13
zchen311 0:ab3d7d0c34ce 14 TMP36::TMP36(PinName pin) : _pin(pin)
zchen311 0:ab3d7d0c34ce 15 {
zchen311 0:ab3d7d0c34ce 16 // _pin(pin) means pass pin to the AnalogIn constructor
zchen311 0:ab3d7d0c34ce 17 }
zchen311 0:ab3d7d0c34ce 18
zchen311 0:ab3d7d0c34ce 19 float TMP36::read()
zchen311 0:ab3d7d0c34ce 20 {
zchen311 0:ab3d7d0c34ce 21 //convert sensor reading to temperature in degrees C
zchen311 0:ab3d7d0c34ce 22 return ((_pin.read()*3.3)-0.500)*100.0;
zchen311 0:ab3d7d0c34ce 23 }
hippi345 1:ae41c2cdfd94 24
hippi345 1:ae41c2cdfd94 25 float TMP36::readFah()
hippi345 1:ae41c2cdfd94 26 {
hippi345 1:ae41c2cdfd94 27 float tempC = ((_pin.read()*3.3)-0.500)*100.0;
hippi345 1:ae41c2cdfd94 28 return 9.0*(tempC)/5.0 + 32.0;
hippi345 1:ae41c2cdfd94 29
hippi345 1:ae41c2cdfd94 30 }
hippi345 1:ae41c2cdfd94 31
zchen311 0:ab3d7d0c34ce 32 //overload of float conversion (avoids needing to type .read() in equations)
zchen311 0:ab3d7d0c34ce 33 TMP36::operator float ()
zchen311 0:ab3d7d0c34ce 34 {
zchen311 0:ab3d7d0c34ce 35 //convert sensor reading to temperature in degrees C
zchen311 0:ab3d7d0c34ce 36 return ((_pin.read()*3.3)-0.500)*100.0;
zchen311 0:ab3d7d0c34ce 37 }
zchen311 0:ab3d7d0c34ce 38