1

In my current project I am reading temperature through sensor which is having only two wires.

Temperature Sensor

To read sensor values, I have used below schematic which I have found on Arduino Project Hub site.

ds1820b connection

In my project I am using below Arduino program.

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

// Data wire is plugged into pin 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);

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 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
    delay(1000);
}

After doing all this I am getting this output on serial monitor.

Result

At which step I am doing wrong?

karanrp
  • 396
  • 4
  • 13

1 Answers1

1

The -127 for your temperature reading is actually the error code for DEVICE_DISCONNECTED_C.

In the library's example code, they put an if that checks for this error code before printing the temperature:

if(tempC != DEVICE_DISCONNECTED_C) 
{
  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(tempC);
} 

Since your sensor has only 2 wires, according to Wikipedia, you need a capacitor to power the sensor:

One distinctive feature of the bus is the possibility of using only two wires — data and ground. To accomplish this, 1-Wire devices include an 800 pF capacitor to store charge and power the device during periods when the data line is active.

This should hopefully resolve your issues.

Dat Ha
  • 2,943
  • 6
  • 24
  • 46