Picture of Temperature with DS18B20
It's a small project about temperature monitoring. I used a DS18B20 digital device to get the temperature. The device uses OneWire protocol. You need only an Arduino board, a DS18B20 and a 4.7KOhm resistor and a breadboard. If you have a display shield for the Arduino that's a plus.
Remove these adsRemove these ads by Signing Up

Step 1: What you have to know about DS18B20

Picture of What you have to know about DS18B20
The device is communicating on one common bus. It means that you can connect several devices. We use only two pins on the device to connect them to the Arduino board. We need two libraries in the sketch: OneWire and DallasTemperature .

So we must include these libraries.

Step 2: Setup

Picture of Setup
We define the communication IO port:
#define ONE_WIRE_BUS 3

We need two object to communicate with the device(s):
OneWire ds(ONE_WIRE_BUS);
DallasTemperature sensors(&ds);

ds object will handle the onewire protocol
sensors object will handle the temperature monitoring devices

I chained 3 devices in this example on a breadboard. On the left side I used a potmeter instead of a 4.7K resistor.

Step 3: Inventory

Picture of Inventory
1st step is to start the sensors:

2nd step is to count the number of sensors on the bus. This function sets the global variable: maxsensors

3rd step is to request the temperatures from the devices:

4th step is to read the temperatures one by one:
for (int i=0;i {
float f = sensors.getTempCByIndex(i);
lcdprint(dtostrf(f, 5, 1, buffer));

lcdprint is a function to display the result on an LCDshield
dtostrf is a function defined in stdio library. it must be included.

Step 4: Source code

Some link for downloading the source codes

Full source code
Dallas Temperature library by Miles Burton
LCD shield library, I used in this project

The LCD shield was ordered from here
herrold12 months ago


This is my first Arduino project and so far have one DS18B20 working as a basic thermostat using a code I found that didn't required the Dallas Temperature Library. However I would love to use three sensors and the display as shown here, the only problem is that I don't seem to be able to find the actual variables that contain the temperature values for each sensor. I will need these to operate various hardware such as fans, actuators and heaters based on a temperature set point and the values for each of the sensors.

Can anyone help with some code to create or access these variables.


mosvath (author)  herrold12 months ago

If you want to use more sensors, then I recommend using the library. ScanSensors will do a loop and read all the sensors on the same wire. It runs from the Setup function, so let's say it collects how many sensors we have. Then we read the individual sensor temperatures from the Loop function. First we send a command to measure the temperature: sensors.requestTemperatures(); After we can re the values by an index: float f = sensors.getTempCByIndex(i);

herrold mosvath12 months ago

Thanks Mark,

I am very new to the C programming language but after some reading I think you have created an array that contains the sensor values and to access each one individually they would be called f[0], f[1] and f[2], is this right?

Unfortunately I haven't been able to test this theory as I am getting multiple errors when I try and compile the sketch. In fact I can't even compile the examples in the onewire and Dallas Temperature library's that came from the link above. The errors from the OneWire library example are to do with "WConstants.h: Nosuch file or directory" and a bunch of variables that "was not declared in this scope" like digitalPinToBitMask and delayMicroseconds etc. The errors from your sketch are pointing to the LCD4bit_mod library file location and also mention the "WConstants.h: Nosuch file or directory" and a bunch of variables that "was not declared in this scope".

Is there likely to be a simple fix for this?

herrold herrold11 months ago

Well I can now see I was a little off the mark. I have found that the individual readings can be accessed by

float temp1 = sensors.getTempCByIndex(0);

float temp2 = sensors.getTempCByIndex(1); etc....

The WConstants.h error can be resolved by removing the #include "WConstants.h" from the .cpp file and adding #include "Arduino.h" to the .h file as per

happyh0ur1 year ago
I found a link:
mosvath (author)  happyh0ur1 year ago

That's cool. I found it in my project folder too :)
if you need it for some reasons:

an older version:


Thanks Mark,

Oddly enough I am having issues with OneWire library. Did you recieve a WConstants.h error when trying to compile? I attached an image of the error and my OneWire.h library.

mosvath (author)  happyh0ur1 year ago

No I didn't get such error message when I used 1.0.4 version of IDE with this library:



Thanks for the code. I get another error now, see attached, and am using it for a sous vide application.

arduino error screenshot.jpg
mosvath (author)  happyh0ur1 year ago

I can still recommend you using Arduino 1.0.4.

That version is working and can be compiled. See attached image.

mosvath (author)  happyh0ur1 year ago

double checked, no compile errors at all. I used this simple source:

#include <OneWire.h>

#include <DallasTemperature.h>

#include <stdio.h>

#define ONE_WIRE_BUS 3

char buffer[25];

byte maxlines = 2;

char line1[16];

char line2[16];

byte x = 0;

byte y = 1;

byte maxsensors = 0;

/* DS18S20 Temperature chip i/o */

OneWire ds(ONE_WIRE_BUS);

DallasTemperature sensors(&ds);

void flashled(void)


digitalWrite(13, HIGH);


digitalWrite(13, LOW);


void scanSensors(void)


byte i;

byte present = 0;

byte data[12];

byte addr[8];

while (



for( i = 0; i < 8; i++) {

Serial.print(addr[i], HEX);

Serial.print(" ");


if ( OneWire::crc8( addr, 7) != addr[7]) {

Serial.print("CRC is not valid!\n");



if ( addr[0] == 0x10) {

Serial.print("Device is a DS18S20 family device.\n");



else {

if (addr[0] == 0x28) {

Serial.print("Device is a DS18B20 family device.\n");



else {

Serial.print("Device is unknown!\n");

Serial.print("Device ID: ");






// The DallasTemperature library can do all this work for you!


ds.write(0x44,1); // start conversion, with parasite power on at the end

delay(1000); // maybe 750ms is enough, maybe not

// we might do a ds.depower() here, but the reset will take care of it.

present = ds.reset();;

ds.write(0xBE); // Read Scratchpad



Serial.print(" ");

for ( i = 0; i < 9; i++) { // we need 9 bytes

data[i] =;

Serial.print(data[i], HEX);

Serial.print(" ");


Serial.print(" CRC=");

Serial.print( OneWire::crc8( data, 8), HEX);




Serial.print("No more addresses.\n");




void setup(void) {


pinMode(13, OUTPUT);

byte i, j;

for (j=0;j<16;j++)


line1[j] = 32;

line2[j] = 32;


line1[16] = 0;

line2[16] = 0;

x = 0;

y = 1;




void loop(void) {

sensors.requestTemperatures(); // minden eszköztől kérjük a hőmérséklet mérést...

for (int i=0;i<maxsensors;i++)



float f = sensors.getTempCByIndex(i); // egyesével kiolvassuk a mért értéket

Serial.print("Sensor ");


Serial.println(dtostrf(f, 6, 2, buffer));



happyh0ur1 year ago
The Dallas Temperature library link shows to be offline on the site. I have looked at a few sites and they all seem to point to this broken link. Is there any other avenue to access this library?
aslan04902 years ago
can this project be used for different types of sensors like temp,press,humidity etc..?
mosvath (author)  aslan04902 years ago
Yes, sure. As it is based on Arduino, many sensors can be used easily. You can start here: