Brick-Temperature-DS18B20

From ArduinoInfo
Jump to navigation Jump to search

Temperature Sensor DS18B20

(See Thermocouples for high temperatures HERE)
DS18B20-brick.jpg Detail 134 ds18b20-3.jpg TemperSensorSStube-DS18B20-1.jpg

Electronic Brick, Waterproof and other Versions

NOTE: There are different-appearing versions of the DS18B20 electronic temperature sensor but they work the same. This is an electronic thermometer which has high accuracy over a wide range (accurate to ±0.5°C over the range of -10°C to +85°C) (Workable from -55°C to +125°C).

You can locate these thermometer chips up to 100M away from your Arduino. Shorter cables can be just 2 wires. NOTE: There must be a pullup resistor of about 5K in all cases, but the Brick versions have this included.

Multiple thermometers can be connected on the same wire because every one has it's own internal address. This is the "1-wire" bus system(Wikipedia). Below is an example of connecting up to 5 DS18B20's on a single Arduino pin. This requires that you know the internal address of each sensor. A utility sketch to find the address is also below.

Here (above, right) is what the individual chip sensors look like, and their pinout (below):

DS18B20-pinout.JPG
DS18B20-Wiring.jpg


Links to more about the DS18B20 and other "1-Wire" chips

NOTE: See the table of contents on the DS18B20 Page

Ds18b20-4s.jpg The DS18B20 Brick is the easiest way to get connected and measure temperatures. Or a waterproofed version

The easy way to get connected is to simply plug the supplied cable into a YourDuinoRoboRED with built-in 3-pin connectors.

How does this Work??

Now, let's think for a minute about what we're doing here. The earlier devices we connected were simple: Switches, LEDs, Potentiometers, etc. A single component. This is different: we are connecting to a chip that has hundreds of transistors inside it and dozens of possible functions. It expects us to send it commands and it will return data as lots of signal pulses of 1's and 0's. How will we communicate with it? Fortunately, we're not in an empty room. We're in a library.

On the shelves, free for us to check out, are lots of already-published Arduino functions that we can use. We will use two library check-outs to run our temperature sensor that other people have spent 100's of hours working on, and given us unlimited copies to check out. (Terry's wife designs Libraries.Here's one she designed in China: Luckily for us, now she is writing more Arduino HOW-TO!)

To make this work, you MUST download and install the library DallasTemperature chips and copy it to your Arduino software installation Library folder. (See above).

(each one is called a "Library" not a Book! )

For more about Libraries see our Arduino Libraries page.

Then just copy and paste this test code (below) into your http://arduinoinfo.mywikis.net/wiki/GettingStarted-Software Arduino IDE]:

NOTE: If using the YourDuinoRobo1, connect to pin 3 (and change the pin number in the code below to 3).

Then:

  • ArduinoIDE-VerifyButton.jpg Click the VERIFY button. Soon you should see "Compiling" and "Done Compiling"
  • ArduinoIDE-Upload-Button.jpg Click the UPLOAD button. You should see "Uploading to IO Board" and "Done Uploading"
  • ArduinoIDE-Monitor-Button.jpg Now, click the SERIAL MONITOR button. (Make sure you have the baud rate set to 9600 baud in lower right of Serial Monitor). A new window should pop up and scroll lines of results showing you.. Temperature!

Troubleshooting?? Go back and get the original BLINK program working first. http://arduinoinfo.mywikis.net/wiki/GettingStarted-Software Check Here:]

Sketch to test DS18B20

<shtml hash="6e138d9b8b94536cb428debb6c4a503fe24c66822a482ca31a4f8f427141c897">
<font color="#95a5a6">/* YourDu05/21/2018</font>
<font color="#95a5a6">- Connect cable to Arduino Digital I/O Pin 2</font>
<font color="#95a5a6"></font>
<font color="#95a5a6">***  FIX Wire.h include</font>
<font color="#95a5a6">terry@yourduino.com */</font>



<font color="#95a5a6">/*-----( Import needed libraries )-----*/</font>
<font color="#434f54">//#include <OneWire.h>    // Included within recent DallasTemperature</font>
<font color="#5e6d03">#include</font> <font color="#434f54"><</font><font color="#d35400">DallasTemperature</font><font color="#434f54">.</font><font color="#000000">h</font><font color="#434f54">></font>

