Thursday, October 02, 2014

How it works: DS18B20 and Arduino

by / Monday, 05 August 2013 / Published in Arduino

DS18B20There are many Arduinods18b20 sketches on the Web that provide sample code how to work with MAXIM’s (formerly DALLAS) DS18B20\DS18S20 digital temperature sensors. However, most of them do not describe the process in details. In this article, I’m going to show the gears that make the whole thing work.

As you should know, DS18x20 family members are relatively accurate digital temperature sensors. They use MAXIM’s 1-wire bus protocol, which requires only 1 wire for receiving and transmitting data (A ground line is also required). Moreover, DS18x20 support so-called parasite power mode, when they drain energy from data bus when it’s high – to charge sensor’s integrated capacitors that will be used as a power source. Sure, they also support normal power scheme, with external supply (and this scheme is used in this article).

 

power1

You can have up to 127 devices on 1-Wire line. To use one of them, a master device initiates a reset pulse, and them receives presence pulse from slave devices. Then he searches ROMs (each produced device has a unique 64-bit ROM code, like serial number. Tiny first part of it defines a family of device, like 0x10 for DS18S20, 0x28 for DS18B20, 0x01 for iButton-DS1990A etc., and all remaining bits are a unique item number. Finally, master device selects the necessary slave device with MATCH ROM [55h] command. (Or transmits broadcast commands).

By the way: to us all data transfer on 1-wire bus seems to be organized in bytes. Since OneWire library for Arduino takes charge of all low-level operations, we just send and receive bytes.

All data and commands are transmitted least significant bit first over the 1-Wire bus.

To get a temperature measurement, we need to issue CONVERT [44h] command. When our sensors receive it, they will initiate data conversion process  - to produce 2 bytes with the measurement. This is relatively slow process, and it can take up to 750 milliseconds, so we have to wait for some time after issuing the command.

All that measurements are stored in a so-called scratchpad – a piece of sensor’s RAM. We can read it – to get the data, and we can also write to it – to set alarm thresholds, to specify sensor resolution (but it’s more advanced topic, and it will not be discussed in this article).

scratchpad

To read a scratchpad, we issue a READ SCRATCHPAD [BEh] command. After that, we should receive 9 bytes of data (see figure above).

Then we can get a temperature according to the following formulae:

Temp =  ((HighByte << 8) + LowByte ) *0.0625

temprg temp

A multiplier of 0.0625 is a conversion coefficient between sensor’s internal values and real temperature – according to 12-bit resolution, each “tick” on sensor’s scale stands for 0.0625 °C.

(do not forget that Temp should be float variable)

To sum up all above, let us write a function that stores the temperature in a global variable, and return true of false depending whether measurement was successful or not:

#define DS18S20_ID 0x10
 #define DS18B20_ID 0x28
 float temp;
 boolean getTemperature(){
 byte i;
 byte present = 0;
 byte data[12];
 byte addr[8];
 //find a device
 if (!ds.search(addr)) {
 ds.reset_search();
 return false;
 }
 if (OneWire::crc8( addr, 7) != addr[7]) {
 return false;
 }
 if (addr[0] != DS18S20_ID && addr[0] != DS18B20_ID) {
 return false;
 }
 ds.reset();
 ds.select(addr);
 // Start conversion
 ds.write(0x44, 1);
 // Wait some time...
 delay(850);
 present = ds.reset();
 ds.select(addr);
 // Issue Read scratchpad command
 ds.write(0xBE);
 // Receive 9 bytes
 for ( i = 0; i < 9; i++) {
 data[i] = ds.read();
 }
 // Calculate temperature value
 temp = ( (data[1] << 8) + data[0] )*0.0625;
 return true;
 }
 

 

If there are several sensors in the scheme, this function will give values from each of them in cycles.

Do not forget also to create OneWire object at the beginning of the program:

#include <OneWire.h>
OneWire ds(pin_number);

(And do not forget to download the library and put it to necessary folder)

And here is small circuit schematic for getting started with digital temperature sensors:

ds18b20 bb

Useful info:

commands

Read 4798 times Last modified on Monday, 05 August 2013 17:52

Site still under construction

I will add more sections in the future

  • My works / Photogallery
  • More Joomla!-related articles
  • Personal blog
  • Arduino articles from the old site (done)

Latest Posts

Dealing with nginx “The plain HTTP request was sent to HTTPS port” error

Dealing with nginx “The plain HTTP reque…

In some cases you have to run multiple services on... Read more

21-09-2014 in Linux/BSD

Getting Argyll/dispcalGui working under Windows 8.1 64-bit

Getting Argyll/dispcalGui working under …

If you upgrade to windows 8.1 64-bit, you may find... Read more

03-01-2014 in Blog

How to transfer or mirror your Joomla! / Wordpress / Drupal website from one server to another in one click

How to transfer or mirror your Joomla! /…

The following script may help you to transfer or mirror... Read more

13-10-2013 in Linux/BSD

Setting up your multi-user hosting VPS server (Apache): A secure way to run several Joomla/Worpress/Drupal sites

Setting up your multi-user hosting VPS s…

Keywords: Apache MPM ITK PHP MySQL VPS CentOS In this article... Read more

13-10-2013 in Linux/BSD

How it works: DS18B20 and Arduino

How it works: DS18B20 and Arduino

There are many Arduino sketches on the Web that provide... Read more

05-08-2013 in Arduino

Premium Joomla templates

Premium Joomla templates

Two years ago I discovered a site with tons of... Read more

27-03-2013 in Recommendations

Interfacing HH10D with Arduino

Interfacing HH10D with Arduino

HH10D is capacitive-type relative air humidity sensor. It is cheap... Read more

17-06-2012 in Arduino

Arduino and watchdog timer

Arduino and watchdog timer

Have you ever encountered situations when your Arduino-based device... Read more

16-06-2012 in Arduino

Measuring frequency with Arduino

Measuring frequency with Arduino

There are many signals that contain necessary information in frequency.... Read more

02-03-2011 in Arduino

Reading iButton with Arduino

Reading iButton with Arduino

We all are familiar with iButton devices - a small... Read more

03-12-2010 in Arduino

Prev Next


Scroll to top