Reading LM34 Temperature Sensor

09 Feb 2010

I got my first embed yesterday and I am already enjoying its simplicity of programming and great breadboard formfactor. I did a few light shows on the four LEDs and setup a debug terminal through the USB serial connection. All very cool, and easy stuff - a great out-of-the-box experience.

Then I went to my parts bin and found an LM34 temperature sensor and after a brief wiring snafu was able to setup a temperature data logger in no time. (Breadboard, mbed, LM34, three jumpers) I forced the clock settings for my start time, which was also quite easy (after I read the correct time structure settings).

Just to test I scaled the analog input with just a multiplier hack:

AnalogIn LM34(p20);
temp = LM34.read()*300;

I am sure this number is close, but what is the proper way to scale the input for this device? I am running it directly off the 5V USB (VU), with the output to p20.

So far I love this little embed guy! Thanks for including the handy pin reference cards. I am looking forward to playing with my SPI, I2C stuff today. And my SoundGin and SpeakJet chips are waiting for some use. I will certainly be contributing to the community as my knowledge increases. I guess I can still use all my old PIC stuff for remote sensors. :-)

-Johnnie

10 Feb 2010

Johnnie,

 

Exactly the same thing I did w/ my system!  I wrote a small program to log temp data and a python program to grab it via USB<->Serial and graph it.  I convert the value into decimal Fahrenheit degrees between 0 and 255...

http://mbed.org/users/leimgrub/programs/gpdz5a

I don't remember exactly what I did in the program above, but my understanding of the LM34 datasheet is that without a negative voltage bias, it starts can only read between [5, 300] deg F.  It should output 10mv / degF.  You need to take into account that the A2D reference is 3.3v (regardless of the fact you power the LM34 from the +5 volt rail).  I'm not 100% about if 5 degF reads 0 volts or 50mV and I couldn't find that exactly in the datasheet.

read = 0.0 = 0.0v = 5 degF

read = 0.1 = 0.33v = (33 + 5)  = 38 degF

Essentially take the read() result times 330 and add 5 deg.

Let me know how that works out... For some reason I recall subtracting 5, but I don't know why... Maybe you can figure it exactly out!

Thanks!

-John

10 Feb 2010

Thank you much, John! I will give that a try while I am in a phone meeting today. It is nice that I can just grab my breadboard and plug it into any computer to test.

Really loving this embed so far. I just wish unit cost was cheaper so I could use it ubiquitously. I will have to research how the actually MCU is used external to the development system.

- Johnnie

16 Feb 2010

The following is the results I got after a bunch of testing at a temperature of 72 degrees fahrenheit as measured by my DMM temp probe touching the LM34.

-Johnnie

 

// LM34 temperature fahrenheit temperature sensor calibration test
#include "mbed.h"

Serial usbserial(USBTX, USBRX);

AnalogIn LM34(p20);       // LM34 temperature sensor output is directly attached to p20 (no resistor)
                          // positive voltage supplied by VOUT (pin 40 on embed)
float multiplier = 77;    // this number got me closest to the reading on my multimeter temp probe
float temp;               // calculated temperature
int count;                // for computing average reading
float total;
float average;

int main() {
    count = 0;
    total = 0.0;
    while (1) {
        // formula is analog reading * multiplier + 5 degrees F
        temp = LM34.read() * multiplier + 5;
        count++;
        total += temp;
        average = total / count;
        usbserial.printf("Temperature= %6.2f Average= %5.1f \n", temp  , average );
        wait(2);
    }
}