Home ArduinoArduino Tutorials Arduino and DS18B20 example

Arduino and DS18B20 example

The DS18B20 is a nice little temperature sensor that can be used in various simple projects.  This part uses the 1 wire bus and you can connect multiple sensors up to your Arduino.

The part is also relatively low cost and only requires a 4k7 pull up resistor. In the example below we shall make a basic example that reads the temperature and outputs via serial and can be verified using the serial monitor in the Arduino IDE.

Lets look at the parts list and the the parts laid out on a mini breadboard which sits on an arduino protoboard shield, as you can see 2 parts and some hook up wire.

Parts List

Label Part Type Properties
DS1 DS18B20 1-Wire Temperature Sensor part # DS18B20
Part1 Arduino Uno (Rev3) type Arduino UNO (Rev3)
R1 4.7k Ω Resistor package THT; tolerance ±5%; bands 4; resistance 4.7kΩ; pin spacing 400 mil
ds18b20 breadboard

ds18b20 breadboard

Layout

 

ds1820 layout

ds1820 layout

Coding

You need to download 2 libraries and copy them into your Arduino libraries folder, this makes life easier.

“DallasTemperature” Library
“1-Wire” Library

Now copy and paste this into a new sketch

 

#include <OneWire.h>
#include <DallasTemperature.h>

//the pin you connect the ds18b20 to
#define DS18B20 2

OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);

void setup()
{
Serial.begin(9600);
delay(1000);
//start reading
sensors.begin();
}

void loop()
{
//read temperature and output via serial
sensors.requestTemperatures();
Serial.print(sensors.getTempCByIndex(0));
Serial.println(” degrees C”);
}

 

When you upload the sketch, go to Tools -> Serial Monitor and you should see something like the following. Put your finger on the DS18B20 and you should see the temperature vary.

 

ds18b20 output

ds18b20 output

Links

I’m seeing some great prices for DS18B20 parts, 10 of these for about $7.

Buy DS18B20 from Amazon UK

 

Buy DS18B20 from Amazon US

You may also like