<font color="#95a5a6">/*-----( Declare Constants )-----*/</font>
<font color="#5e6d03">#define</font> <font color="#000000">ONE_WIRE_BUS</font> <font color="#000000">2</font> <font color="#95a5a6">/*-(Connect to Pin 2 )-*/</font>

<font color="#95a5a6">/*-----( Declare objects )-----*/</font>
<font color="#95a5a6">/* Set up a oneWire instance to communicate with any OneWire device*/</font>
<font color="#d35400">OneWire</font> <font color="#000000">ourWire</font><font color="#000000">(</font><font color="#000000">ONE_WIRE_BUS</font><font color="#000000">)</font><font color="#000000">;</font>

<font color="#95a5a6">/* Tell Dallas Temperature Library to use oneWire Library */</font>
<font color="#d35400">DallasTemperature</font> <font color="#000000">sensors</font><font color="#000000">(</font><font color="#434f54">&</font><font color="#000000">ourWire</font><font color="#000000">)</font><font color="#000000">;</font>

<font color="#95a5a6">/*-----( Declare Variables )-----*/</font>


<font color="#00979c">void</font> <font color="#5e6d03">setup</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#95a5a6">/*----( SETUP: RUNS ONCE )----*/</font>
<font color="#000000">{</font>
<font color="#95a5a6">/*-(start serial port to see results )-*/</font>
<font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">begin</font><font color="#000000">(</font><font color="#000000">9600</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">"YourDuino.com: Temperature Sensor Test Program"</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">"Temperature Sensor: DS18B20"</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font>

<font color="#95a5a6">/*-( Start up the DallasTemperature library )-*/</font>
<font color="#000000">sensors</font><font color="#434f54">.</font><font color="#d35400">begin</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">}</font><font color="#95a5a6">/*--(end setup )---*/</font>


<font color="#00979c">void</font> <font color="#5e6d03">loop</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#95a5a6">/*----( LOOP: RUNS CONSTANTLY )----*/</font>
<font color="#000000">{</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">"Requesting temperature..."</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">sensors</font><font color="#434f54">.</font><font color="#d35400">requestTemperatures</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Send the command to get temperatures</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">"DONE"</font><font color="#000000">)</font><font color="#000000">;</font>

<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">"Device 1 (index 0) = "</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#000000">sensors</font><font color="#434f54">.</font><font color="#d35400">getTempCByIndex</font><font color="#000000">(</font><font color="#000000">0</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">" Degrees C"</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">"Device 1 (index 0) = "</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#000000">sensors</font><font color="#434f54">.</font><font color="#d35400">getTempFByIndex</font><font color="#000000">(</font><font color="#000000">0</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">" Degrees F"</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">5000</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">}</font><font color="#95a5a6">/* --(end main loop )-- */</font>

<font color="#95a5a6">/* ( THE END ) */</font>



</shtml>



What You Should See (Your temperature may vary! Hold the sensor in your hand and see value increase):

YourDuino.com: Temperature Sensor Test Program
Temperature Sensor: DS18B20

Requesting temperature...DONE
Device 1 (index 0) = 25.12 Degrees C
Device 1 (index 0) = 77.22 Degrees F
 

If you see THIS there is a connection or sensor problem!

YourDuino.com: Temperature Sensor Test Program
Temperature Sensor: DS18B20

Requesting temperature...DONE
Device 1 (index 0) = -127.00 Degrees C
Device 1 (index 0) = -196.60 Degrees F

Here's the schematic diagram of the Brick:

TempSensor DS18B20 BrickDiag.jpg

TemperatureSensorDS18B20Waterproof-500.jpg

And here is another form of exactly the same DS18B20 temperature sensor in a waterproof stainless steel tube. These are Available Here).

You have the same connections to voltage and ground and signal, but you have to provide your own 4.7K "pullup" resistor which can be any small type resistor (two 10K resistors in parallel works well). Be careful to ensure the Signal and Ground connections are correct. If they are reversed, the sensor can get quite hot (even at such a low voltage).

TempSensorConnector1-500.jpg

TemperSensorSStube-DS18B20-1.jpg

Here (above) is a photo of this sensor connected to a [1] "3-pin Sensor" connector]so that it can be connected like our regular Electronic Bricks. You can see the 4.7K pullup resistor connected between + and Signal.

Sensor Connections: red (VCC), blue or white or yellow (DATA), black (GND)

Some Versions, like Adafruit, have these connections: Orange Stripe: (VCC) White: (GND) Blue Stripe:(DATA)

TempSensorConnector2-500.jpg

And on the right is a handy connection method if you need to quickly connect many temperature sensors to get their addresses, like I do. That's one of those Stereo speaker connectors that you push the
button in and push the wire in place. Scavenged from an old Stereo. Notice the resistor (4.7K to 10K OK) on the back from +5 (Red) to Signal (White).




Read Temperatures from multiple DS18B20 sensors on 1 Arduino Pin:

MultiDs18B20.png

You will need to know the internal address of each sensor. Farther below is a Utility Sketch that can read the address from sensors one at a time. You need to cut and paste the addresses, first to a text file and later into the example multi-sensor example sketch below.

Multiple DS18B20 Example:

NOTE: For an example that displays multiple DS18B20 temperatures on a 2-line OR 4-line LCD Display SEE THIS EXAMPLE:

/* YourDuino Multiple DS18B20 Temperature Sensors on 1 wire
  Connections:
  DS18B20 Pinout (Left to Right, pins down, flat side toward you)
  - Left   = Ground
  - Center = Signal (Pin 2):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
  - Right  = +5 or +3.3 V

   Questions: terry@yourduino.com 
   V1.01  01/17/2013 ...based on examples from Rik Kretzinger
   
/*-----( Import needed libraries )-----*/
// Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <OneWire.h>

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS_PIN 2

/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);

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

