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 the 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.


  • 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

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.

Anything just ask!!!

127 comments on “Temperature Sensor + Arduino

  1. lucas goulart July 7, 2008 1:30 pm

    nice and quick project! 🙂

  2. Ricardo Boabaid July 10, 2008 11:21 pm

    You rock man… Your website is awesome!

  3. John July 19, 2008 10:10 am

    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!

  4. Oscar G. July 19, 2008 12:05 pm

    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:

    Also would be fun to check also by website (in spanish):

    Best regards from spain and keep on the good work!
    Oscar G.

  5. Les Garwood July 19, 2008 2:00 pm


    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.



  6. Jim Tobias July 19, 2008 6:01 pm

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

  7. Chief Robot July 19, 2008 8:28 pm

    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?

  8. Silver July 20, 2008 5:04 am


    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)


  9. Bruce Schechter July 23, 2008 3:36 pm

    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.

  10. Raj July 26, 2008 8:45 am


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

  11. Skyler July 27, 2008 6:52 pm

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

  12. Flavio Curella July 31, 2008 7:12 pm

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

  13. DanielAndrade July 31, 2008 8:32 pm

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

    @Flavio No need to.

  14. Brutus August 10, 2008 2:15 am

    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:

    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:

    • jorge November 27, 2010 3:59 pm

      I´m using ARDUINO Diecimila.

      Will my LM35 sensor is faulty? For my reading in celcius have very high values, 99, 104 degrees ?

  15. DanielAndrade August 10, 2008 12:19 pm

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

  16. Michael Higgins September 9, 2008 9:39 pm

    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.

  17. Michael Higgins September 10, 2008 12:56 am

    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.

  18. action_owl September 12, 2008 11:04 pm

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

  19. dharmang September 18, 2008 5:18 pm

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

  20. dharmang September 18, 2008 5:22 pm

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

  21. DanielAndrade September 22, 2008 2:33 am

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

  22. Collin September 29, 2008 1:56 am

    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

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

    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

    Serial.print(0xFE, BYTE);
    Serial.print(128, BYTE); //position line 1 c1
    Serial.print(tempf); //display Temp F data
    Serial.print(0xFE, BYTE);
    Serial.print(132, BYTE); //position line 1 c4
    Serial.print(“fahrenheit”); //display Temp F
    Serial.print(0xFE, BYTE);
    Serial.print(192, BYTE); //position line 2 c1
    Serial.print(tempc); //display Temp C data
    Serial.print(0xFE, BYTE);
    Serial.print(196, BYTE); //position line 2 c4
    Serial.print(“Celsius”); //display Temp C
    Serial.print(0xFE, BYTE);
    Serial.print(148, BYTE); //position line 3 c1
    Serial.print(“Max”); //display Temp Max
    Serial.print(0xFE, BYTE);
    Serial.print(152, BYTE); //position line 3 c4
    Serial.print(maxi); //display Temp Max data
    Serial.print(0xFE, BYTE);
    Serial.print(212, BYTE); //position line 4 c1
    Serial.print(“Min”); //display Temp Min
    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(0x7C, 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(0x01, BYTE); //clear command.

  23. dharmang November 2, 2008 11:54 pm

    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 http://www.pachube.com !!!

  24. Jeff Bricker November 4, 2008 11:55 am

    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?

  25. Edward January 4, 2009 4:08 pm

    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.

  26. Vbryan January 14, 2009 1:09 pm

    How to do this with Temprature-Sensor SMT 160-30 ?

  27. Richard January 14, 2009 8:57 pm

    Hi Daniel,

    Any progress on adding the LCD and RTC to this project yet? Im a noob and i will be using this as part of my own learning project.

  28. DanielAndrade January 15, 2009 9:28 pm

    Richard, not yet, right now I am traveling in USA, but this year I will do for sure! Thanks for reading!

  29. ScaredyCat January 30, 2009 12:10 pm

    The ladyada.net link shows the use of a resistor – your circuit doesn’t .. was it an omission or is it not needed?

  30. ScaredyCat January 30, 2009 12:40 pm


    Just to reply to my own question. Without the resistor you will only be measuring temperatures of +2 Deg C to 150 Deg C. If you want to measure the full -55 Deg C to +150 Deg C you need the resistor.

  31. joel s. bacoor March 2, 2009 4:00 am

    can i use zilog? what is the code when i use zilog?

    • faby February 2, 2011 10:16 am

      pls help me by sending details about the arduino lilypad,for doing my project related to robotic textiles

  32. jpang April 20, 2009 7:37 pm

    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?

  33. atoweha May 1, 2009 1:13 pm

    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]

  34. Rudolph July 7, 2009 3:39 pm

    Hi, awesome tutorial !
    I’m testing the code using an Arduino NANO and some LM35DZ-NS

    The LM35 is hooked to 5v, Grd, A0

    I’m getting “weird” temperature readings:

    109 Celsius, 228 fahrenheit -> 109 Max, 100 Min
    109 Celsius, 228 fahrenheit -> 109 Max, 100 Min
    109 Celsius, 228 fahrenheit -> 109 Max, 100 Min

    What can be the cause of this false reading ..?

    Best regards

  35. Simon August 10, 2009 6:40 pm

    Good stuff, but I’m having a similar problem to Rudolph above – my temperature readings are all over the place. My room is warm, but I think I’d notice if it really was 415 deg C 🙂

    >415 Celsius, 779 fahrenheit -> 416 Max, 100 Min
    >415 Celsius, 779 fahrenheit -> 416 Max, 100 Min

    I note in one of the entries above it says –

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

    I’m using a 328 – would that make a difference?

  36. yalefe August 21, 2009 6:32 am

    I don’t understand
    i copy your sketch ( the first without LCD) but i obtain lot of warnings when i try to compil.( with Arduino v. 0017 )

    for exemple this :

    Serial.print(” fahrenheit -> “);

    error: missing terminating ” character In function ‘void loop()’:

  37. yalefe August 21, 2009 6:36 am

    i found the error with the “

  38. yalefe August 21, 2009 7:24 am


    i have a lm335 and i try to do what you say , but i don’t understand

    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)

    can you show me your code, please ?

    Sorry for my bad english, I’m french….

  39. yalefe August 21, 2009 2:37 pm

    ok , it’s good


    i don’t understand how


    samples[i] = ( analogRead(pin) * 500.0) / 1024.0;

    you update to this:

    samples[i] = ( analogRead(pin) * 110.0) / 1024.0;

  40. DanielAndrade August 21, 2009 9:53 pm

    you need to use the 5V for the code to work, otherwise the Analog-Digital converter will not work right.

    Check the datasheet to see how many mV per temp.

  41. Rmg August 23, 2009 7:20 am

    The jumpy temperatures also come from the arduino not rounding for example

    19.1 will become 19
    19.6 will become 19

    you can add 0.5 to your analog readout and with this you numbers get rounded

    like this :

    room_temp = ( analogRead(ro_temp) * 110.0) / 1024.0 0.5;
    (using internal ref)

    19.1 0.5 = 19.6 > 19
    19.6 0.5 = 20.1 > 20

  42. CaptainObvious August 25, 2009 11:38 am

    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

  43. Anders Pedersen October 2, 2009 5:15 am

    i don’t get it, im getting readings like this.

    ?¦æŤ??¦ä¶?Ä????´æb??¦?Å\??¦ä??$???NDæb??¦þŤ??¦ä¦?Ä? ??Ds1

    Any one who knows what the problem can be?

  44. DanielAndrade October 3, 2009 4:42 pm

    @Anders, that’s strange! Check only the analogRead of your AD, to see if it’s working.

  45. C October 8, 2009 11:06 am

    @Anders – you’ve got the wrong baud rate set. Try 9600

  46. Wouterk November 4, 2009 7:38 am


    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!

  47. Grant December 9, 2009 10:30 am

    I have a tip if you are powering through USB. Since it doesn’t go through the voltage regulator, and USB rarely gives exactly 5v, you need to adjust for the voltage your USB port puts out.

    Change “samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;” to “samples[i] = ( voltage * analogRead(pin) * 100.0) / 1024.0;”

    and at the top, add “float voltage = 5.0;”

    Then measure the 5V and GND pins with a volt meter, and put in the actual voltage. In my case it was 4.71. This made my sensor (LM335) quite a bit more accurate.

  48. rogkmyers December 10, 2009 9:33 am

    @ Simon and Rudolph – my readings are … 307 Celsius, 584 fahrenheit, 307 Max, 100 Min, even after I tried Grant’s fix. I am using a Duemilanove atmega328. my usb voltage is 5.09

    Also. if i put my fingers over the sensor, the temperature goes down, which is kinda good because, a fever of 300 C would be bad.

  49. Diego December 12, 2009 9:05 pm

    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

  50. DanielAndrade December 13, 2009 8:36 pm

    @Diego, sorry, didn’t get what you are saying. Are you brazilian, if so. Fala em português mesmo. 😛

  51. Marcos December 15, 2009 5:56 pm

    Danie, muito bom seu post!
    Parabéns principalmente pela explicação da fórmula. Confesso que eu entendia mais ou menos essa parte. Agora ficou claro pra mim.

  52. Ross Dargan January 12, 2010 10:44 pm

    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?

  53. Erik January 30, 2010 9:30 pm

    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

  54. Erik January 31, 2010 2:02 am

    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

  55. Dimitris February 3, 2010 12:08 am

    Hi there,

    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.

  56. Francesco February 3, 2010 9:43 am

    When you change Vref, the first reading isn’t correct so I added a void reading soon after analogReference():

    void setup {

    /* … */

    analogRead( analogPin );

    /* … */

  57. Solar Pond Pumps March 4, 2010 3:18 am

    Good post. I put you in RSS now so I can see more from you sometime again.

  58. Paul Hopkinson March 13, 2010 8:21 pm

    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
    // to the pins used:

    const int analogInPin = 1; // Analog input pin that Vout of the LM35 is connected to

    int sensorValue = 0; // value read from the LM35DZ
    int TempInCelcius = 0; // Calculated temperature in Celcius

    void setup() {
    // initialize serial communications at 9600 bps:

    void loop() {

    // Setup analog port 0 as Vcc
    pinMode(14, OUTPUT);
    digitalWrite(14, HIGH);
    // Setup analog port 2 as 0v
    pinMode(16, OUTPUT);
    digitalWrite(16, LOW);

    // read the analog in value:
    sensorValue = analogRead(analogInPin);

    // Calculate Temperature in Celcius

    TempInCelcius = ( 5.0 * analogRead(analogInPin) * 100.0) / 1024.0;

    // print the results to the serial monitor:
    Serial.print(“\n Temperature = ” );
    Serial.print(” Degrees c”);

    // wait 1 second before the next temperature reading


  59. hardik March 23, 2010 4:51 pm

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

  60. Flavio July 2, 2010 1:50 pm

    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?..


  61. Hughesy July 4, 2010 8:49 pm


    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.

  62. Flavio July 6, 2010 11:23 am

    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.7°C or better a 27.78°C instead of a no-decimal value like 27°C?…

    • ben February 24, 2011 4:36 am


      You will need to change the variables that temperature values from int datatypes to double

      ‘double samples[8];’ instead of ‘int samples[8];’

      (int datatype only stores whole numbers)


  63. Capn Scott July 6, 2010 5:54 pm

    Simon – your positive and negative are backwards…

  64. faiz October 21, 2010 2:43 pm

    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

  65. luan November 29, 2010 2:17 am

    Opa, sei que você é brasileiro né ? Então, eu estou trabalhando em um projeto que tem 1 sensor, um dallas 18B20. eu tentei usar esse código que vc utilizou e ele me retorna uma leitura 0°C,32F,0K … o que está totalmente equivocado.Tem alguma noção do pq ele não está lendo corretamente ?

    • DanielAndrade December 3, 2010 11:19 pm

      Da uma olhada no datasheet do sensor, e ajusta os cálculos que deve funcionar!

  66. mazai January 7, 2011 6:10 am

    What is the maximum distance between the temperature sensor and arduino can be?

  67. Shiji February 16, 2011 1:19 pm

    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

  68. Jason February 20, 2011 6:01 pm

    Hi can this temperature sensor work on ATmega328?

  69. ben February 24, 2011 4:22 am

    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' should be '1<=7'


    • ben February 24, 2011 4:25 am

      oops sorry that should read: ‘i< =7' should be 'i<=7'

  70. DanielAndrade February 25, 2011 1:00 am

    @Jason yes it will!!
    @Ben thanks, I’ll fix that! ^

    • Blaze March 5, 2011 11:14 pm

      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;

      void setup()
      Serial.begin(9600); //start serial communication

      void loop()
      tempc = (5 * analogRead(pin)*100)/1024;

      Serial.print(tempc, DEC);
      Serial.print(” Celsius”);

  71. gamezat March 22, 2011 9:32 pm

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

  72. Muzammil March 27, 2011 10:03 am

    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

    • Marry January 4, 2013 5:55 am

      I got the same problem as yours, have you figure out the solution? could you please share it with me? Thanks.

  73. jens June 15, 2011 6:48 am


    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

  74. sush July 11, 2011 5:52 am

    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..!!!

  75. Anant September 24, 2011 8:01 pm

    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 can’t afford to fry my Freeduino.


  76. Harparas G. December 4, 2011 4:40 am


    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.

    Thank you,

    Harparas Kaur

  77. Paul K. December 29, 2011 7:14 pm

    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.

  78. Gilbert January 19, 2012 11:10 am

    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:


    • DanielAndrade January 20, 2012 7:53 am

      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.


  79. John March 5, 2012 6:28 pm

    Hi Guys,

    Please what exactly does this line do?:

    samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;

    Thank you

  80. PP5VX (Bone) May 28, 2012 11:06 am

    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

    When they use the correct terminology ?
    ( I say it to TEXAS and NATIONAL, too… )


    PP5VX (Bone)

    • KF7PCL June 9, 2012 12:42 am

      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

  81. APCC DIY October 23, 2012 6:54 am

    thanks works very fine, i want to use it on a diy project (Heat recovery system)

  82. Denney Mark November 13, 2012 2:35 am

    sir what kind of arduino did you use?

  83. Malin December 17, 2012 10:18 am

    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.

  84. pritam lunawat March 5, 2014 5:08 am

    i want program of temperature sensor pt100 with arduino freeduino atmega 328..

  85. pritam lunawat March 5, 2014 5:21 am


  86. isme July 14, 2014 1:55 pm

    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.

  87. percolator on bong December 26, 2014 7:12 am

    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.

  88. Maly July 6, 2015 5:01 pm

    Hello ! I don’t know if somebody will answer me but I try !
    Do you think it is possible to use an Arduino Nano for this system ?
    Thank you very much !

    • Daniel Andrade July 6, 2015 9:28 pm

      Yes yon can! You can use any arduino or microcontroller with an ADC port.

  89. Darshit Khant October 21, 2015 4:37 am

    Please help me in program to ATmega328 connected with LCD 16*2 dispaly and LM35 sensor.

  90. sarra November 28, 2015 1:58 pm

    I want a code like this but for water level controller… helpe me please

  91. kavya March 26, 2016 3:14 am

    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

    for(i = 0;i< =7;i++){ // gets 8 samples of temperature
    please help me

  92. gamar May 7, 2016 8:46 am

    thank you very much

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.