How to measure temperature with the Dallas Maxim DS1820 sensor?

February 17th, 2006

LCD Temperature reading, ATMega16, DS1820 and HD44780
Sometimes we have a need to measure the temperature of a room, of an object or just maybe a terrarium; so it never gets too hot or too cold for your hamster and it warns the system to turn on a cold or hot fan (ok just a bit Sci-Fi) ;-) .

It can be done in less than 3 hours. All what you need is a microcontroller and a temperature sensor. In case you want to build just a warning system then you don’t need a microcontroller because that can be done with a comparator but next time more about comparators.

Today you have a great number of temperature sensors, analog and digital ones. If you want to use an ADC (Analog to Digital Converter) then you can use a thermistor for this project, however I prefer digital sensors. In this project I used the Dallas Maxim DS1820 (click for the datasheet) digital temperature sensor with the 1wire technology from Dallas Maxim.

DS1820 Pinout
The pinout for the DS1820

DS1820 Scheme for using with a microcontroller
Scheme how I connected the DS1820 with my AVR ATMega16

It is not manufactured anymore but it is equivalent to its successor DS18S20. You can order a sample from Maxim, of course free of charge. This time the source code was not a problem, I found the source for this project in CodeVision’s C example folder (there are a lot of examples also for GNU AVR GCC), I just modified it a bit, I removed some of the code that we won’t need because I used only one DS1820 sensor however you can attach up to 8 sensors on one wire, cool isn’t it?!?! I had not a 4.7k resistor to connect between DQ (the middle pin from the sensor which is used to communicate with it the microcontroller and to read the temperatures) and Vdd, instead I used a 6.8k resistor; it will work fine, don’t worry. DQ is connected to I/O line 6 of PORT A. The microcontroller I used was the ATMega16 and a crystal of 4 MHz frequency; it should work with any other microcontroller as well. I won’t explain how I connected the LCD because I did it in one of my previous projects. It is connected to the PORT C, just to display the results (the current temperature).

