Arduino Mega 2560 + DallasTemperature DS18B20 doesn't work. Please help

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:
http://forum.bildr.org/viewtopic.php?f=29&p=5471

But no solution.

The code I use is this:

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

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

// 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.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
  
  // 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: ");
		printAddress(tempDeviceAddress);
		Serial.println();
		
		Serial.print("Setting resolution to ");
		Serial.println(TEMPERATURE_PRECISION,DEC);
		
		// 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.println();
	}else{
		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(sensors.getTempC(deviceAddress));
  //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(tempC);
  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
  Serial.println("DONE");
  
  
  // 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: ");
		Serial.println(i,DEC);
		
		// 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);
  }
}[/quote]


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.
Greetings.

Hi, I also tried the one wire library, the latest version and it doesn't detect the adress of the Sensor:

#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) {
  Serial.begin(9600);
}


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.println();
    ds.reset_search();
    delay(250);
    return;
  }
  
  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!");
      return;
  }
  Serial.println();
 
  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  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.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present,HEX);
  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);
  Serial.println();

  // 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.print(" Celsius, ");
  Serial.print(fahrenheit);
  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.

Pete

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 :slight_smile:

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.

Results:

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.

Installed 4.7K to signal pin from 5V.

Thanks.

Why i cant change resolution ?

Locating devices...Found 1 devices.
Parasite power is: OFF
Found device 0 with address: 10475EC402080032
Setting resolution to 9
Resolution actually set to: 12
Requesting temperatures...DONE
Temperature for device: 0
Temp C: 29.81 Temp F: 85.66

i try with two sensors but same

Who would ever know the answer to that? but a guess is that you are not sending the required instruction.

Something like

  sensors.setResolution(InThermo, 10);

No instruction means default to 12 bit. You might also get some sensible information from

note that there are two programmes to use, one to sniff the address and the other to use the sensor.

theenggprojects:
You should check this one out

I guess it works OK with one sensor, but I have never understood how, if you use more than one, you can tell which is which.

Nick_Pyner:
I guess it works OK with one sensor, but I have never understood how, if you use more than one, you can tell which is which.

Those sensors are typically used on I2C bus and each sensor has a unique address that you can access by address. I used these in a heater project and had a sensor in each room on the same bus. It worked fine. There is sample code that searches I2C bus and tells you the address of the attached device.

What was alluding to is the "by index" polling in the above link. Sniffing the address leaves no doubt about which sensor is where, and that is the method I use. None of this has anything to do with I2C.

@Nick
If you know the addresses of each DS18B20 (you have to anyway), then you can figure out what order they will be found on the bus. As I explain in DS1822: How does the program assign the sensor's index? - Programming Questions - Arduino Forum, when the ds18b20 library enumerates the devices on the bus it finds them in numerical order. It searches through the addresses from high order byte (leftmost) to low order. BUT when it checks each byte, it does so starting with the low order bit first. This is why they don't seem to be in numerical order if you list them by index, but actually they are if you reverse the bits in each byte.
So, if you have the addresses of all the ds18b20 on a bus, you can figure out what order they will be found. Knowing this, you can assign them to specific locations based on the order that they will be found on the bus.
Since I don't use the alarms, I found it easier to write an ASCII label for each one into one of the alarm registers and use that as the ID. This is useful if one dies because you can just get a new one, label it, and put it on the bus, reboot and the code will still say, for example, that sensor B is in the dining room. No worries about whether the new sensor is in the right numerical order.

Pete

el_supremo:
@Nick
If you know the addresses of each DS18B20 (you have to anyway),.....
......when the ds18b20 library enumerates the devices on the bus it finds them in numerical order.

No worries about whether the new sensor is in the right numerical order.

OK Thanks. I now recall seeing your previous.

Though I never understood it, and DS18B20s don't die too often, it seems there never was much point in using the by "index system".

I always got the impression that those using "by index" were just playing around with DS18B20s and had no practical application - hence the lack of a decent explanation. I guess it wasn't helped by the fact that those who use the addresses in the code don't really need to understand "by index" anyway.