Arduino Feeding With Temperature From DS18B20

About: Focussing currently on supplying open source software to the masses. I supply linux CDs and DVDs across Europe, as well as mirror various open source projects. Currently I mirror http://rsyn...

This instructable will explain how to:
* connect a DS18B20 temperature sensor to an arduino
* read from the sensor using the 1-wire interface
* send that data to directly from the arduino
* view the resulting graph on the internet is an Internet of Things host that generates graphs based on data you provide. In this example I am going to use it to monitor the temperature in my room. It's not a hackerspace, my wife keeps it far too tidy for that.

Step 1: Parts List

* Arduino with ethernet shield
I have an arduino Uno clone and a wiznet 5100 shield.
* Power supply (it can be powered via the USB port)
I was fortunate that my router has USB ports that provide the 5v output meaning the arduino  is powered whenever the router is and saves a plug, electricity, the environment and the  future of the world.
* DS18B20 temperature sensor
* RJ45 network cable connected between the arduino and your router

Step 2: Schematic

* Arduino gnd to DS18B20 both outer legs
* Arduino 5v to 4.7k resistor leg 1
* 4.7k resistor leg 2 to DS18B20 middle leg
* Arduino digital pin 2 to DS18B20 middle leg
* RJ45 cable from Arduino to router
* Power to Arduino (via USB or barrel jack, I use USB from my router)

Step 3: Prerequisites