/*-----( Declare Variables )-----*/
// 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 Probe01 = { 0x28, 0x8A, 0xB1, 0x40, 0x04, 0x00, 0x00, 0xC7 };
DeviceAddress Probe02 = { 0x28, 0xCC, 0x92, 0x40, 0x04, 0x00, 0x00, 0xB6 };
DeviceAddress Probe03 = { 0x28, 0x4D, 0x8D, 0x40, 0x04, 0x00, 0x00, 0x78 };
DeviceAddress Probe04 = { 0x28, 0x9A, 0x80, 0x40, 0x04, 0x00, 0x00, 0xD5 };
DeviceAddress Probe05 = { 0x28, 0xE1, 0xC7, 0x40, 0x04, 0x00, 0x00, 0x0D };


void setup()   /****** SETUP: RUNS ONCE ******/
{
  // start serial port to show results
  Serial.begin(9600);
  Serial.print("Initializing Temperature Control Library Version ");
  Serial.println(DALLASTEMPLIBVERSION);

  // Initialize the Temperature measurement library
  sensors.begin();

  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 10);
  sensors.setResolution(Probe02, 10);
  sensors.setResolution(Probe03, 10);
  sensors.setResolution(Probe04, 10);
  sensors.setResolution(Probe05, 10);

}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  delay(1000);
  Serial.println();
  Serial.print("Number of Devices found on bus = ");
  Serial.println(sensors.getDeviceCount());
  Serial.print("Getting temperatures... ");
  Serial.println();

  // Command all devices on bus to read temperature  
  sensors.requestTemperatures();

  Serial.print("Probe 01 temperature is:   ");
  printTemperature(Probe01);
  Serial.println();

  Serial.print("Probe 02 temperature is:   ");
  printTemperature(Probe02);
  Serial.println();

  Serial.print("Probe 03 temperature is:   ");
  printTemperature(Probe03);
  Serial.println();

  Serial.print("Probe 04 temperature is:   ");
  printTemperature(Probe04);
  Serial.println();

  Serial.print("Probe 05 temperature is:   ");
  printTemperature(Probe05);
  Serial.println();


}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{

float tempC = sensors.getTempC(deviceAddress);

   if (tempC == -127.00)
   {
   Serial.print("Error getting temperature  ");
   }
   else
   {
   Serial.print("C: ");
   Serial.print(tempC);
   Serial.print(" F: ");
   Serial.print(DallasTemperature::toFahrenheit(tempC));
   }
}// End printTemperature
//*********( THE END )***********

