Photon Maker Kit - DS18B20 - temperature sensing issues/questions


#1

I purchased the Photon maker kit and am attempting to use the DS18B20 temperature sensor that comes with the kit. (I’m kind of new to the photon but did get another sensor to work so am not a total newb.) Debugging indicates that the photon isn’t detecting the presence of the DS18B20 and is returning a temp of -127.0.

I may have a combination of wiring and software issues going, and want to make sure the wiring is correct before looking at the code.

  • All the info I’ve found indicates that a 5k-ohm pull-up resistor is needed, but it doesn’t appear that this resistor came with the kit. I may have missed something, but why would the kit not come with that resistor if it’s required for use with the DS18B20? If I need to buy it, where can I get it?

  • Also, should the DS18B20 Vdd pin be grounded or supplied with voltage? I’ve seen it done both ways so am unsure which is correct…

Thanks in advance,
Paul


#2

I’m not completely sure what resistors come with the kit, but I would be surprised if there where no 4k7 resistors - and you can try other values between 2k and 10k too (or two 10k parallel will give you a 5k).

Whey you say you only found that didn’t really help, have you seen these
e.g.
DS18B20 Dallas Temperature readings jumping high at random intervals
DS18B20 is throwing -127C from time to time
Getting correct address of DS18B20

or any of the other threads dealing with DS18B20 (search box top left)

About the wiring you could look here
https://github.com/tomdeboer/SparkCoreDallasTemperature (online library for Particle Build/Web IDE)
https://docs.particle.io/assets/datasheets/makerkit/DS18B20.pdf (datasheet linked in the Maker Kit docs)


#3

I’ve been having the exact same issue as you, @pwerner.

It’s quite frustrating… the sensor seems to be reporting 0.00 for me using the test .ino that’s in the library for the DS18B20.

This shows how I’ve wired it up:

I’m using one of the 10k resistors that came in the kit. As you can see here, @ScruffR : https://docs.particle.io/datasheets/photon-shields/#resistors-30- the kit doesn’t come with any 4k7 resistors (though that is what I’ve seen recommended on the Adafruit site, for example).

@pwerner Did you ever get it working?
@ScruffR Any suggestions for an improvement to my wiring?


#4

If you look at the datasheet I linked above you might see that you’ve got Vdd and GND swapped round.


#5

I do?! Oh wow… I misread the datasheet then.

When I swap them and power it up though, the RGB LED just lights up red. The way I have them, it boots but only reads 0.00


#6

Just tried two 10k resistors in sequence too, and no change…

Still reads 0.00


#7

Hold a min, I think I misread (missed the “BOTTOM VIEW”) the datasheet now :blush:
I’ll have another look.


#8

