[color=#ff0000;]EDIT: I made a correction to the code and the description. I mixed up the LM34 and L35. Let me explain. The LM34 measures in Fahrenheit whereas the LM35 measures in Celsius. When using the LM34 you would need to subtract the 10mv * 25c (which is zero Fahrenheit) from the millivolt reading to show the reading correctly. With the LM35 this is not necessary. You may however still want to subtract some amount of mv from the reading if you decide to calibrate it as it may vary +-1.0c at room temperature.[/color]
On the off chance you don't have the documentation, I have attached it. First you need to look at the documentation. As Stefan pointed out all sensors are different. There are many temperature sensors out there and all of them work differently. The LM35 using a basic Analog Input on the Netduino. So what you need to know is that the LM35 provides a 250mV output at 25°C. This give you a scale to work with.
Using the Scale you determined you can create a class with the following code. You definite an analog input and set the range from 0v to 3.3v (max range of the analog input) in millivolts thus the 3300. Next you create a method to read the temp in centigrade. Performing the sensor.Read returns a reading of 0 - 3300 milli-volts. Since we know 250mv is 25c, we subtract the 250 to zero it out. Finally you take the milli-volts and divide by 10 since we know we get a 10mV change per degree Centigrade going by the specs.
public class LM35{ protected AnalogInput sensor; public LM35(Cpu.Pin pin) { sensor = new AnalogInput(pin); sensor.SetRange(0, 3300); } public float GetTemperatureInC() { // gain = 10 mV/Deg C int mv = sensor.Read() - 250; // 250 = 0c int mv = sensor.Read(); return mv / 10f; }}
To get the reading you'll need to instantiate the class and call the GetTemperatureInC method. You can use this type of code in your main loop.
public static void Main(){ LM35 sensor = new LM35((Cpu.Pin)AnalogIn.Pin.A0); while (true) { Debug.Print("Temperature: " + sensor.GetTemperatureInC() + "c"); Thread.Sleep(1000); }}
If you want your reading in Farenheit also you can easily at a new method to the class called GetTemperatureInF and have it call the GetTemperatureInC method and do the math to convert from C to F.
Edited by Dave VanderWekke, 15 February 2013 - 08:33 PM.