Temperature Sensor + Arduino

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.

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.

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?

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)

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.

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)

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;

SOLAR WATER HEATER PUMP CONTROLLER
I want to make a simplistic controller for a solar water heater pump, for a fish pond. It would consist of a solar water heater panel with an air temp sensor LM35 inside panel box. A 120VAC submersible pump, a 3000gal fish pond with a water temp sensor LM35 (waterproofed), and a controller box supplied by 120VAC.
The logic should be
1. If the pond water Temp is below ~75°F, and the Solar Panel Temp is above ~90°F, Turn ON AC to the pump.
2. If pond water Temp reaches ~80°F, Turn OFF pump
3. If pond water drops to ~68°F sound remote alarm
*Temperatures should be variables that can be adjusted
What do I need to make this controller and program it, I am thinking this is well within the capabilities of the Arduino board. But maybe someone has a better solution. I am a novice but have skills in board assembly but little programming experience.

This is great! I’m using your code to make an Arduino thermostat. Im going to use it to control the temperature in my Bull Snakes cage. But Im sure it would work great for a Kegerator or various other projects.
Heres what Ive come up with so far.

I set up something similar with a LM34 (which reads in degrees F).
The device would overheat unless I put a current limiting resistor in series with the LM34. (10K at Vs). I don’t see the resistor on anyone else’s circuit, am I missing something?

This can easily be used with an LM335 (135,235). The only modification to the code is to subtract 273.15 from your samples[i]to convert from Kelvin to Celsius.

On the hardware side, you only use 2 pins with the LM335. Your reference voltage connects to the center( ) pin through a 2k2 resistor and the negative pin goes to ground, Analog 0 then connects directly to the center pin.

You guys are so admirable that you can build a solution like this. I am in the market looking for a temperature sensor which can alert me should the temperature goes out of range. To do this, I want to use SNMP utilities to make query on the temperature and send email alert if it is too high. The SNMP utility will also log data and plot graph.

How can we further develop your project to make it SNMP ready? Am I asking for too much?

Hi, can you tell me what kind of temperature sensor you used? is it a 1K or 50? i want to try this project out and im buying the temp sensor you used. please email me back with the answer at [email protected]
thanks!

>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)

what is your your reference voltage ? 3,3 V or 5 Volts ? and why a resistance ? is the same with 3,3 v then with 5 v ?
why don’t use the pin Adjust on the LM335 ?
i connect as you say but the temp doesn’t move (with or without my finger on the lm335)

I’m so confused! I’m using the LM335AZ, and I’ve tried original method, and.. well realized it wasn’t the same part.. so that was no good! But then I tried Edwards post.. and again, it was just way off. The original(obviously) was very high, 230C, and after trying Edwards.. dropped down to too low, almost a -3C, and it’s not freezing(yet) in my room 😀

@Edward, when you connect it as you were saying, just the 2 pins, AREF to center pin through a 2.2k resistor, and ground, grounded, with the output pin between the resistor and the thermistor, are you changing the analogReference in software to anything? I tried (INTERNAL) and still, way off.

But great write up on the sensor in use, haha hope you don’t mind if I ask some questions!:X

I’ve been trying to get this working in exactly the same way as you did (also with arduino duemilanova).
I’m using the same code and hardware setup. Everything seems to be working just fine, but the temperatures i get are just not right, its giving me 71 degrees celcius when it should be like 20 degrees (room temperature).

I’ve tried different LM35’s but they all do the same, what am i overlooking here? any help would be appreciated!

Hi everyone!
This code is great and simple for people like me just beginning with Arduino..
Could anyone tell me how to get also decimal values of temperature?
If I want to get 26.7 degrees or even better 26.78 degrees which part of the code do I have to modify? and how?..

I am a beginner at using the Arduino Uno, and I hope you can help me with something that I am having trouble with. I used some of your code for a LM335 temperature sensor that I am using. I have made a buffer circuit to convert the 10mV/K into a degrees celsius circuit. The circuit outputs for 0 degrees and 100 degrees is 2.73V and 3.73V respectively; but I am having problems getting the arduino to display the temperature. Could you look at my code at tell me if I did something wrong? Where should my voltage from my circuit go into the Arduino (A0 pin?) Can you help me?

OK. In the ADC section of page 263 of the instruction manual of the ATmega 328P it speaks of a Temperature sensor. Do they mean internal temperature sensor? Or is it a special channel reserved to connect a Temperature sensor?

I am building a Temperature data logger using an LM35DZ sensor. Is there a need to connect the temperature sensor to a dual op-amp LM358, to amplify the signal and detect very small temperature variations? Or is it that the Freeduino v 1.22 with the ATmega 328P will be able to detect the very small variations in Temperature? If so, how? Is it because of a fast sampling rate of the microcontroller ADC that the small variations will be able to be picked up? I see you have connected the sensor directly to the Freeduino.

What range of input DC voltage can I connect to the Freeduino v1.22? Because I can’t afford to fry my Freeduino.

I reviewed your code that you have posted on your site (http://www.danielandrade.net/2008/07/05/temperature-sensor-arduino/). I am generating a code that is similar to yours. My only question is, how would I go about storing data every 5 minutes, for 12 hours? I will appreciate it if you can get back to me at your earliest convenience.

Just a heads up – if you cut and paste code from the comments here to the Arduino V1.0 IDE, and it was posted with a non-US-English keyboard, and you are using a US-English keyboard, you will get strange errors when you try to compile. Errors like ” stray ‘\’ in program” or “expected `)’ before ‘u00d701′”. These seem to be caused by differences in the code pages. Some of these are caused by the ” character specifically in Serial.print(“something”); lines. Delete the ” and retype it. I suspect some ( or ) were causing problems but couldn’t verify. Can’t speak for any other keyboard combinations.

Just something to watch for if you’re getting strange compile errors.
PCK

I am thinking about using this setup, however I have a question that I am not able to easily find the answer to:

I am already using the Arduino (Uno) to pilot [4] servos, is it possible to upload your sketch on the same device and have both servo controls (via USB) and temperature being fed back to the computer for treatment at the same time?

Hello Gilbert, what you could do is first make the program read the temperature, then try to join them together, doing this way I think you will be able to make it with no problem.
I was taking a look at the code, maybe you could also add a CASE that reads the temperature and sends to the computer.

Hi!
I’m making a project with an sht15-sensor. Do you know how to program it if i want an alarm for example if temperature is over 20oC (68 F)? I have tried but can’t get the function correct. Please help.

We’re a group of volunteers and opening a new scheme in our community.
Your web site offered us with valuable information to work on. You have done an impressive job and our entire community will be grateful to you.

Hello sir nice and quick project, but I am the beginner for arduino the same code which i have uploded in my Richduino Board
I am getting this error
expected primary-expression before ‘=’ token
sketch_mar26a.ino: In function ‘void loop()’:
sketch_mar26a:16: error: expected primary-expression before ‘=’ token
for the line