Go Down

Topic: Arduino Mega 2560 + DallasTemperature DS18B20 doesn't work. Please help (Read 4 times) previous topic - next topic


Hi everyone, I have the following setup:

And I have tried every simgle piece of tutorials on the web and no luck the device is never found. I have also read another problems with the Mega and the DS18B20:

But no solution.

The code I use is this:
Code: [Select]
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 12

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

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

int numberOfDevices; // Number of temperature devices found

DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

void setup(void)
 // start serial port
 Serial.println("Dallas Temperature IC Control Library Demo");

 // Start up the library
 // Grab a count of devices on the wire
 numberOfDevices = sensors.getDeviceCount();
 // locate devices on the bus
 Serial.print("Locating devices...");
 Serial.print("Found ");
 Serial.print(numberOfDevices, DEC);
 Serial.println(" devices.");

 // report parasite power requirements
 Serial.print("Parasite power is: ");
 if (sensors.isParasitePowerMode()) Serial.println("ON");
 else Serial.println("OFF");
 // Loop through each device, print out address
 for(int i=0;i<numberOfDevices; i++)
   // Search the wire for address
   if(sensors.getAddress(tempDeviceAddress, i))
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");

Serial.print("Setting resolution to ");

// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);

Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");


// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
 // method 1 - slower
 //Serial.print("Temp C: ");
 //Serial.print(" Temp F: ");
 //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit

 // method 2 - faster
 float tempC = sensors.getTempC(deviceAddress);
 Serial.print("Temp C: ");
 Serial.print(" Temp F: ");
 Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit

void loop(void)
 // call sensors.requestTemperatures() to issue a global temperature
 // request to all devices on the bus
 Serial.print("Requesting temperatures...");
 sensors.requestTemperatures(); // Send the command to get temperatures
 // Loop through each device, print out temperature data
 for(int i=0;i<numberOfDevices; i++)
   // Search the wire for address
   if(sensors.getAddress(tempDeviceAddress, i))
// Output the device ID
Serial.print("Temperature for device: ");

// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
//else ghost device! Check your power requirements and cabling


// function to print a device address
void printAddress(DeviceAddress deviceAddress)
 for (uint8_t i = 0; i < 8; i++)
   if (deviceAddress[i] < 16) Serial.print("0");
   Serial.print(deviceAddress[i], HEX);

Bu I allways get:
[quote]Dallas Temperature IC Control Library Demo
Locating devices...Found 0 devices.
Parasite power is: OFF
Requesting temperatures...DONE
Requesting temperatures...DONE

If anyone could help I be very greatfull.


Hi, I also tried the one wire library, the latest version and it doesn't detect the adress of the Sensor:
Code: [Select]
#include <OneWire.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
// http://www.pjrc.com/teensy/td_libs_OneWire.html
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire  ds(6);  // on pin 10

void setup(void) {

void loop(void) {
 byte i;
 byte present = 0;
 byte type_s;
 byte data[12];
 byte addr[8];
 float celsius, fahrenheit;
 if ( !ds.search(addr)) {
   Serial.println("No more addresses.");
 Serial.print("ROM =");
 for( i = 0; i < 8; i++) {
   Serial.write(' ');
   Serial.print(addr[i], HEX);

 if (OneWire::crc8(addr, 7) != addr[7]) {
     Serial.println("CRC is not valid!");

 // the first ROM byte indicates which chip
 switch (addr[0]) {
   case 0x10:
     Serial.println("  Chip = DS18S20");  // or old DS1820
     type_s = 1;
   case 0x28:
     Serial.println("  Chip = DS18B20");
     type_s = 0;
   case 0x22:
     Serial.println("  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

 Serial.print("  Data = ");
 Serial.print(" ");
 for ( i = 0; i < 9; i++) {           // we need 9 bytes
   data[i] = ds.read();
   Serial.print(data[i], HEX);
   Serial.print(" ");
 Serial.print(" CRC=");
 Serial.print(OneWire::crc8(data, 8), HEX);

 // convert the data to actual temperature

 unsigned int 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];
 } else {
   byte cfg = (data[4] & 0x60);
   if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
   else if (cfg == 0x20) raw = raw << 2; // 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;
 fahrenheit = celsius * 1.8 + 32.0;
 Serial.print("  Temperature = ");
 Serial.print(" Celsius, ");
 Serial.println(" Fahrenheit");

and output:
No more addresses.


Don't use quotes for your code. Use the code tag button labelled '#' immediately to the left of the quote button.



Is the pin number correct?  You are using a shield and the pinnumbers may be incorrect (test a LED on pin 12 will do)

Does the DS18 get the proper voltage? what can you measure on the little board?

Is it getting hot ?   => I found out that temp sensors work "reverse" when connected wrong, they radiate heat iso measuring it  :)

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


Using a MEGA2560, updated libraries and Arduino IDE 1.0...  here is what I did:

I copied the code from your post (1st one) and installed a DS1820 on a breadboard.  Installed 4.7K to signal pin from 5V. Powered the sensor from 5V (no parasitic). Used the same pins. Uploaded your code unchanged and opened serial monitor.


Code: [Select]
Locating devices...Found 1 devices.
Parasite power is: OFF
Found device 0 with address: 10F0C32500000014
Setting resolution to 9
Resolution actually set to: 9
Requesting temperatures...DONE
Temperature for device: 0
Temp C: 23.76 Temp F: 74.77
Requesting temperatures...DONE
Temperature for device: 0
Temp C: 23.75 Temp F: 74.75

So it's hard to blame the code from my perspective.


Go Up