DS18B20 - Cannot generate address

I have a DS18B20 that I cannot generate an address with using the following sketch although the sensor work well using a sketch that defines the actual ds pin and gives a serial output I cannot get anything to appear on the serial monitor for the address sketch.

What could I be missing?

#include <OneWire.h>

OneWire  ds(2);  // This is where DQ of your DS18B20 will connect.

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

void getDeviceAddress(void) {
  byte i;
  byte addr[8];
  
  Serial.println("Getting the address...\n\r");
  /* initiate a search for the OneWire object we created and read its value into
  addr array we declared above*/
  
  while(ds.search(addr)) {
    Serial.print("The address is:\t");
    //read each byte in the address array
    for( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');
      }
      // print each byte in the address array in hex format
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
      }
    }
    // a check to make sure that what we read is correct.
    if ( OneWire::crc8( addr, 7) != addr[7]) {
        Serial.print("CRC is not valid!\n");
        return;
    }
  }
  ds.reset_search();
  return;
}

void loop(void) {
  // do nothing
}

Also with this sensor I am able to serial print the temperature display though I cannot output the temperature to a OLED display without the senor reading -127 signifying an incorrect connection.

Has anyone else had this issue???

#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>
#include <OneWire.h>
#include <DallasTemperature.h>

OneWire bus(2); //SET UP ONE WIRE LIBRARY ON PIN 2
DallasTemperature sensors(&bus); //SETUP DALLAS TEMP TO WORK ON ONEWIRE BUS


//==========================================================================
//  THE BELOW WORKS FOR A SH1106 OLED DISPLAY ON THE U8G2 LIBRARY
//  THIS SETS UP THE OLED TO OPERATE CORRECTLY 
//==========================================================================

U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); 


//=========================================================================
//    THE XBM FILE GOES BELOW - MUST KNOW DEMENSIONS!
//========================================================================
#define Thermo_width 41
#define Thermo_height 64

static const unsigned char Thermo_bits[] PROGMEM = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x83, 0x01, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x00,
   0x00, 0x00, 0x81, 0x01, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x00,
   0x00, 0x00, 0x81, 0x01, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x00,
   0x00, 0x00, 0x81, 0x01, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x00,
   0x00, 0x00, 0x81, 0x01, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x00,
   0x00, 0x00, 0x81, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x00,
   0x00, 0x00, 0x81, 0x01, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x00,
   0x00, 0x00, 0x81, 0x01, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x00,
   0x00, 0x00, 0x81, 0x01, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x00,
   0x00, 0x00, 0x81, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x00,
   0x00, 0x00, 0x81, 0x01, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x00,
   0x00, 0x00, 0x81, 0x01, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x00,
   0x00, 0x00, 0xb9, 0x01, 0x00, 0x00, 0x00, 0x00, 0xb9, 0x01, 0x00, 0x00,
   0x00, 0x00, 0xb9, 0x01, 0x00, 0x00, 0x00, 0x00, 0xb9, 0x0f, 0x00, 0x00,
   0x00, 0x00, 0xb9, 0x01, 0x00, 0x00, 0x00, 0x00, 0xb9, 0x01, 0x00, 0x00,
   0x00, 0x00, 0xb9, 0x01, 0x00, 0x00, 0x00, 0x00, 0xb9, 0x01, 0x00, 0x00,
   0x00, 0x00, 0xb9, 0x01, 0x00, 0x00, 0x00, 0x00, 0xb9, 0x01, 0x00, 0x00,
   0x00, 0x00, 0xb9, 0x01, 0x00, 0x00, 0x00, 0x00, 0xb9, 0x0f, 0x00, 0x00,
   0x00, 0x00, 0xb9, 0x01, 0x00, 0x00, 0x00, 0x00, 0xb9, 0x01, 0x00, 0x00,
   0x00, 0x00, 0xb9, 0x01, 0x00, 0x00, 0x00, 0x00, 0xb9, 0x01, 0x00, 0x00,
   0x00, 0x00, 0xb9, 0x01, 0x00, 0x00, 0x00, 0x80, 0xb9, 0x03, 0x00, 0x00,
   0x00, 0xc0, 0x3c, 0x06, 0x00, 0x00, 0x00, 0x60, 0xfe, 0x0c, 0x00, 0x00,
   0x00, 0x20, 0xff, 0x0d, 0x00, 0x00, 0x00, 0xb0, 0xff, 0x1b, 0x00, 0x00,
   0x00, 0x90, 0xff, 0x1b, 0x00, 0x00, 0x00, 0xd0, 0xff, 0x17, 0x00, 0x00,
   0x00, 0xd0, 0xff, 0x17, 0x00, 0x00, 0x00, 0xd0, 0xff, 0x17, 0x00, 0x00,
   0x00, 0x90, 0xff, 0x1b, 0x00, 0x00, 0x00, 0xb0, 0xff, 0x1b, 0x00, 0x00,
   0x00, 0x20, 0xff, 0x0d, 0x00, 0x00, 0x00, 0x60, 0xfe, 0x0c, 0x00, 0x00,
   0x00, 0xc0, 0x38, 0x06, 0x00, 0x00, 0x00, 0x80, 0xc3, 0x03, 0x00, 0x00,
   0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

float temperature; //NOT SURE WHAT THESE ARE FOR YET
char temperatureString[6] = "-"; //NOT SURE WHAT THESE ARE FOR YET

void draw(void) {
// graphic commands to redraw the complete screen should be placed here  
 u8g2.drawXBMP( 10, 0, Thermo_width, Thermo_height, Thermo_bits); // THESE LINES CALL UPON THE SIZING AT THE TOP OF THE BITMAP CODING.
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(43,20,"TEMP");
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(43,40,temperatureString);
}

void setup(void) {
  sensors.begin();
  u8g2.begin();
  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(43,20,"IT'S");
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(18,37,"GETTING");
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(30,55,"REAL!");
  } while ( u8g2.nextPage() );
  delay(1000);
}

void loop(void) { 
 // THIS STARTS THE PICTURE LOOP
u8g2.firstPage();
do {
     draw();
    } while( u8g2.nextPage() );

// Update the sensor readings 
sensors.requestTemperatures(); 
  
// Read Temperature 
temperature = sensors.getTempCByIndex(0); 
dtostrf(temperature, 2, 2, temperatureString); 


// rebuild the picture after some delay
delay(1000);
}

Which Arduino board ?

It is a Arduino Leonardo

Leonardo and pin 2 :o

Pin 2 is SCL
Pin 3 is SDA
SDA and SCL are also extra pins near the reset button, but they are the same as pin 2 and 3.

Is the display using the I2C bus ?

Put the DHT sensor at pin 4 and it should work :smiley:

That's a good point!

I will swap the pins and test tonight.

You've also picked up something I had totally missed.
I am using the I2C SH1106 Display but have included SPI.........

Thanks for your help