Read individual DS18B20 Internal Addresses:

Here's a way to connect multiple DS18B20's to get their addresses and test them: A 3-wire cable connects Arduino +5V, Ground and Pin 2 to a small breadboard. The breadboard can "Store" DS18B20's you are working on, and 1 or more can be connected for test.

DS18B20M-2-512.jpg
DS18B20M-3-256.jpg

Here's a closeup of the connections: Left=Ground, Center=Data, Right=+5V, 3300 to 4700 ohm resistor from +5V to Data.

Find your sensors and put them in the top "Storage" row of the breadboard. Then put them one at a time in the active 3 columns on the lower right. You will use the Software Sketch below to get the individual addresses.

Next, cut and paste the sketch below into an Arduino IDE blank window. Save it. Upload it. Then open the Serial Monitor window. You should see this:

Found '1-Wire' device with address:
0x28, 0x8A, 0xB1, 0x40, 0x04, 0x00, 0x00, 0xC7
Done



That address is unique to that DS18B20.. not another the same anywhere! Open a text file and cut/paste the address to save it. I'd label it Sensor1 and put that sensor back in the first "storage" position. Do the same for the rest of your sensors. The sensors are hard to physically mark. "Sharpie" Paint Markers can work. Or an electric engraver works OK. Tape?
Now cut/paste the "Multiple DS18B20 Example" Sketch above. Save. Edit it to put the addresses of your sensors in the lines that look like this:


DeviceAddress Probe01 = { 0x28, 0x8A, 0xB1, 0x40, 0x04, 0x00, 0x00, 0xC7 };

Save, Upload. Plug your first DS18B20 in.  Then open the Serial Monitor window. You should see this:

Getting temperatures...
Probe 01 temperature is: C: 26.75 F: 80.15
Probe 02 temperature is: Error getting temperature
Probe 03 temperature is: Error getting temperature
Probe 04 temperature is: Error getting temperature
Probe 05 temperature is: Error getting temperature


DS18B20M-4-512.jpg
Now try plugging in 2 or 3 sensors at once, like this:

Now more sensors should show temperature readings. Wait about 5 minutes for them to stabilize and they should all be within 0.5 Degrees C.






Test Sketch to read DS18B20 addresses:

(Cut and paste from Serial Monitor)

/* YourDuino Example: Find Address of a DS18B20 Temperature Sensor
 Cut and paste the address to a text file for later use.
 V1.1 01/17/2013
 Questions: terry@yourduino.com 
 
 Connections:
 DS18B20 Pinout (Left to Right, pins down, flat side toward you)
 - Left   = Ground
 - Center = Signal (Pin 2):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
 - Right  = +5 or +3.3 V   
 This sketch looks for 1-wire devices and  prints their addresses (serial number)
 to the Serial Monitor in a format that is useful in Arduino sketches.
 Based on example at: 
 http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
 */

/*-----( Import needed libraries )-----*/
#include <OneWire.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define SENSOR_PIN 2  // Any pin 2 to 12 (not 13) and A0 to A5

/*-----( Declare objects )-----*/
OneWire  ourBus(SENSOR_PIN);  // Create a 1-wire object

void setup()  /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);

  discoverOneWireDevices();  // Everything happens here!
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  // Nothing happening here
}

/*-----( Declare User-written Functions )-----*/
void discoverOneWireDevices(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];

  Serial.print("Looking for 1-Wire devices...\n\r");// "\n\r" is NewLine 
  while(ourBus.search(addr)) {
    Serial.print("\n\r\n\rFound \'1-Wire\' device with address:\n\r");
    for( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');
      }
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
      }
    }
    if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n\r");
      return;
    }
  }
  Serial.println();
  Serial.print("Done");
  ourBus.reset_search();
  return;
}

//*********( THE END )***********

MULTIPLE 1_WIRE BUSES:

It is possible to have multiple 1-wire buses as well as multiple DS18B20's on each bus. Here is a test Sketch that creates two buses and searches each bus for 1-wire devices:

