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:

Temperature Sensor

sens

processin

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

Share/Save/Bookmark

32 Replies

No one has
  1. 1 lucas goulart

    nice and quick project! :)

  2. 2 Ricardo Boabaid

    You rock man… Your website is awesome!

  3. 3 Silver

    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

  4. 4 John

    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!

  5. 5 Oscar G.

    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.

  6. 6 Les Garwood

    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

  7. 7 Jim Tobias

    how many of the sensors can be connected to 1 arduino, and how long can the wires be? thanks, and sorry for the ignorance….

  8. 8 Chief Robot

    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?

  9. 9 Silver

    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

  10. 10 Bruce Schechter

    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.

  11. 11 Raj

    Hey,

    Can you tell me how you connect the Arduino board to PC? What is the cost of that board?

  12. 12 Skyler

    Great tutorial! This makes a very nice and easily put together temperature sensor.

  13. 13 Flavio Curella

    Didn’t you use any resistance from 5v to the temp sensor?

  14. 14 DanielAndrade

    @Raj Just use a USB cable. It costs around U$34.

    @Flavio No need to.

  15. 15 Brutus

    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

  16. 16 DanielAndrade

    @Brutus great comment, I will take a look at that! Thanks for sharing!

  17. 17 Michael Higgins

    Is there a way to make this sensor work for liquids, in particular pond water. what is the max length for a cable between board and sensor. I hope this string has not been abandoned.

  18. 18 Michael Higgins

    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.

  19. 19 action_owl

    is it possible to save the temperature readings to an SD card or USB flash drive?

  20. 20 DanielAndrade

    @Michael Higgins, yes, it’s possible to do that, take a look gere: http://www.instructables.com/id/Waterproof-a-LM35-Temperature-Sensor

    @action_owl Yep, on the web you will find information about that. I haven’t tried yet, but I want to do it too. If you do it, please share with us!! :D

  21. 21 dharmang

    hey i tried using the same code and the sensor, the only problem i faced it it doesn’t show anything. I am working with arduino decimila 0011 is it anything related to software or the hardware ??

  22. 22 dharmang

    it just shows
    Done uploding
    Binary sketch size: 3578 bytes (of a 14336 byte maximum)

  23. 23 DanielAndrade

    @dharmang After uploading, you must check the serial communication option. have you tried that?

  24. 24 Collin

    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.

    /*
    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 heat = 2; // heater output on pin 2
    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()
    {
    pinMode(heat, OUTPUT);
    Serial.begin(9600);
    backlightOn();
    }

    void loop()
    {

    for(i = 0;i maxi) {maxi = tempf;} // set max temperature
    if(tempf = 85) {digitalWrite(heat, LOW); // turn heater off if temp is 85 or higher;
    clearLCD();} // Removes HEATING from lcd

    delay(10);
    Serial.print(0xFE, BYTE);
    Serial.print(128, BYTE); //position line 1 c1
    Serial.print(tempf); //display Temp F data
    delay(10);
    Serial.print(0xFE, BYTE);
    Serial.print(132, BYTE); //position line 1 c4
    Serial.print(”fahrenheit”); //display Temp F
    delay(10);
    Serial.print(0xFE, BYTE);
    Serial.print(192, BYTE); //position line 2 c1
    Serial.print(tempc); //display Temp C data
    delay(10);
    Serial.print(0xFE, BYTE);
    Serial.print(196, BYTE); //position line 2 c4
    Serial.print(”Celsius”); //display Temp C
    delay(10);
    Serial.print(0xFE, BYTE);
    Serial.print(148, BYTE); //position line 3 c1
    Serial.print(”Max”); //display Temp Max
    delay(10);
    Serial.print(0xFE, BYTE);
    Serial.print(152, BYTE); //position line 3 c4
    Serial.print(maxi); //display Temp Max data
    delay(10);
    Serial.print(0xFE, BYTE);
    Serial.print(212, BYTE); //position line 4 c1
    Serial.print(”Min”); //display Temp Min
    delay(10);
    Serial.print(0xFE, BYTE);
    Serial.print(216, BYTE); //position line 4 c4
    Serial.print(mini); //display Temp Min data

    tempc = 0;

    delay(500); // delay before loop
    }
    void backlightOn(){ //turns on the backlight
    Serial.print(0×7C, BYTE); //command flag for backlight stuff
    Serial.print(157, BYTE); //light level 157 = 100% 128 = off
    }
    void clearLCD(){
    Serial.print(0xFE, BYTE); //command flag
    Serial.print(0×01, BYTE); //clear command.
    }

  25. 25 dharmang

    hi again but i was thinking if you can guide me out with sending real time data on to web for temperature and humidity !!! through www.pachube.com !!!

  26. 26 Jeff Bricker

    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?

  27. 27 Paul

    Hi,

    Would any one have any idea how to modify this to work with an LM135?

    http://www.jaycar.com.au/products_uploaded/LM135.pdf

    Thanks

  28. 28 Edward

    Hey Paul,

    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.

    That’s the way I have things set up, and it seems to be working well.

  1. 1 Temperature sensor + Arduino | The Kevin Pipe
  2. 2 GB Vehicles Info » Blog Archive » Temperature sensor + Arduino
  3. 3 DELACREW.net WEBLOG » Blog Archive » Arduino a go-go
  4. 4 Bookmarks about Sensor

Leave a Reply