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…
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).
I had another look at your wiring and I wonder why I missed this and rather got on the wrong track with Vdd/GND
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)
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!)
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).
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.
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
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