Hello people, it’s been a while since I have posted projects on this website. This semester was really busy, I didn’t have time to much else, but soon I will have my winter holiday (Here in south our summer holiday is from December to February).
Today I am going to show you how to build a simple temperature sensor using one LM35 Precision Temperature Sensor and Arduino, so you can hookup on your future projects. The circuit will send serial information about the temperature so you can use on your computer, change the code as you will. I’m planning to build a temperature sensor with max/min + clock + LCD, and when I get it done, I will post here.
Parts:
- Arduino (You can use other microcontroller, but then you will need to change the code).
- LM35 Precision Centigrade Temperature Sensor, you can get from any electronic store. Here is the DATA SHEET.
- BreadBoard
Assembling:
This is a quick and simple step. Just connect the 5V output from arduino to the 1st pin of the sensor, ground the 3rd pin and the 2nd one, you connect to the 0 Analog Input.
Down goes some pictures that may help you, click to enlarge:

Here is the Arduino Code, just upload it and check the Serial Communication Option.
You can also download the .pde HERE.
An open-source LM35DZ Temperature Sensor for Arduino. This project will be enhanced on a regular basis
(cc) by Daniel Spillere Andrade , http://www.danielandrade.net
http://creativecommons.org/license/cc-gpl
*/
int pin = 0; // analog pin
int tempc = 0,tempf=0; // temperature variables
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;
void setup()
{
Serial.begin(9600); // start serial communication
}
void loop()
{
for(i = 0;i< =7;i++){ // gets 8 samples of temperature
samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
tempc = tempc + samples[i];
delay(1000);
}
tempc = tempc/8.0; // better precision
tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit
if(tempc > maxi) {maxi = tempc;} // set max temperature
if(tempc < mini) {mini = tempc;} // set min temperature
Serial.print(tempc,DEC);
Serial.print(" Celsius, ");
Serial.print(tempf,DEC);
Serial.print(" fahrenheit -> “);
Serial.print(maxi,DEC);
Serial.print(” Max, “);
Serial.print(mini,DEC);
Serial.println(” Min”);
tempc = 0;
delay(1000); // delay before loop
}
Anything just ask! ![]()



nice and quick project!
You rock man… Your website is awesome!
Hey Daniel,
I did almost the same thing back in May and posted it to LadyAda’s website. However, mine didn’t have the nice pictures yours has. Nice job.
http://forums.ladyada.net/viewtopic.php?t=5763
I don’t understand where the multiplying by 5 comes from here:
samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
Cool Project, thanks!
Hello guys,
Cool and easy to understand circuit. Well done!
I have made a similar one as tutorial at my friend blog uchobby.com using Arduino adn LM35, including RPM fan control. I’ve used basicaly the same this as well as same formula to get temperature value. You can heck it out here:
http://www.uchobby.com/index.php/2007/09/23/arduino-temperature-controlled-pc-fan/
Also would be fun to check also by website (in spanish):
http://www.bricogeek.com
Best regards from spain and keep on the good work!
Oscar G.
Hi,
Thanks for sharing; it will be helpful to me. I’m working on a similar project. I used Silver’s code, mentioned above, to get me off the ground, but now I have to advance it and your code will help improve my understanding and capability, so thanks again.
Currently, I’m on the hunt for code that will display a bar graph in real time of the temperature data. So far, this has been daunting.
Regards,
Les
how many of the sensors can be connected to 1 arduino, and how long can the wires be? thanks, and sorry for the ignorance….
Great post!
I am also currently working on a similar project. I have the LM35 working and have just received my LCD 16 X 2 screen. Now to solder on the header pins….
How would you alter your code to not only give a readout of the temperature, but also turn something on and off like a small fan?
John,
Short answer:
The 5 refers to the 5v supply voltage.
Long answer:
The Analog to Digital Converter (ADC) compares a sample voltage to the supply voltage and provides a digital number based on 1024 steps. The formula (Sample Voltage * 1024 / Supply voltage) shows that 2.5volts * 1024 / 5volts would result in a digital number of 512 which is what we would expect for a voltage half the supply voltage.
To get the find the sample voltage from a digital number the formula is reversed (Digital number * supply voltage / 1024). So our count of 512 would give (512 * 5volts / 1024) and tell us that there is 2.5volts on the sample pin.
However, that doesn’t explain the 100 in his formula. That is there because the LM35 supplies 10 millivolts per degrees Celsius. So, let’s look at an example. The room temperature is 19 Celsius which means the LM35 supplies 190 millivolts, or .190volts. Putting that number into the first formula (.19volts * 1024 / 5volts) means we would expect to see a digital number of 39 on the analog pin. So let’s say we do get a reading of 39, what does the program do with it?
The formula in the program says ( 5volts * 39 * 100 / 1024) which equals 19 meaning 19 Celsius. As I said in my program, a close approximation of the temperature in Celsius can be found by dividing the input number by 2. Daniel’s program divides the number by 2.048 (5 * 100 / 1024) which is a little more accurate but because both programs are using integer values, the result is roughly the same. Use which ever formula you will be able to understand when reading your program six months from now. (grin)
Silver
I like this! I previously used a Dallas 18B20 sensor, which is far more accurate but works over a one-wire interface, making it tough to program. This seems plenty accurate enough, so I’ll give it a try!
Not that it makes much of a difference, but you really don’t require the array samples[i] to perform averaging. A single variable can easily be used to keep a running total, which can be zeroed out after each reading.
Hey,
Can you tell me how you connect the Arduino board to PC? What is the cost of that board?
Great tutorial! This makes a very nice and easily put together temperature sensor.
Didn’t you use any resistance from 5v to the temp sensor?
@Raj Just use a USB cable. It costs around U$34.
@Flavio No need to.
Just played with this and changed one thing:
Since the Arduino default vref is 5V, the analog read from 0 to 1024 represents 0 to 5V.
Changing the Vref to internal, means the Arduino will use 1.1V as a reference. This gives you more precision in the reading, and helps calm down the 19/20 degrees changes you see. the 0 to 1024 now represents 0 to 1.1V (So long as your arduino uses the ATmega168)
In your setup(), add this line:
analogReference(INTERNAL);
and your formula:
samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
could be changed for simplicity to
samples[i] = ( analogRead(pin) * 500.0) / 1024.0;
and so you would update to
samples[i] = ( analogRead(pin) * 110.0) / 1024.0;
This will give you a more precise reading from the sensor. (Just don’t exceed 100 degrees Celsius with the sensor, as it will then generate a voltage higher than vref)
Also.. a delay(200) is quite enough of a delay between sample reads.. the sensor output is constant, so the 8 samples can be accomplished in 1/5 of the time. Just change your main loop delay to a higher value, something of the order of delay(5000).
At least here, I noticed a more stable operation.
If your arduino uses the ATmega8, the above will still give a more precise reading, but you will need to change the formula to:
samples[i] = ( analogRead(pin) * 256.0) / 1024.0;
which in this case could be simplified again to
samples[i] = analogRead(pin) / 4.0;
AnalogReference page:
http://www.arduino.cc/en/Reference/AnalogReference
@Brutus great comment, I will take a look at that! Thanks for sharing!