Digital Temperature Sensor - DS18B20. Code does not work

I’m trying to use Digital Temperature Sensor - DS18B20 on Mega 2560.
Code from here: http://www.hobbytronics.co.uk/ds18b20-arduino
The only change is:
ONE_WIRE_BUS = A3:

 #include <OneWire.h>
#include <DallasTemperature.h>
 
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS A3
 
// 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);
 
void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}
 
 
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");

  Serial.print("Temperature for Device 1 is: ");
  Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? 
    // You can have more than one IC on the same bus. 
    // 0 refers to the first IC on the wire
 
}

this is the result:

Temperature for Device 1 is: -127.00

what I’m doing wrong?

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Also cannot find devices using this code:

// 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(A3);  // Connect your 1-wire device to pin 3

void setup(void) {
  Serial.begin(9600);
  discoverOneWireDevices();
}

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++) {
      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");
        return;
    }
  }
  Serial.print("\n\r\n\rThat's it.\r\n");
  ds.reset_search();
  return;
}

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

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Are you using a pullup resistor on the data line? Can you show us your connections?

3.9 kOhm to +5V

The code works

4.7k resister between +5 and data line

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

byte oneWireMacAddress[7][8];          // hold mac address max 7 probes per pin
int totalDevices=0;

OneWire myProbes(   need to give pin number    );// change pin number

DallasTemperature tProbe(&myProbes);


void setup(){
  Serial.begin(9600);
  
  totalDevices = getProbeCount(myProbes);

  Serial.print("One wrire device count = ");
  Serial.println(totalDevices);

  if(!getProbeCount(myProbes)==0){
    tProbe.begin();
    discoverOneWireDevices( myProbes );         // get addresses of our one wire devices into allAddress array 
    for (byte i=0; i < totalDevices ; i++) 
      tProbe.setResolution(oneWireMacAddress[i], 10);      // and set the a to d conversion resolution of each.
  }

}

void loop(){

  tProbe.requestTemperatures();
  delay(2000);
  printTemperature(tProbe, totalDevices);
  delay(2000);
}


byte discoverOneWireDevices( OneWire temp ) {
    int j=0;                           
                                              
     while ((j < totalDevices) && (temp.search(oneWireMacAddress[j]))) {        
       j++;
     }
     
    for (byte i=0; i < j; i++) {
     Serial.print("Device ");
     Serial.print(i);  
     Serial.print(": ");  
     
      printAddress(oneWireMacAddress[i]);                  // print address from each device address array.
   
}
  Serial.print("\r\n");
  return j                      ;                 // return total number of devices found.
}

void printAddress(DeviceAddress addr) {
  byte i;
  for( i=0; i < 8; i++) {                         // prefix the printout with 0x
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');                        // add a leading '0' if required.
      }
      Serial.print(addr[i], HEX);                 // print the actual value in HEX
      if (i < 7) {
        Serial.print(", ");
      }
    }
  Serial.print("\r\n");
}

void printTemperature(DallasTemperature ds , byte count ) {
  
  float tempC;
  int arrayCount;
  
  for (byte i=0; i < count; i++) 
  {
   tempC = ds.getTempC(oneWireMacAddress[i]);           // read the device at addr.
    
   if (tempC == -127.00){
     Serial.println("Error");
   }else{
     Serial.print("Device temp = ");
     Serial.println(tempC);
   }
  }
}


byte getProbeCount(OneWire ds){
  byte count=0;
  byte addr[8];
  
  while(ds.search(addr)) {
    count++;
  }

  ds.reset_search();
  return count;
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

this sketch also is not working.
which pin are you using?

OneWire myProbes( need to give pin number );// change pin number

you need to put pin number you are using in the line above.

the code works i tested it on sensors before i uploaded

use all 3 wires on the ds20, and use a 4.7k resister.
connect between +5 and data. ie pullup resister.
and you have to use a digital pin?

Pin 3 and Pin A3 are not the same thing. That may be your problem.

change
your code
OneWire ds(A3); // Connect your 1-wire device to pin 3
ro
OneWire ds(3); // Connect your 1-wire device to pin 3

no such thing as a3

no such thing as a3

There certainly is. The 4th analog pin, being used as a digital pin, is A3.

PaulS:

no such thing as a3

There certainly is. The 4th analog pin, being used as a digital pin, is A3.

Not sure what was intended but a3 is not the same as A3.

--- bill

ye on the board a0,a1 to show they are analog,

but you cant use a0 or a1 in the C code?? only INT value can be used???
might be wrong, will check later :slight_smile:

DS20 uses digital pins so the value has to be OneWire ds(3);

Do we have any confidence that this thing is wired up correctly in the first place?
Photo of the wiring would be useful - after all no code at all will "work" if the thing
isn't wired up right...

Amus3d:
ye on the board a0,a1 to show they are analog,

but you cant use a0 or a1 in the C code?? only INT value can be used???
might be wrong, will check later :slight_smile:

Again. a0 is not the same as A0 and a1 is not the same as A1.
When programming, details totally matter.
User sketch code is free to use a0, a1, etc. for its own use (variables, constants, functions etc...)

DS20 uses digital pins so the value has to be OneWire ds(3);
WRONG.
The names An like A0, A2, A3 etc... are symbolic constants that
the Arduino team created and represent
the arduino pin connected to the header pin on the board labled
A3. That name can not only be used with the digital pwm function
analogWrite() or the analog function analogRead(),
but can also be used with any of the digital pin functions
just like any any integer number or the pre-defined Dn constants.
i.e.

pinMode(A3, INPUT); // digital input mode

pinMode(A3, OUTPUT); // digital output mode
digitalWrite(A3, HIGH); // digital mode output
digitalRead(A3); // digital mode input
analogWrite(A3, 42); // digital mode pwm output
analogRead(A3); // analog mode input



etc...

The reason it is better to use something like A0 vs D14
is because while on UNO A0 is the same as D14 this is not the case
on other boards. So code that uses D14 and expects to be using the pin
labeled A0 may not work as expected on other boards since it may be using the wrong pin.

--- bill

learning some new every day'

thank you Sir :slight_smile:

redone all connections do DS1820 and replaced resistor 2K2 instead of 4K7.
everything is working now.
Probably problem with resistor.
Thank you.