Anda di halaman 1dari 14

About (/about/) Photos (/photos/) Archive (/archives/) Contact (/contact/)

(http://www.danielandrade.net/)
Temperature Sensor + Arduino
(http://www.danielandrade.net/2008/07/05/temperature-
sensor-arduino/)
July 5, 2008
Hello people, its been a while since I have posted projects on this website. This semester was
really busy, I didnt 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 (http://www.arduino.cc), 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. Im 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 (http://www.national.com/ds/LM/LM35.pdf).
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
(http://www.danielandrade.net/files/temperature_sensor_lm35.pde).
/*
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
*/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

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
}
view raw (https://gist.github.com/dansku/5682776/raw/arduino-temperature.ino)
40
41
42
43
44
45
46
47
48
49
50
arduino-temperature.ino
(https://gist.github.com/dansku/5682776#file-arduino-temperature-ino) hosted with by GitHub (https://github.com)
Anything just ask!!!
Category: Arduino (http://www.danielandrade.net/category/arduino/), Electronics
(http://www.danielandrade.net/category/electronics/), Engineering
(http://www.danielandrade.net/category/engineering/), Hardware
(http://www.danielandrade.net/category/hardware/), Howto
(http://www.danielandrade.net/category/howto/)
105 Comments daniel andrade Login

Sort by Best Share

Join the discussion


Favorite

Reply
John 3 years ago
Hi Guys,
Please what exactly does this line do?:
samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
Thank you

15


Reply
pritam lunawat 6 months ago
i want program of temperature sensor pt100 with arduino freeduino atmega 328..

2


Reply
isme 2 months ago
Hey @john,
That line takes a sample reading(given in ohms(we are reading from a thermistor)) and converts
it to a temperature, then adds it to an array called samples to be averaged in the next step.

1


Reply
pritam lunawat 6 months ago
pt100


Reply
Malin 2 years ago
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.


Reply
Denney Mark 2 years ago
sir what kind of arduino did you use?


Reply
APCC DIY 2 years ago
thanks works very fine, i want to use it on a diy project (Heat recovery system)


PP5VX (Bone) 2 years ago
On SI the word "Centigrade" doesn't exists !
The correct name in SI is CELSIUS...
"Centigrade" is the 100th part of a degree,
and means nothing on this temperature's
measurements.
When they use the correct terminology ?
( I say it to TEXAS and NATIONAL, too... )
Regards,
Share
Share
Share
Share
Share
Share
Share
Reply
PP5VX (Bone)


Reply
KF7PCL 2 years ago > PP5VX (Bone)
PP5VX de KF7PCL
I feel the same way. I really do not understand why people call it centigrade.
Celsius is the correct term.
As far as the original post goes, I wonder if it is possible to use floating point to get more
precise measurements? With a 10 bit ADC, it should be accurate to atleast 0.5*C.
73 KF7PCL / John


Reply
Gilbert 3 years ago
Hi there.
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?
Many thanks if you're able to answer.
P.S. the servo control comes from this URL:
http://principialabs.com/ardui...
Gilbert


Reply
Daniel Andrade 3 years ago > Gilbert
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.
:)


Reply
Paul K. 3 years ago
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


Share
Share
Share
Share
Share
Reply
Harparas G. 3 years ago
Hello,
I reviewed your code that you have posted on your site (http://www.danielandrade.net/2.... 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.
Thank you,
Harparas Kaur


Reply
Daniel Andrade 3 years ago > Harparas G.
@Harparas Well, you can do in many different ways, one which I think is the easy one, is
to connect the arduino to the computer and with an processing program.
http://electronics.stackexchan...
You want to store 144, you can save it on a array. But then it will be stored in the RAM,
and deleted every time you reset the C
You can also use an EEPROM, that will save the information on ROM, that will not be
deleted when restarting. Here you get more infos: http://arduino.cc/playground/C...
Hope I have helped!
Daniel


Reply
Harparas K. 3 years ago > Daniel Andrade
Thank you! i will try it.


Reply
Anant 3 years ago
Hi. Please forgive my ignorance.
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 cant afford to
fry my Freeduino.
Thanks


sush 3 years ago
Share
Share
Share
Share
Reply
sush 3 years ago
heya..can anyone plz tell me dat can i use GH 311 ultrasonic tranciever module for temperature
measurement?? earlier i used TS 601 and it worked quite well.
can anyone tell me the coding..!!!


Reply
jens 3 years ago
hello,
i was wondering if i could use this type of sensor to measure a water temperature, because i am
using an arduino to control the water flood of a jaccuzi.
thxxx and greetings from Jens


Reply
Muzammil 3 years ago
Hi i m having a ardunio Uno board n i uploaded the code but i m getting strange output
287 Celsius, 548 fahrenheit -> 288 Max, 100 Min
287 Celsius, 548 fahrenheit -> 288 Max, 100 Min
i have tried other options + i have changed lm35 and tested it again but its the same.
plz help


Reply
Marry 2 years ago > Muzammil
I got the same problem as yours, have you figure out the solution? could you please
share it with me? Thanks.


Reply
gamezat 3 years ago
hello , i have built it and work greate
but any body develop software to read from ardino direact
thank you


Reply
Daniel Andrade 4 years ago
@Jason yes it will!!
@Ben thanks, I'll fix that! ^


Blaze 4 years ago > Daniel Andrade
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?
int pin = 0; //analog pin
int tempc = 0;
Share
Share
Share
Share
Share
Share
Reply
void setup()
{
Serial.begin(9600); //start serial communication
}
void loop()
{
tempc = (5 * analogRead(pin)*100)/1024;
delay(1000);
Serial.print(tempc, DEC);
Serial.print(" Celsius");
delay(1000);
}


Reply
ben 4 years ago
Hi, thanks for doing this - very neat!
just minor note: the for loop in the code at the moment has a space that needs to be removed 'i<
=7&#039 should be &#0391<=7&#039
:)


Reply
ben 4 years ago > ben
oops sorry that should read: i< =7&#039 should be &#039i<=7&#039


Reply
Jason 4 years ago
Hi can this temperature sensor work on ATmega328?


Reply
Shiji 4 years ago
Dear sir
Can You tell me How can i keep milk at 35 degree centegrade in a electrical cooker
For all the day
thank u
shiji


Reply
mazai 4 years ago
Hello.
What is the maximum distance between the temperature sensor and arduino can be?


Reply
luan 4 years ago
Opa, sei que voc brasileiro n ? Ento, eu estou trabalhando em um projeto que tem 1
sensor, um dallas 18B20. eu tentei usar esse cdigo que vc utilizou e ele me retorna uma leitura
0C,32F,0K ... o que est totalmente equivocado.Tem alguma noo do pq ele no est lendo
corretamente ?


Share
Share
Share
Share
Share
Share
Share
Reply


Reply
DanielAndrade 4 years ago > luan
Da uma olhada no datasheet do sensor, e ajusta os clculos que deve funcionar!


Reply
faiz 4 years ago
please tell me the coding for program, if lm35 detect 150 degree Celcius, fan will on, and if
below it will turn off..please help me ?..thank


Reply
Capn Scott 4 years ago
Simon - your positive and negative are backwards...


Reply
Flavio 4 years ago
Hi Hughesy!
Actually I'm using a LM35CAZ that is quite precise but I paid one sensor 10 euro. That's ok for
me 'cause I'm using it in a research project so I've a bit of money to spend on it..
But could someone help me and tell where and how to modify the code so that in my screen I'll
get a 27.7C or better a 27.78C instead of a no-decimal value like 27C?...
Please!


Reply
ben 4 years ago > Flavio
Hi
You will need to change the variables that temperature values from int datatypes to
double
i.e:
'double samples[8];' instead of 'int samples[8];'
(int datatype only stores whole numbers)
Ben.


Reply
Hughesy 4 years ago
Flavio,
That is not possible with this IC as it doesn't have the required precision. You will be able to out
put values to 2 decimal places but they will be incorrect you require a more accurate sensor.
If you find one that is fairly cheap please let me know as I am also interested.


Flavio 4 years ago
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?..
Share
Share
Share
Share
Share
Share
Share
Reply
modify? and how?..
Thanks!!!


Reply
hardik 4 years ago
hw can i interfacw the lm 358 with lm 35 to achive maximum gain??/??/ just plz.. rpl.. as soon aa
possible.......thank you.......


Reply
Paul Hopkinson 5 years ago
see more
For anyone who wants to plug an LM35 directly into the Arduino board here is a version that
uses analog inputs only to power
the LM35 sensor.
/*
Reads from a LM35DZ connected directly to an Arduino
Also prints the temperature to the serial monitor.
The circuit:
* LM35 Vcc connected analog input pin 0
* LM35 Vout connected to analog input pin 1
* LM35 Gnd connected to analog input pin 2
created 13th March 2010
by Paul Hopkinson [Modified from an original Tom Igoe sketch]
*/
// These constants won't change. They're used to give names


Reply
Solar Pond Pumps 5 years ago
Good post. I put you in RSS now so I can see more from you sometime again.


Reply
Francesco 5 years ago
When you change Vref, the first reading isn't correct so I added a void reading soon after
analogReference():
void setup {
/* ... */
analogReference(INTERNAL);
analogRead( analogPin );
/* ... */


Dimitris 5 years ago
Hi there,
Share
Share
Share
Share
Share
Reply
anyone used a 1N4148 diode for temperature sensor? I'm trying to get results but I don't know
how to convert the given voltage.


Reply
Erik 5 years ago
Not sure what happened to my comment.
If you want to show 22.6 for temp, multiply your constants by 10 and then you can get them out
when you report the data.
for ( i = 0; i


Reply
Erik 5 years ago
If you want to have a decimal value (ie 22.6), you can do this:
for ( i = 0; i maxi) {maxi = tempc;} // set max temperature
if(tempc


Reply
DanielAndrade 5 years ago
@Ross Amazing! Good job :)


Reply
Ross Dargan 5 years ago
Here shows what I mean (sample taken every minute)
http://tweetphoto.com/8634960
Ta
Ross


Reply
Ross Dargan 5 years ago
This works well via USB, but is all over the place when powered via the mains! (I'm reading the
results via Ethernet).
Has anyone else experienced this?


Reply
DanielAndrade 5 years ago
@Marcos , no esquea de visitar o meu blog em portugus -> http://blog.danielandrade.net
Valeu


Reply
Marcos 5 years ago
Danie, muito bom seu post!
Parabns principalmente pela explicao da frmula. Confesso que eu entendia mais ou menos
essa parte. Agora ficou claro pra mim.
Abrao.


Share
Share
Share
Share
Share
Share
Share
Share
Load more comments
Reply
DanielAndrade 5 years ago
@Diego, sorry, didn't get what you are saying. Are you brazilian, if so. Fala em portugus
mesmo. :P


Reply
Diego 5 years ago
I have a question I'm using a teensy which uses a USB instead of a serial how could I modify the
program?
Thanks in advance


Subscribe

Add Disqus to your site


d
Share
Share

Anda mungkin juga menyukai