How to convert temperature from DS18B20 sensor module to Celsius?

Hello, I am starting to explore Arduino Nano and some sensors. I have a DS18B20 sensor that came in a Elegoo kit, the sensor is part of a module:

I connected the module according to:
A0 of arduino connected to Y of module
GND connected to G
3.3V connected to R

I wrote the following code:

const int sensor_pin = A0;

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

void loop() {
  int sensor_val = analogRead(sensor_pin);
  Serial.print("Value:");
  Serial.print(sensor_val);
  Serial.println();
  delay(500);
}

Which outputs values around 2000.

How to convert these values into Celsius? I tried scripts with the OneWire and DallasTemperature libraries while using a digital pin, but I get value -127. Example of script: KY-001 Temperature Sensor Module - ArduinoModulesInfo

The DS18B20 is not an analog sensor. Search for the proper way to use it.

1 Like

See DS18B20 is only giving -127

-127 means not connected.
The DS18B20 has output in celcius, no conversion required. Use the sketch in your link, not the rubbish in your post. Nanos and Unos are the same.
Your wiring appears to be kosher, if it isn't the sensor will get very hot but might survive. Your problem may simply be a bad breadboard.

You're missing the pull-up resistor from the output to Vcc and your sketch is for an analog sensor. Add the pull-up and run the example sketch for the DallasTemperature 1-Wire.

1. Check again that the Temperature is DS18B20 and NOT LM35. You can see the type number on the sensor's surface.

2. If the is DS18B20, then connect it with ESP32 NANO as per following diagram (Fig-1).


Figure-1:

3. Upload the following tested sketch.

//12-bit default resolution; external power supply
#include<OneWire.h>

OneWire ds(5);    //DPin-7 of UNO with data line of DS18B20
byte addrs[8];         //to hold 64-bit ROM Codes of DS1
float celsius;
byte data[9];

void setup()
{
  Serial.begin(9600);
  ds.reset();  //sensor reset
  ds.search(addrs);  //collect 64-bit ROM code from sensor (DS1)
}

void loop()
{
  ds.reset();       //bring 1-Wire into idle state
  ds.select(addrs); //slect with DS-1 with address addr1
  ds.write(0x44);    //conversion command
  delay(750);   //maximum conversion delay
  //---------------------------
  ds.reset();
  ds.select(addrs);  //selectimg the desired DS18B20
  ds.write(0xBE);    //Function command to read Scratchpad Memory (9Byte)
  ds.read_bytes(data, 9); //data comes from DS and are saved into buffer data[8]
  //---------------------------------
  //data[0] contains lower 8-bit of temperaure
  //data[1] contains upper 8-bit of temperature

  float fracTemp = bitRead(data[0], 3) * 0.5 + bitRead(data[0], 2) * 0.25
                   + bitRead(data[0], 1) * 0.125 + bitRead(data[0], 0) * 0.0625;
  int integerTemp = bitRead(data[1], 2) * 64 + bitRead(data[1], 1) * 32
                    + bitRead(data[1], 0) * 16 + bitRead(data[0], 7) * 8
                    + bitRead(data[1], 6) * 4 + bitRead(data[1], 5) * 2 + bitRead(data[1], 0) * 1;

  celsius = (float)integerTemp + fracTemp;
  Serial.println(celsius, 4);
}

4. Check that the Serial Monitor shows correct temperature value:

25.5000
25.0000
25.0000
25.5000
25.5000
25.5000
32.5000  //suddenly heated up by a lighter
32.0000
32.0000


At least have the PIN numbers on your schematic match OPs sensor.

Have you used Multimemter to check that left-most pin (Pin-1, GND) of Sensor is shorted with Pin-3 of the Breakout Board?

My point is that OP is obviously not knowledgeable enough to make the leap from your schematic to hooking up their breakout board correctly so you should do the work of mapping to the correct pins for them.

Right! If you have used Multimeter for mapping, then you are correct in post #7.

I have different breakout board (DS1307, Fig-2) containging DS18B20; but, the Pin numbers-vs-signals (Fig-1) of the sensor are:
PIn-1 (Leftmost) : GND
Pin-2 (Middle): DQ (Signal)
Pin-3 (Rightmost): VDD (3.3V)


Figure-1:


Figure-2:

I mixed up, I was using this sensor while reading a tutorial that was using an analog sensor... Thank you for correcting me.

So, now I changed to pin D2, and used the script according to KY-001 Temperature Sensor Module - ArduinoModulesInfo
On the sketch of the url it shows that the VCC is connected to 5V, however I am providing 3.3V. I still get the value of -127 and the LED in the module is not blinking. Should I connect the VCC to VBUS instead of 3.3V?

Are you using LM35 (analog sensor) or DS18B20 (digital sensor)?


The data sheet confirms that 3V3 is okay for Vdd. Do you have a 4K7 resistor from DQ to 3V3? The "schematic" from your link seems to omit it.

From the data sheet:

I am using the DS18B20 (digital sensor). But I was following an analog sensor tutorial :man_facepalming:

Follow post #6. For connection of the Sensor of your Breakout Board, follow post #7.

I am a bit confused with the schematic, previously you shared that R (2), the middle pin, is VCC, from this latest schematic looks like the middle pin is the data.

That image is analogous to a picture of the chip itself. If you hold the chip (DS18B20) with the flat side toward you, DQ is the center pin. You are thinking about your circuit board which has the orientation different. Pay attention to the pin names, DQ(Output), Vcc, GND, when you are hooking it up.

From the link you posted:


Add a 4.7k resistor from the blue wire to 3.3V. Connect Vcc to 3.3V too. They are using a different board than yours and it is running off 5V. Don't connect to 5V on your board as that will damage your board.

I'm probably confusing you as I'm explaining through use of device data sheet information and schematic diagrams and you are evidently used to working from pictures of prototyping breadboards. Maybe someone else can post a Fritzing pictogram using the components you have.
I would encourage you to study device data sheets and learn the engineering language of schematic diagrams if you are interested in progressing in this field. Your use of a learning kit implies that you want to learn.

OP is using ESP32 NANO and NOT UNOR3; please, see post #1 and guide OP to follow the tested sketch of post #6.

Yes, see the last paragraph of post #17.