Three libraries are required for the arduino:
onewire library from (specifically
httpclient library from (
Xively library from (
install these libraries in your arduino/library directory.

An account with (formerly and
Sign up for a free developer account at
Pick a username, password, set your address and timezone etc. You will receive a confirmation email, click the activation link to avtivate your account.
You can choose to take the test drive to learn about xively or skip it, this is completely up to you.
Doing the test drive? I'll wait
- \ | / - \ | /
Done? Let's continue

Step 4: Add a New Device to Xively

You should now be on the Development Devices page of xively. This is where the fun starts.

Click on +Add Device

Give the new device a name eg Arduino DS18B20 temperature logger

Fill in a description about the device, its location such as My Hackerspace, and yourself if you like. I tend to add a link to my web site here although there is a dedicated web address area later in the instructions.

Choose whether the data is public or private. Do you really want the world knowing how hot your hackerspace is? I choose public unless it is something like an alarm which I keep private.

Click on the Add Device button at the bottom of the page

Step 5: The Xively Interface for Your New Device, and All the Access Codes

You will now be on a page with a lot of information. Don't be overwhelmed – it is mainly set and forget stuff.

The page is broken up into 8 parts:
* product ID, secret, serial number, activation code
* status (Activated), feed ID, feed URL (this shows the data on a graph), API endpoint
* “Add channels to your device!” - the arduino will handle this in the sketch
* request log – this is for debugging and confirming everything is working from the arduino side
* location – the geographical location of the sensor (optional)
* api keys – we will need this later in the arduino sketch
* metadata – can be edited
* triggers – ping a web page when something happens (eg temperature drops below freezing)

Step 6: Arduino Sketch

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>
#include <OneWire.h>

// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Your Xively key to let you upload data
char xivelyKey[] = ""; // enter the key, under API Keys
unsigned long feedId = 0; // enter your feed ID, under Activated
int frequency = 15000; // delay between updates (milliseconds)

// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;

// Define the strings for our datastream IDs
char sensorId[] = "temp";
XivelyDatastream datastreams[] = {
  XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
// Finally, wrap the datastreams into a feed
XivelyFeed feed(feedId, datastreams, 1 /* number of datastreams */);

EthernetClient client;
XivelyClient xivelyclient(client);

// initialize the one-wire interface
OneWire ds(sensorPin);  // on pin 2 (a 4.7K resistor is necessary)
5v - 4.7k resistor - 18B20 middle pin - D2
gnd - 18B20 both legs (joined together)

void setup() {
  // put your setup code here, to run once:
  Serial.println("Starting single datastream upload to Xively...");

  while (Ethernet.begin(mac) != 1)
    Serial.println("Error getting IP address via DHCP, trying again...");
  Serial.print("IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++)
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);

void loop() {
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
int i;

if ( !
  Serial.println("No more addresses.");
if (OneWire::crc8(addr, 7) != addr[7])
  Serial.print("CRC is not valid!");
// the first ROM byte indicates which chip
switch (addr[0])
  case 0x10:
   Serial.print("  Chip = DS18S20");  // or old DS1820
   type_s = 1;
  case 0x28:
   Serial.print("  Chip = DS18B20");
   type_s = 0;
  case 0x22:
   Serial.print("  Chip = DS1822");
   type_s = 0;
   Serial.println("Device is not a DS18x20 family device.");
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

for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] =;
int16_t raw = (data[1] << 8) | data[0];
if (type_s)
  raw = raw << 3; // 9 bit resolution default
  if (data[7] == 0x10)
   // "count remain" gives full 12 bit resolution
   raw = (raw & 0xFFF0) + 12 - data[6];
  byte cfg = (data[4] & 0x60);
  // at lower res, the low bits are undefined, so let's zero them
  if (cfg == 0x00)
   raw = raw & ~7;  // 9 bit resolution, 93.75 ms
  else if (cfg == 0x20)
   raw = raw & ~3; // 10 bit res, 187.5 ms
  else if (cfg == 0x40)
   raw = raw & ~1; // 11 bit res, 375 ms
  //// default is 12 bit resolution, 750 ms conversion time
celsius = (float)raw / 16.0;
        Serial.print("Current temperature: ");
        Serial.println(" celsius");
  Serial.println("Uploading it to Xively");
  int ret = xivelyclient.put(feed, xivelyKey);
  Serial.print("xivelyclient.put returned ");


Step 7: Welcome to the Internet of Things!

Once you have uploaded the code to the Arduino you should start to see information appear in the Request Log section and datapoints appear on the graph.

Congratulations, you are now updating the world with the temperature in your hackerspace :)

What's next? That's up to you.

How many times of day does your front door open?
What is the water level of your begonias?
Is your washing machine finished?
Is the coffee machine empty?
How loud are the kids playing music while you are out?

There are millions of possibilities for monitoring, just take a look around you. Does it do something? Then there must be a way to monitor it.

Here for the world to see is the temperature in my hackerspace<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs> living room:

Currently it is 26.5 degrees C, and it is supposed to be autumn.

Step 8:

I have not tested this code yet (but it does compile at least!)

This version should create temp_nnnnnnnnnnnn for each individual DS sensor and upload the data to xively.

Hopefully it will make the code multiple sensor friendly.




    • Organization Contest

      Organization Contest
    • Build a Tool Contest

      Build a Tool Contest
    • Tape Contest

      Tape Contest

    41 Discussions


    I apologise, I forgot to do the actual reading of the chip in the above code :P

    You need to add the following

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


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

    1 reply

    4 years ago on Introduction

    Hi. is there a way to give their own names sensors (data streams)? Eg. Kitchen, living room, etc. I have 6 sensors.

    3 replies

    The lines to change are
    <code>// Define the strings for our datastream IDs

    char sensorId[] = "temp";</code>
    specifically, change "temp" to each of your device names eg "garage temp"


    Reply 3 years ago

    I need to call each of the sensors connected to a single Arduino otherwise. Eg. Kitchen, garage, lounge.


    I have been thinking about this for sensor 1 = kitchen, sensor 2 = living room and so on. Where to enter the name of the second third and subsequent sensors? Everyone must have a different name.


    3 years ago on Introduction

    This setup will prevent you from using the capabilities of this perfectly designed IC. That is the adressing multiple IC's (temperature readouts) on the same line.


    4 years ago on Introduction

    I want to make circuit b using 4 sensors and 4 single resistor sensor connections print.4 sensor enough for my seven-inch screen. Is there another Arduino, or need


    Reply 4 years ago on Introduction

    for multiple sensors in the end of the code after Serial.println("Uploading it to Xively");


    datastreams[0] = {
    XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),


    XivelyDatastream datastreams[0] = XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT);

    Worked for me, hope that helps.


    Reply 4 years ago on Introduction

    slightly corrected and in my code runs on multiple sensors.

    I just have a problem with the naming of streams. What I mean is that the individual data struienia zwoimi call names and not the serial numbers of sensors.


    Reply 4 years ago on Introduction


    May I ask what you did to get multiple sensors working? I'd like to add additional temp probes but haven't been having much luck.


    5 years ago on Introduction


    Serial.println("Uploading it to Xively");
    datastreams[0] = {
    XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),

    • ------------------------
    sketch_feb13d.ino: In function 'void loop()':

    sketch_feb13d:178: error: expected primary-expression before '{' token
    sketch_feb13d:178: error: expected `;' before '{' token

    2 replies

    Reply 5 years ago on Introduction

    There is a missing after the , in that line. I will have a look shortly and get b back to you.


    4 years ago on Introduction


    Please give solution...

    IP address:

    Chip = DS18B20

    Current temperature: 33.13 celsius

    Uploading it to Xively

    xivelyclient.put returned -3


    4 years ago on Introduction

    Greg; Hi. As with rparkera and sidbin below, I have the same problem with the multiple sources sketch that they have.

    I get the following errors:

    Multiple_Xively.ino: In function 'void loop()':

    Multiple_Xively:179: error: expected primary-expression before '{' token

    Multiple_Xively:179: error: expected `;' before '{' token

    Multiple_Xively:188: error: expected `}' at end of input

    You were going to get back to sidbin, but don't seem to have done so.

    And for the record, I have only just started using Arduino as it's the most practical solution (I think) to a long running temperature monitoring problem I have.



    5 years ago on Introduction

    Hi there - great instructable. Many thanks

    Did you ever get back on sidbin's query? I have exactly the same problem. The single datastream works fine - but I can't get the multiple sources working. Help!