DS18B20

Hello,
I need to connect 2x DS18B20 temperature sensors to the arduino mega board, but I don´t now, how I make it, because I want to put the first sensor to the outside a the second sensor to the inside a I want to show it on the display. Thanks

This tutorial

Nick_Pyner thank you very much, this codes:
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 };
wll this codes equal with my dallas sensors?

No. Each DS18B20 has its own address - those are the addresses of the actual physical devices that were used by the people who developed the tutorial.

You'll have to find the addresses of your own devices (or address them by index). Take a look at the examples that come with the DallasTemperature library to see how to do this.

Check the tutorial I mentioned. You will see there are TWO programmes. One sniffs the addresses, and the other uses them. I found this the best way to go. A very basic example is here. It is derived from the Hacktronics example.

/* Basic 2xDS18B20 code for serial monitor, bluetooth, Excel or w.h.y.
Derived from Hacktronics. USE THEIR ADDRESS SNIFFER and substitute your 
numbers. Use Hacktronics connections diagram. 
http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html
Stay away from using parasite power
-127C means bad connection
85 means you haven't gotten a read yet, probably just the 
wrong order of commands
*/

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

// Data wires are 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);
  
byte Thermo1[8] = {0x28, 0x39, 0xFD, 0x50, 0x04, 0x00, 0x00, 0X69};
byte Thermo2[8] = {0x28, 0x09, 0xA9, 0xC0, 0x03, 0x00, 0x00, 0x95};
float tempC,Temp1,Temp2;  

void setup(){
  Serial.begin(9600);
  sensors.begin();
  delay(500);//Wait for newly restarted system to stabilize
  sensors.setResolution(Thermo1, 12); 
  sensors.setResolution(Thermo2, 12);
}

void loop() {
 sensors.requestTemperatures();  // call readings from the addresses
  Temp1 = sensorValue(Thermo1);
  Temp2 = sensorValue(Thermo2);  

Serial.print("      Temp1 = ");
Serial.print(Temp1);
Serial.print("      Temp2 = "); 
Serial.println(Temp2);
delay(1000);
}

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  tempC = sensors.getTempC (deviceAddress);
  return tempC;
}

Thanks very much. :smiley:

And how much sensors I can put in this circuit on one wire?

quite a lot, seen threads about at least 20

hmm I practise this circuit-I founding the address and when I start the arduino, write on the monitor a address and if I push the reset button on the arduino and it write the new address from this same sensor, because wildbill write:Each DS18B20 has its own address. Why this address changing?

for example: this is the same sensor:

Found '1-Wire' device with address:

0x49, 0x22, 0x92, 0x24, 0x21, 0x89, 0x24, 0x91CRC is not valid!
Looking for 1-Wire devices...

Found '1-Wire' device with address:

0x89, 0x24, 0x42, 0x48, 0x92, 0x24, 0x49, 0x12CRC is not valid!
Looking for 1-Wire devices...

The address is changing because you are probably doing some thing wrong.

Please post the code you are using, and how you have the sensor connected. Are you using the 4.7K pull up resistor on the input line?

The first byte to the address for a DS18B20 should be 0x28. The invalid CRC is also indicating a problem.

Connect one sensors at a time, write the address down with a pencil, and press reset. I think you have several connected. I didn't realise you could do it with several and it simply means that it displays the first it sees at random. This means you will never know which one is which.

it displays the first it sees at random

No, it doesn't. It always displays them in numerical order of address except that it reverses the bits in each byte - i.e. it in effect treats the low order bit as the high order bit etc.
See my message in this thread: DS1822: How does the program assign the sensor's index? - Programming Questions - Arduino Forum

Pete

thanks but the ROM address is the address, which I must write to the "chip address" , but I must write before the each ROM number 0x, for example: ROM: 28 D4 C9 0 4 0 0 49
I change it: 0x28 0xD4 0xC9 0x00 0x04 0x00 0x00 0x49
because I know one code from the http://www.linuxsoft.cz/article.php?id_article=1990 and it is from the "one wire" library, and it is: #include <OneWire.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// OneWire Arduino Library, connecting 1-wire devices (DS18S20, etc) to Teensy
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary)

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*, 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 = ds.read();_
    _ Serial.print(data*, HEX);
    Serial.print(" ");
    }
    Serial.print(" CRC=");
    Serial.print(OneWire::crc8(data, 8), HEX);
    Serial.println();
    // Convert the data to actual temperature*
    * // because the result is a 16 bit signed integer, it should*
    * // be stored to an "int16_t" type, which is always 16 bits*
    * // even when compiled on a 32 bit processor._
    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];
    }
    } else {
    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;
    fahrenheit = celsius * 1.8 + 32.0;
    Serial.print(" Temperature = ");
    Serial.print(celsius);
    Serial.print(" Celsius, ");
    Serial.print(fahrenheit);
    Serial.println(" Fahrenheit");
    }*_

MRFH:
thanks but the ROM address is the address, which I must write to the "chip address" , but I must write before the each ROM number 0x, for example: ROM: 28 D4 C9 0 4 0 0 49
I change it: 0x28 0xD4 0xC9 0x00 0x04 0x00 0x00 0x49
because I know one code from the http://www.linuxsoft.cz/article.php?id_article=1990

I'm not sure what you want to do, or are trying to say. I submit the only line in the above code that has any significance is

// The DallasTemperature library can do all this work for you!

Virtually every other line is outdated junk or, at best, redundant. The example in reply #4 is all you need, and counting the number of relevant lines of code therein might be enlightening.

MOderator: please modify your post and add [code]...[/code] tags

Thanks, the sensors are wrong, my mistake, thanks,