ESP8266 can't detect DS18B20 Temperature Sensor

So, I have connected my ESP8266 module with DS18B20 temperature sensor. I also have connected the 3.3V wire and data wire with pull up resistor 4.7k Ohm. below are the wirings.



Then I uploaded the code below to Generic ESP8266 Module board using the DallasTemperature library's code example (Simple) for ds18b20 sensor. But it doesn't connect with my temperature.

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

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

// 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);

/*
 * The setup function. We only start the sensors here
 */
void setup(void)
{
  // start serial port
  Serial.begin(115200);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}

/*
 * Main function, get and show the temperature
 */
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");
  delay(1500);
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  float tempC = sensors.getTempCByIndex(0);

  // Check if reading was successful
  if (tempC != DEVICE_DISCONNECTED_C)
  {
    Serial.print("Temperature for the device 1 (index 0) is: ");
    Serial.println(tempC);
  }
  else
  {
    Serial.println("Error: Could not read temperature data");
  }
}

Is there any thing that I should check to fix this?


By the way, this is the result of failed detecting DS18B20 module

on your photo it's on gpio 4

#define ONE_WIRE_BUS 4

Oh sorry, I have changed the data wire into GPIO 4

But it still won't connect

GPIO4 is board pin D2 or just 2.
Leo..

It should. Verify your wire connections and pullup resistor value.

this works on a ESP8266 NodeMCU Dev board
DS18B20 Data wire is plugged into pin D2 GPIO4

// ESP8266 DS18B20 one-wire temperature sensor -  read the temperature

// Tools>Board set to "Generic ESP8266 module"

// connect VCC red 3v GND black GND yellow (signal) pin D2 GPIO4 
//  note 4.7Kohm pullup to 3.3V


/********************************************************************/
// First we include the libraries
#include <OneWire.h> 
#include <DallasTemperature.h>
/********************************************************************/
// DS18B20 Data wire is plugged into pin D2 GPIO4 on the ESP8266 
#define ONE_WIRE_BUS 4
/********************************************************************/
// 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) 
{ 
  //    pinMode(4, OUTPUT);  // SR04M  0V power
//      digitalWrite(4, LOW);  
//      pinMode(6, OUTPUT);  // SR04M  5V power
//      digitalWrite(6, HIGH);  
 
 // start serial port 
 Serial.begin(115200); 
 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 temperature readings 
 Serial.println("DONE"); 
/********************************************************************/
 Serial.print("Temperature is: "); 
 Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?  
   // You can have more than one DS18B20 on the same bus.  
   // 0 refers to the first IC on the wire 
   delay(1000); 
} 

serial monitor output

Temperature is: 23.63 Requesting temperatures...DONE
Temperature is: 23.69 Requesting temperatures...DONE
Temperature is: 24.06 Requesting temperatures...DONE
Temperature is: 24.56 Requesting temperatures...DONE
Temperature is: 25.06 Requesting temperatures...DONE
Temperature is: 25.50 Requesting temperatures...DONE
Temperature is: 25.88 Requesting temperatures...DONE
Temperature is: 26.19 Requesting temperatures...DONE
Temperature is: 26.44 Requesting temperatures...DONE
Temperature is: 26.69 Requesting temperatures...DONE
Temperature is: 26.94 Requesting temperatures...DONE
Temperature is: 27.13 Requesting temperatures...DONE
Temperature is: 27.31 Requesting temperatures...DONE
Temperature is: 27.44 Requesting temperatures...DONE

How to verify my wire connections?
For the pullup resistor value is it must be tested on another tools?

Multimeter is the easiest way. If you don't have, careful visual inspection at least. Are the wires soldered or just tied together? Verify resistor value with color codes.

Do you have a multimeter/DMM?

sadly, i don't have multimeter, but I will try to ask my friend about it. But for now, I have verified the wiring for the module's cabel and I think the wiring for pullup resistor-data(yellow)-VCC(red) all good and soldered enough. But I am not pretty sure for the extension of the black one since it just tied up with blue jumper cable. And for the resistor, I have checked online but it seems the resistor have different color than it should be.

My resistor:

Online image show that it should be like this:

or this:

Am i getting the right 4.7k Ohm resistor?

check the color codes and with a multimeter

pullup 4.7Kohn yellow violet red

EDIT: yellow violet black is usually 47ohms

Connections look good enough. Resistor looks correct as well.
Maybe bad sensor...

The image in post#12.
Could it be that the back row of the NodeMCU is in the power rail of that breadboard, thus shorting all the pins together.

This sketch, using pin D6 is tried and tested.
Leo..

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

OneWire oneWire(6); // pin D6
DallasTemperature sensors(&oneWire);
float temp;

void setup() {
  Serial.begin(115200);
  Serial.println("DS18B20 thermometer");
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  Serial.print("Temp is ");
  Serial.print(temp, 4); // four decimal places
  Serial.println(" C");
}

It doesn't work, even the error message is different:

Tried this one too but still error...

That is hard to say. Is the first ring yellow?

I think so? It is between yellow and lime green

On esp8266 this is GPIO6, SDCLK, not usable here.

on an ESP8266 GPIO 4 is D2, so actually D2 = 4.
To confirm, plug in the nodeMCU and see the result of

Serial.prinln(D2, DEC);

So true. (actually none of the GPIO pins on that side of the board are usable)

should be

OneWire oneWire(D6); // pin D6

or

OneWire oneWire(12); // pin D6