/* YourDuino Example: Find Address of a DS18B20 Temperature Sensor
 Search two electrically separate busses.
 Cut and paste the address to a text file for later use.
 V1.0 01/18/2013
 Questions: terry@yourduino.com 
 
 Connections:
 DS18B20 Pinout (Left to Right, pins down, flat side toward you)
 - Left   = Ground
 - Center = Signal (Pin 2):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
 - Right  = +5 or +3.3 V   
 This sketch looks for 1-wire devices and  prints their addresses (serial number)
 to the Serial Monitor in a format that is useful in Arduino sketches.
 Based on example at: 
 http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
 */

/*-----( Import needed libraries )-----*/
#include <OneWire.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define SENSOR_1_PIN 2 // For BUS 1
#define SENSOR_2_PIN 4 // For BUS 2

/*-----( Declare objects )-----*/
OneWire  Bus1(SENSOR_1_PIN);  // Create a 1-wire object
OneWire  Bus2(SENSOR_2_PIN);  // Create another 1-wire object

void setup()  /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  Serial.println("Searching for DS18B20's on two different busses");
  Serial.println();

  Serial.println("Searching for DS18B20's on BUS ONE");
  discoverBusOneWireDevices();

  Serial.println();
  Serial.println();

  Serial.println("Searching for DS18B20's on BUS TWO");
  discoverBusTwoWireDevices();
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  // Nothing happening here
}

/*-----( Declare User-written Functions )-----*/

void discoverBusOneWireDevices(void)
{
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];

  Serial.print("Looking for 1-Wire devices...\n\r");// "\n\r" is NewLine 
  while(Bus1.search(addr)) {
    Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");
    for( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');
      }
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
      }
    }
    if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n\r");
      return;
    }
  }
  Serial.println();
  Serial.print("Done");
  Bus1.reset_search();
  return;
}// END

//------------------------------------------------------------
void discoverBusTwoWireDevices(void)
{
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];

  Serial.print("Looking for 1-Wire devices...\n\r");// "\n\r" is NewLine 
  while(Bus2.search(addr)) {
    Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");
    for( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');
      }
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
      }
    }
    if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n\r");
      return;
    }
  }
  Serial.println();
  Serial.print("Done");
  Bus2.reset_search();
  return;
}//END


//*********( THE END )***********






DS18B20 CLONES and COUNTERFEITS

These chips are so popular that 3 or 4 other companies have made their own versions.. And SOME chips are made to LOOK like the Dallas/Maxim original. Those would be Counterfeits not just Clones.

There is a LOT of information about this on This GITHUB Site . You can download some test sketches there. Here is one that works well at showing all the internal differences.

/*
 * Copyright Chris Petrich
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 *   
 *   File:    discover_fake_DS18B20.ino
 *   Author:  Chris Petrich
 *   Version: 22 Oct 2019 
 *   
 *   Source:  https://github.com/cpetrich/counterfeit_DS18B20/
 *   Documentation:  https://github.com/cpetrich/counterfeit_DS18B20/
 *   
 * This demonstration script performs authenticity tests on DS18B20 sensors by
 * evaluating ROM code and Scratchpad Register. It uses documented ROM commands 
 * F0h and 55h and Function Commands 44h, 4Eh and BEh, only.
 * It does not test the power-up state and it does not write to or test the EEPROM.
 * It is INTENDED for EDUCATIONAL PURPOSES, only.
 * There may be circumstances under which the sketch permanently damages one-wire 
 * sensors in obvious or non-obvious ways.
 * (I don't think it does that to authentic Maxim sensors, but I won't guarantee
 * anything. See licence text for details.)
 * 
 * 
 * This script is written for Arduino. Wiring:
 * 
 * CPU Vcc         -------o------ DS18B20 Vcc
 *                        |
 *                       [R]  <- choose resistor appropriate for supply voltage and current that the microcontroller is able to sink.
 *                        |
 * CPU pin_onewire -------o------ DS18B20 data
 * 
 * CPU GND         -------------- DS18B20 GND
 * 
 */

