DS18B20 problems

I have not been able to get the DS18B20 temperature sensor to work. I would appreciate any help. I have tried everything I could find on here and the playground, as well as google.

// This sketch looks for 1-wire devices and
// prints their addresses (serial number) to
// the UART, in a format that is useful in Arduino sketches
// Tutorial: 
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

#include <OneWire.h>

OneWire  ds(3);  // Connect your 1-wire device to pin 3

void setup(void) {

void discoverOneWireDevices(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];
  Serial.print("Looking for 1-Wire devices...\n\r");
  while(ds.search(addr)) {
    Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");
    for( i = 0; i < 8; i++) {
      if (addr[i] < 16) {
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
    if ( OneWire::crc8( addr, 7) != addr[7]) {
        Serial.print("CRC is not valid!\n");
  Serial.print("\n\r\n\rThat's it.\r\n");

void loop(void) {
  // nothing to see here

Whenever I try this to locate the address I get the following error" ‘OneWire’ " does not name a type

Using this code,

//  This Arduino sketch reads DS18B20 "1-Wire" digital
//  temperature sensors.
//  Copyright (c) 2010 Mark McComb, hacktronics LLC
//  License: http://www.opensource.org/licenses/mit-license.php (Go crazy)
//  Tutorial:
//  http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

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

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress insideThermometer = { 0x28, 0x94, 0xE2, 0xDF, 0x02, 0x00, 0x00, 0xFE };
DeviceAddress outsideThermometer = { 0x28, 0x6B, 0xDF, 0xDF, 0x02, 0x00, 0x00, 0xC0 };
DeviceAddress dogHouseThermometer = { 0x28, 0x59, 0xBE, 0xDF, 0x02, 0x00, 0x00, 0x9F };

void setup(void)
  // start serial port
  // Start up the library
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(insideThermometer, 10);
  sensors.setResolution(outsideThermometer, 10);
  sensors.setResolution(dogHouseThermometer, 10);

void printTemperature(DeviceAddress deviceAddress)
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(" F: ");

void loop(void)
  Serial.print("Getting temperatures...\n\r");
  Serial.print("Inside temperature is: ");
  Serial.print("Outside temperature is: ");
  Serial.print("Dog House temperature is: ");

results in this error “variable or field ‘printTemperature’ declared void” Someone please advise me how to correct these problems and get this to work

Have you downloaded, and correctly installed, the OneWire and DallasTemperature libraries, as linked on http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html ? Having done that, the sketches compile fine for me.

And libraries have to go in your libraries directory...

Thanks guys I finally got it

Care to share what your actual problem was - might help other people in the future searching these forums with similar problem...

It actually turned out to be a hardware issue. I replaced the sensor and everything worked the first time

I had 4 rough weeks figuring out why I get bad readings from my DS18B20. I followed almost every recommendation I could get from Internet: I played with pull-up resistors, played with timing constants in OneWire.cpp. Some recommended CAT5 cables, some called for magic resistors, etc. I am in a smart house project using Mega Board, a lot of lights and switches attached and my cables are from the alarm systems - simple and unshielded.

At the end of the day, I did a careful study from the data protocol author Maxim (ex Dallas). https://www.maximintegrated.com/en/app-notes/index.mvp/id/148

Basically my case was solved by adding a 100 Ohm resistor between Arduino data pin and the the date line and 470pF capacitor from the data line to the ground. This RC scheme is what Maxim recommends for medium-length lines with weights up to 200m

And it works perfectly for me and I hope for you too!