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?

5
  • 2
    Could you make a change to a 'schematic' to show your connection? How do you actually connect 2 wires? (Your current schematic has 3 connections between sensor and Arduino, and sensor on your picture has only 2 wires..) Are you aware of the fact that you want to use 'parasite power mode'? In this case you must provide strong pull-up for temperature conversion time. Take a look at datasheets.maximintegrated.com/en/ds/DS18B20.pdf page 11, 'Convert T [44h]' paragraph...
    – smajli
    Nov 13, 2017 at 8:17
  • 3
    What type or name of the model is your temperature sensor.? It looks like you're trying to connect an analog sensor. But you use the connection schema and code for the digital sensor.
    – AltAir
    Nov 13, 2017 at 8:26
  • Which model temperature sensor you using? If you using lm35 temperature sensor, Serial.print(sensors.getTempCByIndex(0)/9,31); you need add /9.31 to your code, sensors not directly measure the temperature sensors just have variable resistor inside, resistor value changes by temperature after then voltage changes the arduino just measure the voltage, the voltage value divide the 9.31 this result gives temperature Celcius type. Also you have to unplug 4.7 k resistor it needless.
    – Buturlin
    Nov 13, 2017 at 8:31
  • Oh, indeed. karanrp: I have assumed you are using DS18B20. Check your sensor first.
    – smajli
    Nov 13, 2017 at 8:35
  • That looks like a cheap thermocouple from a digital thermometer to me. You need a special chip to run it.
    – Majenko
    Nov 13, 2017 at 11:09

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.