// Tested with OneWire Version 2.3
// https://github.com/PaulStoffregen/OneWire
#include "OneWire.h"


#define pin_onewire 8
#define pin_LED 13

#define Comm Serial


OneWire *ds;

void print_hex(uint8_t value) {
  if (value < 16) Comm.write('0');
  Comm.print(value, HEX);
}

void print_array(uint8_t *data, int n, char sep = ',') {  
  int idx;
  for (idx=0; idx<n; idx++) {
    print_hex(data[idx]);
    if (idx != n-1)
      Comm.write(sep);
  }
}

bool read_scratchpad(uint8_t *addr, uint8_t *buff9) {
  ds->reset();
  ds->select(addr);
  ds->write(0xBE); // read scratchpad
  int idx;
  for (idx=0; idx<9; idx++)
    buff9[idx] = ds->read();
  return 0 == OneWire::crc8(buff9, 9);
}

void setup() {
  Comm.begin(115200);
  Comm.println();  
  
  digitalWrite(pin_LED, HIGH);
  pinMode(pin_LED, OUTPUT);  

  ds = new OneWire(pin_onewire);
  
  {
    // output file name without leading path
    char *file = __FILE__;
    int i;
    for (i = strlen(file); i > 0; i--)
      if ((file[i] == '\\') || (file[i] == '/')) {
        i++;
        break;  
      }    
    Comm.print(F("\n--- # "));
    Comm.println(&file[i]);
  }
  digitalWrite(pin_LED, LOW);
  Comm.println(F("This sketch will test DS18B20 sensors attached to"));
  Comm.print(F("  pin "));
  Comm.print(pin_onewire, DEC);
  Comm.println(F(" for differences with Maxim Integrated-produced DS18B20"));
  Comm.println(F("  using only functionality documented in the datasheet and in"));
  Comm.println(F("  Maxim Application Note AN4377."));  
  Comm.println();
}

