Tutorial code fails

I have just tried a DS18B20 digital temperature sensor using code from a tutorial site but it gives erroneous output and I have no idea why.

The code as I have it (slight modification adding oneWire.reset(); in setup() at line 50)

/*
-- filename   : oneWireDigital.ino
--
-- purpose    : first drive of DS18B20 digital temperatire sensor
--
-- derivation : http://www.hobbytronics.co.uk/ds18b20-arduino
--            : https://www.pjrc.com/teensy/td_libs_OneWire.html
--
-- version    : 0
--
-- date       : January 16, 2018
--
--            : THIS CODE IS IN THE PUBLIC DOMAIN
--
1234567890123456789012345678901234567890123456789012345678901234567890
_________1         2         3         4         5         6         7
*/

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

// general constants
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// debug switch
#define DEBUG true

// instantiations from libraries
// 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 sensor(&oneWire);


/*==========================================*/


void setup(void)
{
	// start serial port
	Serial.begin(9600);

	// tell 'em who we are, man
	Serial.println("oneWireDigital.ino");
	Serial.println();

	// initialize the libraries
	oneWire.reset();
	sensor.begin();
}  //  end setup()


void loop(void)
{
	// issue a global request to all devices on the bus
	Serial.print(" Requesting temperatures...");
	sensor.requestTemperatures();
	Serial.println("DONE");

 
	// there can be more than one IC on the same bus. 
	// 0 refers to the first IC on the wire
	Serial.print("Temperature for Device 1 is: ");
	Serial.println(sensor.getTempCByIndex(0));

	// wait a while
	if (DEBUG)
	{
		delay(3000);
	}
}  //  end loop()

It seems pretty straightforward but I get a wildly wrong temperature reading

Opening port
Port open
oneWireDigital.ino

 Requesting temperatures...DONE
Temperature for Device 1 is: -127.00
 Requesting temperatures...DONE
Temperature for Device 1 is: -127.00
 Requesting temperatures...DONE
Temperature for Device 1 is: -127.00
 Requesting temperatures...DONE
Temperature for Device 1 is: -127.00
 Requesting temperatures...DONE
Temperature for Device 1 is: -127.00
 Requesting temperatures...DONE
Temperature for Device 1 is: -127.00
 Requesting temperatures...DONE
Temperature for Device 1 is: -127.00
 Requesting temperatures...DONE
Temperature for Device 1 is: -127.00
 Requesting temperatures...DONE
Temperature for Device 1 is: -127.00
 Requesting temperatures...DONE
Temperature for Device 1 is: -127.00

I am sure the temperature is wrong. I am in Perth, not New York :slight_smile:

Can someone tell me what the problem is?
Thanks

Hi,
Can you post the code you are using please?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

It will help immensely.

Thanks.. Tom.. :slight_smile:

The circuit is the same as in the tutorial.
usingDS18B20-640x490.png
I posted the code.

Pictures taken at an angle are difficult to read but that breadboard layout looks very wrong. You seem to have power and ground shorted. What should be the pull-up resistor is connected across two of the DS pins. In short, it’s nothing like the tutorial circuit.

Steve

Hi,
Please check your wiring, have you got the 18B20 wired correctly, trace your gnd wire.
Where does the black wire go, and what is the loop of black wire doing?

Tom… :slight_smile: usingDS18B20-640x490edit.jpg

The loop of brown wire is shown in the top image on the tutorial webpage. You're right, it does short +ve and -ve and I wondered about that when I put it in. I took it out and it made no difference.

That's when I notices you'd labelled the UNO end of the red wire 'Gnd". I checked that and you were right! :-* It should have been 5 V! :roll_eyes: I fixed that - now it works.

Thanks, folks. Sorry to trouble you with a stupid mistake like that!

I am still puzzled as to why the original had that brown wire connecting +ve and -ve. As it turned out, I had it connecting Gnd and Gnd so it did nothing. :stuck_out_tongue:

Parasite power, only 2 wires needed. But I can't see which 2 on that pic.

vagulus:
I am still puzzled as to why the original had that brown wire connecting +ve and -ve. As it turned out, I had it connecting Gnd and Gnd so it did nothing. :stuck_out_tongue:

It didn't. It had a piece of wire connecting the two outer pins on the DS18B20. The 5V was connected to the end of the resistor which is in a completely different breadboard strip.

That's why most of us much prefer circuit diagrams to pictures or Fritzings. A diagram is more specific about connections.

Steve

slipstick:
It didn’t. It had a piece of wire connecting the two outer pins on the DS18B20. The 5V was connected to the end of the resistor which is in a completely different breadboard strip.

This is the current circuit
180117-UsingDS18B20-CurrentCircuit-536x640.png

The Brown wire, when it was used, connected the black to the red.

FTR: reinstalling the brown wire cause a jump of two degrees Celsius in the readout :o s

That's o.k. but you're now using the DS18B20 in a different mode from the tutorial you linked to. That had it connected in the recommended "Parasite Power" mode which has pins 1 and 3 connected together to ground and no direct 5V connection.

You're using it in external powered mode. Not a problem, it should still work.

Steve

slipstick:
… the recommended “Parasite Power” mode which has pins 1 and 3 connected together to ground and no direct 5V connection.

It was “out with the Sherlock Holmes style magnifying glass” and the ruination of what is left of my eyesight but this is what I finally figured those Hobbyworks people were up to:
180118-Hobbytronics DS18B10 Circuit-348x640.png
My wiring looks like this
180118-0947-Hobbytronics-DS18B20-Wiring-640x478.jpg
and closeup it looks like this.
180118-0947-Hobbytronics-Wiring-Close-640x478.jpg

I’m pleased to say that the Serial output of the temperature is right give or take a degree.

Thanks to all those who helped unravel this one.

Am I right in assuming that wired this way you only need a twisted pair?
Am I right in assuming that you could put a dozen of these sensors in parallel on a twisted pair and read them individually with no problems?

vagulus:
Am I right in assuming that you could put a dozen of these sensors in parallel on a twisted pair and read them individually with no problems?

Parasite power mode becomes problematic with a large number of devices on the OneWire bus (and also over long distances). You'll have to try it and see how well it works. The alternatives are to use powered mode or to instantiate multiple OneWire and DallasTemperature objects and divide your sensors among a couple of Arduino GPIO pins.