I switched from using the DS18B20 library in Particle Build, to the one @ScruffR mentions above ( https://github.com/tomdeboer/SparkCoreDallasTemperature ). Now instead of 0.00, I get -127.00 (with both setups of 1x 10k resistor, or 2x 10k resistors).

So I don’t know that I have any new information, but I’ve at least tried another approach :wink:


#9

I had another look at your wiring and I wonder why I missed this and rather got on the wrong track with Vdd/GND :blush:

Your resistor is in series with your signal but should actually be a pull-up.
So put one 10k from 26i to 20i on the breadboard, another 10k from 26h to 20h, the blue wire from 25j to 20j and the green from 20f to 11j (D2 on the Photon)


#10

That’s done it! (I know you were going to post a pic… but I ran with it :wink: )


#11

Still throws an occasional -127, but most of the reads are 27.31 °C, which seems right!


#12

It’s kind of awkward to see properly, but this is the layout I have now (working).

For clarity, the two resistors run between the red wire on line 26 and the blue one on line 20. (I think that’s the only part you can’t really see, as the temp sensor is in the way!)

Thanks so much @ScruffR!! :smiley:


#13

In a somewhat nicer picture, that’d look like this :wink:


#14

That’s not uncommon. Be mindful of how quickly you are polling the sensor and I suggest catching those bad reads and throwing them out. It can be a simple if statement or you can check for CRC errors if the lib you are using supports CRC checks (redundant redundancy there, like NIC Card one of my all time pet peeves).


#15

Excellent tips, thanks both!

The library I’m using does CRCs and samples every 2.5 seconds.

Very happy with these results!


#16

I also really struggled with this one for a long time before finding this thread.
I tried different resistors, capacitors and even different cables to no avail.

As a work around (I’m new to this type of stuff, so it isn’t perfect…)
I created a check in the form of an If statement then set it to ignore any value less than -50C. Have been testing it for a good 15-20minutes and so far so good.

Code for anyone else that needs it.
Please note - this code was originally from the book under file: p-10-thermometer-dallas.ino

    // This #include statement was automatically added by the Particle IDE.
#include "OneWire/OneWire.h"

// This #include statement was automatically added by the Particle IDE.
#include "spark-dallas-temperature/spark-dallas-temperature.h"

#include "OneWire/OneWire.h"
#include "spark-dallas-temperature/spark-dallas-temperature.h"

double tempC = 0.0;
double tempF = 0.0;
//New variable added to check the Temp before assigning a new value to tempC
double tempCheck = 0.0;

int tempSensorPin = D2;

OneWire oneWire(tempSensorPin);
DallasTemperature sensors(&oneWire);

void setup() {
    sensors.begin();
    Spark.variable("tempc", &tempC, DOUBLE);
    Spark.variable("tempf", &tempF, DOUBLE);
    //Added in a new tempCheck variable - not sure if this is needed, but followed for consistency
    Spark.variable("tempCheck", &tempF, DOUBLE);
}

void loop() {
  sensors.requestTemperatures();
  //This next line assigns the current temp to the 'Check variable'
  tempCheck = sensors.getTempCByIndex(0);
  //This statement checks to see if the temp is less than -50 deg C - I selected -50C as the sensor for me isn't going to get that cold
  //You can select a different minimum temp, I avoided setting it to -127 as I wasn't sure if there were rounding or decimal issues which I didn't bother checking for
  if ( tempCheck < -50 )
  {
      //The below statement leaves tempC as is if the sensor reports < -50C
      tempC = tempC;
      delay(1000);
  }
  else
  {
    tempC = tempCheck;
    tempF = tempC * 9.0 / 5.0 + 32.0;
  }
}

I hope this helps anyone else who can’t seem to fix the problem at the source.
If there is a better way to write a check statement - please let me know.

Cheers,


#17

I have a few suggestions to clean up your code. You have your two libraries included twice, so you should remove one set of those. There’s no reason to create the Spark.variable, “tempCheck” since the variable it monitors is the same one you’re looking at with “tempf”. The syntax you’re using for the Spark variables is the old syntax; there’s no need for the ampersand or the type any more (should also switch to Particle from Spark, although they both work). The variable tempCheck doesn’t need to be global since you only use it within the loop function as a temporary place to hold the temperature value until you do the validity check. The validity check can be done with a single “if” statement, no need for an “else”; the statement tempC = tempC doesn’t do anything, so is not needed.

#include "OneWire/OneWire.h"
#include "spark-dallas-temperature/spark-dallas-temperature.h"

double tempC = 0.0;
double tempF = 0.0;
int tempSensorPin = D2;

OneWire oneWire(tempSensorPin);
DallasTemperature sensors(&oneWire);

void setup() {
    sensors.begin();
    Particle.variable("tempc", tempC);
    Particle.variable("tempf", tempF);
}

void loop() {
  sensors.requestTemperatures();
  double tempCheck = sensors.getTempCByIndex(0);
  
  if ( tempCheck > -50 && tempCheck < 50) // might want to guard against a spurious high value as well
  {
    tempC = tempCheck;
    tempF = tempC * 9.0 / 5.0 + 32.0;
  }
  delay(1000);
}

Other ways to throw out bad values would be more robust. One way, if your timing allows you to do it, is to take multiple readings close together, put those values into an array, sort that array, then take the center value, or an average of several of the center values. This way, any spurious low or high values end up at the beginning or end of the array, and get thrown out. I gave an example of this here


Understanding the OneWire code
#18

Would you share your code and Library, or would you like to keep it personal. I just need a start. Your layout is great and I like the simplicity, but working on the code and Library as a slow programmer is tough. If you would like to share that would be nice. my email is aj36abt33@yahoo.com