void loop() {
  // ROM address of current sensor
  uint8_t addr[8];  
  // buffers for scratchpad register
  uint8_t buffer0[9];
  uint8_t buffer1[9];
  uint8_t buffer2[9];
  uint8_t buffer3[9];
  // flag to indicate if validation
  //  should be repeated at a different
  //  sensor temperature
  bool t_ok;

  ds->reset_search();
  while (ds->search(addr)) {
    int fake_flags = 0;
    
    print_array(addr, 8, '-');
    if (0 != OneWire::crc8(addr, 8)) {
      // some fake sensors can have their ROM overwritten with
      // arbitrary nonsense, so we don't expect anything good
      // if the ROM doesn't check out
      fake_flags += 1;
      Comm.print(F(" (CRC Error -> Error.)"));
    }

    if ((addr[6] != 0) || (addr[5] != 0) || (addr[0] != 0x28)) {
      fake_flags += 1;
      Comm.print(F(": ROM does not follow expected pattern 28-xx-xx-xx-xx-00-00-crc. Error."));
    } else {
      Comm.print(F(": ROM ok."));
    }    
    Comm.println();
    
    if (!read_scratchpad(addr, buffer0)) read_scratchpad(addr, buffer0);
    
    Comm.print(F("  Scratchpad Register: "));
    print_array(buffer0, 9, '/');
    if (0 != OneWire::crc8(buffer0, 9)) {
      // Unlikely that a sensor will mess up the CRC of the scratchpad.
      // --> Assume we're dealing with a bad connection rather than a bad 
      //     sensor, dump data, and move on to next sensor.
      Comm.println(F("  CRC Error. Check connections or replace sensor."));
      continue;      
    }
    Comm.println();

    // Check content of user EEPROM. Since the EEPROM may have been programmed by the user earlier
    // we do not use this as a test. Rather, we dump this as info.
    Comm.print(F("  Info only: Scratchpad bytes 2,3,4 ("));
    print_array(buffer0+2,3,'/');
    Comm.print(F("): "));
    if ((buffer0[2] != 0x4b) || (buffer0[3] != 0x46) || (buffer0[4] != 0x7f))
      Comm.println(F(" not Maxim default values 4B/46/7F."));
    else
      Comm.println(F(" Maxim default values."));


    Comm.print(F("  Scratchpad byte 5 (0x"));
    print_hex(buffer0[5]);
    Comm.print(F("): "));
    if (buffer0[5] != 0xff) {
      fake_flags += 1;
      Comm.println(F(" should have been 0xFF according to datasheet. Error."));
    } else {
      Comm.println(F(" ok."));
    }

    Comm.print(F("  Scratchpad byte 6 (0x"));
    print_hex(buffer0[6]);
    Comm.print(F("): "));
    if ( ((buffer0[6] == 0x00) || (buffer0[6] > 0x10)) || // totall wrong value
         ( ((buffer0[0] != 0x50) || (buffer0[1] != 0x05)) && ((buffer0[0] != 0xff) || (buffer0[1] != 0x07)) && // check for valid conversion...
           (((buffer0[0] + buffer0[6]) & 0x0f) != 0x00) ) ) { //...before assessing DS18S20 compatibility.
      fake_flags += 1;
      Comm.println(" unexpected value. Error.");
    } else
      Comm.println(" ok.");
    
    Comm.print(F("  Scratchpad byte 7 (0x"));
    print_hex(buffer0[7]);
    Comm.print(F("): "));
    if (buffer0[7] != 0x10) {
      fake_flags += 1;
      Comm.println(F(" should have been 0x10 according to datasheet. Error."));
    } else {
      Comm.println(F(" ok."));
    }

    // set the resolution to 10 bit and modify alarm registers    
    ds->reset();
    ds->select(addr);
    ds->write(0x4E); // write scratchpad. MUST be followed by 3 bytes as per datasheet.
    ds->write(buffer0[2] ^ 0xff);
    ds->write(buffer0[3] ^ 0xff);
    ds->write(0x3F);
    ds->reset();

    if (!read_scratchpad(addr, buffer1)) read_scratchpad(addr, buffer1);
    
    Comm.print(F("  0x4E modifies alarm registers: "));
    if ((buffer1[2] != (buffer0[2] ^ 0xff)) || (buffer1[3] != (buffer0[3] ^ 0xff))) {
      fake_flags += 1;
      Comm.print(F(" cannot modify content as expected (want: "));
      print_hex(buffer0[2] ^ 0xff);
      Comm.write('/');
      print_hex(buffer0[3] ^ 0xff);
      Comm.print(F(", got: "));
      print_array(buffer1+2, 2, '/');
      Comm.println(F("). Error."));      
    } else
      Comm.println(F(" ok."));

    Comm.print(F("  0x4E accepts 10 bit resolution: "));
    if (buffer1[4] != 0x3f) {
      fake_flags += 1;
      Comm.print(F(" rejected (want: 0x3F, got: "));
      print_hex(buffer1[4]);
      Comm.println(F("). Error."));
    } else
      Comm.println(F(" ok."));

    Comm.print(F("  0x4E preserves reserved bytes: "));
    if ((buffer1[5] != buffer0[5]) || (buffer1[6] != buffer0[6]) || (buffer1[7] != buffer0[7])) {
      fake_flags += 1;
      Comm.print(F(" no, got: "));
      print_array(buffer1+5, 3, '/');
      Comm.println(F(". Error."));
    } else
      Comm.println(F(" ok."));    

    // set the resolution to 12 bit
    ds->reset();
    ds->select(addr);
    ds->write(0x4E); // write scratchpad. MUST be followed by 3 bytes as per datasheet.
    ds->write(buffer0[2]);
    ds->write(buffer0[3]);
    ds->write(0x7f);
    ds->reset();

    if (!read_scratchpad(addr, buffer2)) read_scratchpad(addr, buffer2);
    
    Comm.print(F("  0x4E accepts 12 bit resolution: "));
    if (buffer2[4] != 0x7f) {
      fake_flags += 1;
      Comm.print(F(" rejected (expected: 0x7F, got: "));
      print_hex(buffer2[4]);
      Comm.println(F("). Error."));
    } else
      Comm.println(F(" ok."));

    Comm.print(F("  0x4E preserves reserved bytes: "));
    if ((buffer2[5] != buffer1[5]) || (buffer2[6] != buffer1[6]) || (buffer2[7] != buffer1[7])) {
      fake_flags += 1;
      Comm.print(F(" no, got: "));
      print_array(buffer2+5, 3, '/');
      Comm.println(F(". Error."));
    } else
      Comm.println(F(" ok."));

    Comm.print("  Checking byte 6 upon temperature change: ");
    if (( ((buffer2[0] == 0x50) && (buffer2[1] == 0x05)) || ((buffer2[0] == 0xff) && (buffer2[1] == 0x07)) ||
         ((buffer2[6] == 0x0c) && (((buffer2[0] + buffer2[6]) & 0x0f) == 0x00)) ) &&
         ((buffer2[6] >= 0x00) && (buffer2[6] <= 0x10)) ){
      // byte 6 checked out as correct in the initial test but the test ambiguous.
      //   we need to check if byte 6 is consistent after temperature conversion
            
      // We'll do a few temperature conversions in a row.
      // Usually, the temperature rises slightly if we do back-to-back
      //   conversions.
      int count = 5;
      do {
        count -- ;
        if (count < 0)
          break;
        // perform temperature conversion
        ds->reset();
        ds->select(addr);
        ds->write(0x44);
        delay(750);
        
        if (!read_scratchpad(addr, buffer3)) read_scratchpad(addr, buffer3);
        
      } while ( ((buffer3[0] == 0x50) && (buffer3[1] == 0x05)) || ((buffer3[0] == 0xff) && (buffer3[1] == 0x07)) ||
                ((buffer3[6] == 0x0c) && (((buffer3[0] + buffer3[6]) & 0x0f) == 0x00)) );
      if (count < 0) {
        Comm.println(F(" Inconclusive. Please change sensor temperature and repeat."));
        t_ok = false;
      } else {
        t_ok = true;
        if ((buffer3[6] != 0x0c) && (((buffer3[0] + buffer3[6]) & 0x0f) == 0x00)) {
          Comm.println(F(" ok."));
        } else {
          fake_flags += 1;
          Comm.print(F(" Temperature LSB = 0x"));
          print_hex(buffer3[0]);
          Comm.print(F(" but byte 6 = 0x"));
          print_hex(buffer3[6]);
          Comm.println(F(". Error."));
        }
      }
    } else {
      Comm.println(F("not necessary. Skipped."));
      t_ok = true;
    }

    Comm.print(F("  --> "));
    if (!t_ok) {
      Comm.print(F("DS18S20 counterfeit test not completed, otherwise sensor"));
    } else 
      Comm.print(F("Sensor"));
      
    if (fake_flags == 0) {
      Comm.println(F(" responded like a genuie Maxim."));
      Comm.println(F("      Not tested: EEPROM, Parasite Power, and undocumented commands."));
    } else {
      Comm.print(F(" appears to be counterfeit based on "));
      Comm.print(fake_flags, DEC);
      Comm.println(F(" deviations."));
      if (fake_flags == 1) {
        Comm.println(F("  The number of deviations is unexpectedly small."));
        Comm.println(F("  Please see https://github.com/cpetrich/counterfeit_DS18B20/"));
        Comm.println(F("  to help interpret the result."));
      }
    }
    Comm.println();
  } // done with all sensors
  Comm.println(F("------------------------------------------------"));
  delay(4000);
  digitalWrite(pin_LED, digitalRead(pin_LED) == HIGH ? LOW : HIGH);
}


LONG CABLES:


See the example above about Multiple DS18B20's. Most people use readily-available CAT5 Ethernet cable. There are no definite "Standards" for pinout of the cable but here is one that is often used. It also provides separate 5V and 12V power for remote devices:
RJ45pinout.jpg

TROUBLESHOOTING: parasite power


The way that the library detects parasite power is to issue a "Read Power Supply" command to the one-wire bus, to which parasite powered sensors will respond by pulling the bus low and non-parasite sensors will leave the bus as it is. So if any one, or more, of multiple sensors pull the bus low, the library assumes that ALL the sensors are parasite powered. If you have all sensors connected to +5V power then none of the sensors should be pulling the bus low, so if you see "Parasite Power" there's a wiring or other problem with the way that the sensors are connected.


zz