What am I doing wrong?
Good question. Like any other troubleshooting task, rule one is: Check the hardware first. (Plus the fact that the program itself is really only three lines of code
First thing is to check if you have the LM35 oriented correctly. The quick wiring diagram in the comments of the code shows the two outer pins connected to +5 and ground, but it doesn't tell you which pin is which. Plus, there are four different packaging types for the LM35. The version I used is in the TO-92 package and it looks like a transistor. The LM35 also comes in a cylindrical package, a flat surface mount, and a TO-220 that looks like a voltage regulator. There is also a note that the earlier version of the TO-220 had a different pin out. Checking the pin orientation from the spec sheet is essential.
Because the LM35 is only rated to supply a reading up to 150 Celsius, I'm guessing there is a wiring problem. Check that the +5 and ground are going to the correct pins and that there is a signal resistor between the voltage output pin and ground (I used an 18K resistor). Also check that the sample voltage goes into the Analog Input pin 5 of the board (bottom connector, last pin).
Also make sure the +5 and ground are supplied from the Arduino board itself and not an external source. Having two differently referenced ground levels (called a floating ground) makes reading small voltage differences difficult and is a real pain to troubleshoot. (Ask me how I know
If you still don't get a believable reading, try taking the LM35 out of the circuit and replace it with a resistor 10 times the value of the signal resistor. This should give you a voltage divider circuit that will supply .45 volts directly to Analog Input pin 5. Running the program should say that the temp is approximately 92 Celsius, 198 Fahrenheit.
- Code: Select all
180K ohm 18K ohm
+5 ------/\/\/\/-----+---/\/\/\/-----> Gnd
+----------- Analog Input pin 5
One or more of these fixes should get the circuit and program working together. If you still have problems, let me know what value you got when using the basic voltage divider circuit shown above.