DS1820
DS1820, on a small board with the pullup resistor

  1. /* The DS1820/18S20 sensors are connected to
  2. bit 6 of PORTA of the ATMega16 as follows:
  3. [DS1820/18S20] [STK500 PORTA HEADER]
  4. 1 GND         -   9  GND
  5. 2 DQ          -   7  PA6
  6. 3 VDD         -  10 +5V
  7. All the temperature sensors must be connected
  8. in parallel
  9. AN 4.7k (6.8k IN MY CASE) PULLUP RESISTOR MUST
  10. BE CONNECTED BETWEEN DQ (PA6) AND +5V !
  11. */
  12. #asm
  13. .equ __w1_port=0×1b
  14. .equ __w1_bit=6
  15. #endasm
  16.  
  17. /* Use an 20 alphanumeric LCD connected
  18. to PORTC as follows:
  19. [LCD]   [STK500 PORTC HEADER]
  20. 1 GND- 9  GND
  21. 2 +5V- 10 VCC
  22. 3 VLC- LCD contrast control voltage 0..1V
  23. 4 RS - 1  PC0
  24. 5 RD - 2  PC1
  25. 6 EN - 3  PC2
  26. 11 D4 - 5  PC4
  27. 12 D5 - 6  PC5
  28. 13 D6 - 7  PC6
  29. 14 D7 - 8  PC7
  30. */
  31.  
  32. #asm
  33. .equ __lcd_port=0×15
  34. #endasm
  35.  
  36. #include  // LCD driver routines
  37. #include
  38. #include
  39. #include
  40. #include
  41.  
  42. char lcd_buffer[33];
  43.  
  44. // maximum number of DS1820/DS18S20 connected to the 1 Wire bus
  45. #define MAX_DEVICES 8
  46.  
  47. // DS1820/DS18S20 devices ROM code storage area
  48. unsigned char rom_code[MAX_DEVICES,9];
  49.  
  50. main()
  51. {
  52. unsigned char i,j,devices;
  53. int temp;
  54.  
  55. lcd_init(20);
  56. lcd_putsf(“e-dsp.com Temperature control”);
  57. delay_ms(2000);
  58. lcd_clear();
  59.  
  60. // detect DS1820/DS18S20 devices
  61.  
  62. devices=w1_search(0xf0,rom_code);
  63. delay_ms(2000);
  64.  
  65. // check if devices were found
  66. if (!devices)
  67. while (1); // stop here if no devices were found
  68.  
  69. /* measure and display the temperature(s) */
  70. while (1)
  71. {
  72. for (i=0;i
  73. {
  74. temp=ds1820_temperature_10(&rom_code[i,0]);
  75. sprintf(lcd_buffer,“t%u=%i.%u\xdfC”,++i,temp/10,abs(temp%10));
  76. lcd_clear();
  77. lcd_puts(lcd_buffer);
  78. delay_ms(800);
  79. };
  80. };
  81. }

The programming code I used in this project

The DS1820 working with the ATMega16 and displaying data on the LCD
Startup display

The DS1820 working with the ATMega16, displaying temperature on the LCD
Displaying my room temperature

Our new little project is able to measure temperatures from -55°C to +125°C in 0.5°C increments or Fahrenheit equivalent from -67°F to +257°F in 0.9°F increments. The 0.5°C or 0.9°F increment means you will get temperature readings like 30.5°C or 30°C but never 30.1°C (for the case with Fahrenheit it is different, only 0.9°F increments or decrements).

I thought for a while how to get the best (most precise) temperature reading from an object and came up with the idea of using thermal paste between the object and the DS1820. If you try it let me know how good the results were.

Entry Filed under: Electronics, DIY

64 Comments Add your own

  • 1. Sivro P.  |  |  February 18th, 2006 at 2:38 pm

    Cool keep up good workin…!!!

  • 2. Refik  |  |  February 19th, 2006 at 1:25 pm

    Thank you Sivro, I’ll give my best ;-)

  • 3. Law  |  |  February 19th, 2006 at 1:45 pm

    I’ve tried using thermal paste on my radiator with one of thsoe sensors, works quite well, might even read a bit too high im not sure but deffinately much better than just sticking it on.

  • 4. Joel Simpson  |  |  February 19th, 2006 at 6:09 pm

    I recently added 4 DS18S20s to my server room via this very handy kit that provides a serial interface.
    http://www.electronickits.com/kit/complete/meas/ck110.htm

    and free all-in-one monitoring/logging/graphing software from here http://www.klein.com/thermd/

    Cheap and VERY easy.

  • 5. refikh  |  |  February 21st, 2006 at 12:34 pm

    Great. I am glad somebody else has tried it. Thank you Law.

  • 6. aslam  |  |  February 27th, 2006 at 6:06 pm

    sir, i’m a b.tech 4th year student.

    now i’m doing project in microcontroller based temperature trans mitters.

    i’m using ad595 thermocouple, adc0804, dac0808, microcontroller at89c51 and 2X16 lcd.

    but im unable to make connections.

    plz send me d circuit which consists of all these things connected together to function properly.

    thanking u sir,
    aslam.

  • 7. Jona  |  |  March 2nd, 2006 at 1:58 pm

    How can I connect 16 Dallas sensors to a PC, with the option of selecting one of the sensor at a time? Pls give a hint. Thanks.

  • 8. refikh  |  |  March 6th, 2006 at 5:09 pm

    Dear Aslam,
    I haven’t tried the AD595, ADC0804 and DAC0808, however I am sure if you go to the manufacturers website you will find useful information. Btw. google for it as well! Good luck, let me know how the project ended up. :)

  • 9. refikh  |  |  March 6th, 2006 at 5:16 pm

    Dear Jona,
    if you are from Bucharest, it is really a nice place, 6 months ago I visited it. I’m not sure if it is possible to connect 16 at all, take a look in the datasheet. The hint: You have to read their addresses, the address from each sensor, and call each by its address one at a time. I haven’t tried it with a PC but I guess you want to use it with the parallel port! Did you look for it on google?? Thank you for the comments!

  • 10. William at Myblueroom  |  |  March 7th, 2006 at 1:58 pm

    Question was…
    “How can I connect 16 Dallas sensors to a PC, with the option of selecting one of the sensor at a time?”

    Depending on conditions you can have up to 32 1wire devices on a network running up to 300′ total. Gets a little dicy though, you should not use turbo and should use the checksum.

    Easy way is to use an ibutton adapter (if you don’t feel like building one)

    http://www.maxim-ic.com/products/ibutton/products/adapters.cfm

    I’m working on a multizone thermostat called a Cricket (Sorry it’s PIC based not AVR) the schematics’ on my site at www.myblueroom.com

  • 11. Jona  |  |  March 13th, 2006 at 7:29 am

    Thank you for the ideas. I at least there’s a start. Keep up the good work.

  • 12. deepak  |  |  March 22nd, 2006 at 10:38 am

    dear aslam,
    I am Making a temperature controller for that i’m using ad595 thermocouple, adc0809, microcontroller at89c51 and 2X16 lcd.So logic of our both circuits are same.So kindly please give ur project report to me.pls giv ur email id also.
    by deepak

  • 13. Mirko Ugolini  |  |  March 25th, 2006 at 11:38 am

    Great Site!!!!
    Good Job.

  • 14. refikh  |  |  March 29th, 2006 at 11:00 am

    Sorry guys for not being too helpful these days but it is because of the exams and quizes.

  • 15. Alan  |  |  April 5th, 2006 at 6:49 am

    Nice work! Those sensors are great, it is the same one I used for sensing water temperature in my fish tank :)

    http://www.alan-parekh.com/fish_tank_lights.html

  • 16. Atommann  |  |  April 8th, 2006 at 10:24 pm

    Great!
    I’ll test the project! Thank you!

  • 17. Dave  |  |  April 13th, 2006 at 7:39 am

    Hello,

    I am a student trying to buid a heater that runs off of a 35w halogen light bulb. The thermocouple sensor I am using is a type J. I was wondering if you could offer some advice on how I could regulate the temperature of my light bulb and display the current/set tempertaure via LCD?

    Thanks!
    Dave

  • 18. JOSE AVILES  |  |  May 28th, 2006 at 3:07 am

    HI. WE ARE A COLOMBIAN STUDENTS. WE’RE TRYING TO BUILD A TEMPERATURE CONTROLLER WITH A AT89C51 AND ADC0804.
    DO YOU KNOW HOW TO MAKE THEM WITHOUT LOSE THE SYNCHRONIZATION? HOW CAN I CONNECT IT WITH A LCD 2×16?
    THANKS FOR YOUR HELP?

    EXCUSE OUR ENGLISH, WE’RE SPANISH-SPEAKER.

  • 19. simii  |  |  July 11th, 2006 at 10:36 pm

    hello,
    i m doing a project regarding speed contrl of id fan of boilers so i ‘ve to measure temp n then control the speed of fan by it.
    ur above project is a portion of this one. therfore i need the report of ur project so that i can get the details of all connections because i had never worked on microcntrolller before. plz u can surely help me .
    regards

    simii

  • 20. joe  |  |  July 14th, 2006 at 8:19 pm

    Hello! this dallas maxim chips confuse me to the other most. I am new this chip. where do i start. how do i get code to them. do i build my onw cabling between PC and the breadbroad i am using or do they provide some sort of a development kit for people to experience some other design aspecs. where do i get a compiler for it. I just got a samples max 800LCPE and max7233bfiqh

  • 21. joe  |  |  July 14th, 2006 at 8:24 pm

    i mean is there a site i could buy what i want from or i have to do it the old fashion way. Can you guys tell me how i am really excited about this whole thing.

  • 22. payam  |  |  July 26th, 2006 at 11:46 am

    it was so usefull :-)

  • 23. Quyrzt  |  |  August 10th, 2006 at 8:22 am

    I try to use the LM 335 sensor instead of DS1820, also need the 0809 and AT 89C51, the same result but it’s seem more difficult, need alot of prog. language to change from analog to 8bits

  • 24. embedded-system.net &raqu&hellip  |  |  August 14th, 2006 at 9:13 pm

    […] Interfacing Dallas DS1820 to Atmel AVR ATMega16rnrnhttp://www.e-dsp.com/how-to-measure-temperature-with-the-dallas-maxim-ds1820-sensor/ […]

  • 25. Interfacing Dallas DS1820&hellip  |  |  August 18th, 2006 at 1:54 pm

    […] Interfacing Dallas DS1820 to Atmel AVR ATMega16 http://www.e-dsp.com/how-to-measure-temperature-with-the-dallas-maxim-ds1820-sensor/ […]

  • 26. kerdip  |  |  August 20th, 2006 at 8:33 pm

    Thanks for the codes :)

  • 27. nam  |  |  September 11th, 2006 at 6:00 am

    help about measure and control temperature circuit use AT89C51

  • 28. yus  |  |  September 13th, 2006 at 4:55 pm

    can i use atmega8535 for this sensor…

  • 29. ahmed Waheed  |  |  October 2nd, 2006 at 12:44 pm

    Thanks alot ,it is usefull addition…. How can i use LM 35 with AVR atmega8L ?
    regards,

    Ahmed Waheed

  • 30. gaurav  |  |  November 11th, 2006 at 6:50 pm

    i want to know how can i implement at89c51 IC as a temperature controller can i have ckt for that from your side please help me and reply

  • 31. DS1820 Temperature Sensor&hellip  |  |  November 15th, 2006 at 2:30 am

    […] Interfacing Dallas DS1820 to Atmel AVR ATMega16 http://www.e-dsp.com/how-to-measure-temperature-with-the-dallas-maxim-ds1820-sensor/ […]

  • 32. yogita  |  |  November 24th, 2006 at 9:40 am

    hi i am doing project on graphic lcd interfacing that is depending on on temp change fraph should vary so i am not unable to understand how to interface drivar ic . so can u please help me

  • 33. JaBBa  |  |  December 3rd, 2006 at 2:07 am

    The Dallas DS18S20 (or DS18B20) is more accurate than depicted above!
    Although it’s absolute accuracy is indeed 0.5 degC, it’s relative accuracy does allow you to display temperatures to the first decimal, ie to .1 degC!!
    Yes, it is a bit more fiddly with the scratchpad read off the device, but it yields a lot more accurate measurement (bearing in mind the absolute accuracy).!
    >> If you need better resolution than .5, consult the component’s datasheet!

  • 34. Ahmed Waheed  |  |  December 7th, 2006 at 5:04 pm

    Hi yogita ,
    give me the name of the GLCD u use ,temp sensor and the microcontroller and i will help u.

  • 35. deepak budakia  |  |  January 24th, 2007 at 12:22 pm

    i need to control the speed of fan using microcontroller and temparature sensor… so could you plz send me the details.

  • 36. praveen  |  |  January 28th, 2007 at 3:40 pm

    i need some material &application notes of interfacing temperature sensor with microcontroler
    plz send me

  • 37. Jeff  |  |  February 1st, 2007 at 6:43 pm

    I am trying to see how people are connecting these on to the cat5 cable. I am using hobbyboards hub which gives 6 lines you can run and power to it. I have only 2 sensors at the moment hooked up. They report back to homeseer software.

  • 38. alihossein  |  |  February 7th, 2007 at 1:38 pm

    tankyou

  • 39. ashish  |  |  February 9th, 2007 at 4:36 pm

    i m trying to make a thermometer…
    i wnted to know how to callibrate the readings in the adc register?

  • 40. Elliott Price  |  |  February 15th, 2007 at 11:24 pm

    Can sensor work through die cast metal. At least 2mm in thickness.

    If so can you recomend a source I could speak with?

    We are wanting to use our die cast product to embedd sensor into or underneath to allow for input to a computer device.

  • 41. Hascenn  |  |  February 19th, 2007 at 3:04 pm

    I’m designing a temperature monitoring system.
    I wanted to know how to map the 255 levels of ADC to 0-100 oC Range of temperature in the 89c51’s assembly language code.

  • 42. Josef  |  |  March 10th, 2007 at 2:10 pm

    Perfect example. But just not runing with my stk 500 and my LCD. Can anyone post the full code?

  • 43. Firoz Ahmed  |  |  March 15th, 2007 at 10:50 am

    Dear Sir,
    I try to make digital temperature controller with temperature display by using atmel AT89C51( Software - BASCOM ).
    Please help me

    Firoz

  • 44. D.U.Rathod  |  |  April 3rd, 2007 at 1:40 pm

    Sir,
    i am doing job in elctricity board where we need to measure the temparature of transformer oil and its winding.
    we have oil temp. adn winding temp. indicator in transformer
    but if i want to show temp. of winding and oil in contol room of sub-staion and also i want to interphase that temp with computer so what i have to do
    please give your kind suggestions.
    thanking you.waiting for your immediate responce.

  • 45. M aitzaz ayub  |  |  April 16th, 2007 at 5:47 am

    great description

  • 46. vaishu  |  |  April 19th, 2007 at 3:34 pm

    i am doing a projct on teperature sensor using Lm35,ADC-0809,AT89c51.I am facing some problem in program for ADC interfacing .so please suggest.

  • 47. karanja  |  |  April 25th, 2007 at 2:24 pm

    am unable to program 8085a, adc800,8279 and dispay interfacing for my project in temp measurement.help pliz

  • 48. muna  |  |  April 26th, 2007 at 7:39 am

    sir,i desparately need your help on how program 8085A,ADC800,AND a display interface using 7segment BCD.am just stuck!

  • 49. pasan  |  |  May 23rd, 2007 at 3:31 pm

    Do u know a good free tepmperature project using these

    PIC16F628 or something
    2X16 LCD
    DS18B20 orDS18S20

    I saw a comercial project like this http://aycu35.webshots.com/image/16474/2000196205879273025_rs.jpg

    Thank u

  • 50. Saber Motasadi Zarandi  |  |  May 28th, 2007 at 11:19 am

    thnax

  • 51. new user  |  |  June 8th, 2007 at 9:55 pm

    Hi,
    I am new on programming and I did not understand what you mean by :
    #include // LCD driver routines
    #include
    #include
    #include
    #include
    so code you send me the .c code, thanks alot

  • 52. Brett  |  |  July 13th, 2007 at 7:02 pm

    There is a good arduino description of how to interface on the one wire interface here:
    http://www.arduino.cc/playground/Learning/OneWire

    from previous experience, http://www.phanderson.com/ has some nice serial interface packages for the one-wire system making it easy to communicate with multiple devices without worrying about the low-level stuff

  • 53. Jake n Andika  |  |  July 21st, 2007 at 10:26 am

    Thanks 4 a lot !!!so Great…..

  • 54. Santosh Kumar SIngh  |  |  July 27th, 2007 at 8:12 am

    I am trying to use int0,int1 int2 of atmega8535 for external signal to manupulate data but is not working well.
    can u help me plz

  • 55. Sajeeb Saha  |  |  July 29th, 2007 at 9:03 pm

    Hello,
    I m trying to do a project that will measure the temparature and display it to the computer.

    I need some help regardig this.
    I think u can help me alot regarding this.
    So plz help me.

  • 56. bahar  |  |  August 19th, 2007 at 8:14 am

    hi
    Im student
    I need some help about my project with this title
    (schematic of LM35 sensor connection to AVR & show this on LCD with C_programme )

  • 57. MAHENDRA  |  |  August 31st, 2007 at 7:17 am

    WELL DONE
    REALLY HELPFUL TO CLEAR INTERFACING CONCEPT

  • 58. susu  |  |  September 5th, 2007 at 6:55 pm

    Thank you! Very helpfull!!!
    But where is the difference with the DS18B20?

  • 59. megha  |  |  September 6th, 2007 at 4:07 pm

    sir i hav to make a project in microcontrollers can you please send me a circuit of digital thermometer using lm35 and 8951.. ill be very thankful to you

  • 60. Manh Hai  |  |  September 8th, 2007 at 8:01 am

    Hi!
    Now, I am designing research as AVR, I use IDE codevisionAVR. I don’t method of sprintf function. Can you explain as sprintf(…) funcfion in IDE CodeVisisionAVR? What are (%,’x', ..)? and drift of it.
    Thanks Members. Can you send answer as email for me?

  • 61. ROnics  |  |  September 10th, 2007 at 6:55 pm

    hi i m doing a project on displaying ambient temperature on 7-segment display via AT89s51,ADC0809 and LM35 . can anybody help me to give me some tips..

  • 62. Rajesh  |  |  October 4th, 2007 at 5:02 pm

    Hi,
    I want to use LM35 as a sensor. can you send me the complete discription regards to this?
    I am waiting for your reply.

  • 63. Yen Nhi  |  |  October 6th, 2007 at 8:35 am

    thank u

  • 64. James Webster  |  |  April 13th, 2008 at 12:30 am

    I am try to use a temperature sensor to check
    temperatures in side an grain silo
    I have not had the time to check if this will work,
    any comment
    Regards
    James Webster

Leave a Comment

Required

Required, hidden

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Send to a friend

Send to a Friend:





Send to a friend:

Most Recent Posts