Hi I use ESP32 board, and when I connect the DS18B20U sensor it shows the temperature to me -37 at room +24 and when I heat it the temperature Increases.
#include <OneWire.h> // Библиотека протокола 1-Wire
#include <DallasTemperature.h> // Библиотека для работы с датчиками DS*
#define ONE_WIRE_BUS 23 // Шина данных на 10 пине
OneWire oneWire(ONE_WIRE_BUS); // Создаем экземпляр объекта протокола 1-WIRE - OneWire
DallasTemperature sensors(&oneWire); // На базе ссылки OneWire создаем экземпляр объекта, работающего с датчиками DS*
void setup(void)
{
Serial.begin(9600); // Настраиваем Serial для отображения получаемой информации
sensors.begin(); // Запускаем поиск всех датчиков
}
void loop(void)
{
Serial.println("Requesting temperatures...");
sensors.requestTemperatures(); // Запускаем измерение температуры на всех датчиках
// Когда температура измерена её можно вывести
// Поскольку датчик всего один, то запрашиваем данные с устройства с индексом 0
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
}
```
@antons15
Thry the following sketch which does functional check on a DS18B20 temperature sensor.
//12-bit default resolution; external power supply
#include<OneWire.h>
OneWire ds(23); //IO Pin-23 with which the data line of the senosr is connected.
byte addr[8]; //to hold 64-bit ROM Codes of DS1
byte data[9]; //buffer to hold data coming from DS18B20
void setup()
{
Serial.begin(9600);
ds.reset();
ds.search(addr); //collect 64-bit ROM code from sensor
}
void loop()
{
//----------------------------
ds.reset(); //bring 1-Wire into idle state
ds.select(addr); //slect with DS18B20 sensor with address in addr[] array
ds.write(0x44); //conversion command
delay(1000); //insert max conversion time or poll status word
//---------------------------
ds.reset();
ds.select(addr); //selectimg the desired DS18B20
ds.write(0xBE); //Function command to read Scratchpad Memory (9-byte)
ds.read_bytes(data, 9); //data comes from DS and are saved into buffer data[]
//---------------------------------
int16_t raw = (data[1] << 8) | data[0]; //---data[0] and data[1] contains temperature data : 12-bit resolution-----
float myTemp = (float)raw / 16.0; //12-bit resolution
Serial.println(myTemp, 2); //show two-digit after decimal point
}
Requesting temperatures...
Temperature for the device 1 (index 0) is: 28.00
Requesting temperatures...
Temperature for the device 1 (index 0) is: 28.00
Requesting temperatures...
Temperature for the device 1 (index 0) is: 28.00
Requesting temperatures...
Temperature for the device 1 (index 0) is: 28.00
Requesting temperatures...
Temperature for the device 1 (index 0) is: 28.06
Requesting temperatures...
Temperature for the device 1 (index 0) is: 28.00
Requesting temperatures...
Temperature for the device 1 (index 0) is: 28.06
....
The temperature here is 28.xx now.
I have use MEGA.
I think your sensor is measuring your hand temperature.
That's the sign of a total lack of communication between the Arduino and the sensor. So first check your connections and pullup, scope the signal pin to see if there's communication going on etc.
That's a good thing to have confirmed.
Like I said, his problem is likely on the hardware side or a very fundamental software issue. The DS18B20 library always reports -127 if the data readout fails altogether and only 0's are read by the host.
Yeah, that might be the problem. It could also be a short between adjacent pins that shouldn't be shorted. He's using an smd SOP-8 (if memory serves and my eyes don't lie to me) which is kind of challenging to suspend in the air like this with pins soldered to it....I wouldn't be surprised if there's a short there somewhere.