Nick_Pyner:
There are really only two things to go wrong - the software, and the hardware. The latter includes the wiring.
If you are using established software, i.e. the standard libraries and a sketch known to be kosher from Hacktronics or the like, the problem has to be hardware. If the wiring is OK (?) the sensor may well be a dud - because it has suffered from recent user abuse. Trust me, the DS18B20 can take a lot of abuse, but your options for a working solution are limited.So, what is the code? and do you have a virginal DS18B20 to hand?
Here is my full code... some of the stuff is from the thermistor code, so I have commented it out. A lot of the code is for driving my 7-seg display. My sensor code is all in the setup and loop functions at the bottom.
The sensor I am currently using is virginal.
#include <OneWire.h>
#include <DallasTemperature.h>
#define A 16
#define B 8
#define C 4
#define D 2
#define E 1
#define FP 32
#define G 64
#define ZERO (A|FP|B|E|C|D)
#define ONE (B|C)
#define TWO (A|B|G|E|D)
#define THREE (ONE|A|G|D)
#define FOUR (FP|B|G|C)
#define FIVE (A|FP|G|C|D)
#define SIX (A|FP|G|C|D|E)
#define SEVEN (ONE|A)
#define EIGHT (ZERO|G)
#define NINE (A|B|C|D|FP|G)
//#define RANGE 56
int analogPin = 0;
int tempRead = 0;
int leftOn = 10;
int rightOn = 11;
int index;
int count = -1;
int sample = 0;
/*int temps[RANGE] = {715,707,698,689,680,670,661,651,641,631,621,611,600,590,579,568,557,546,535,523,512,500,489,477,466,454,442,431,419,407,395,384,372,361,349,338,327,316,305,294,283,273,263,253,243,233,224,214,205,196,188,179,171,163,156,148};*/
int pins[8] = {3,4,5,6,7,8,9,12};
byte encoded[10] = {ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE};
byte addr[8];
OneWire ts(2);
DallasTemperature temp(&ts);
byte convertInt(int num)
{
return encoded[(int)num];
}
void draw(byte digit)
{
for (int i = 0; i < 7; i++)
{
if (((digit >> i) & 1) == 1)
{
digitalWrite(pins[i], LOW);
}
else
{
digitalWrite(pins[i], HIGH);
}
}
}
void writeNumber(int num){
byte right;
byte left;
if (num > 10)
{
left = convertInt(num/10);
}
else
{
left = 0;
}
right = convertInt(num%10);
if(num < 0)
{
digitalWrite(pins[7], HIGH);
}
else
{
digitalWrite(pins[7], LOW);
}
digitalWrite(rightOn, LOW);
digitalWrite(leftOn, HIGH);
draw(left);
delay(10);
digitalWrite(leftOn, LOW);
digitalWrite(rightOn, HIGH);
draw(right);
delay(10);
}
void setup() {
int i;
for(i = 3; i < 13; i++) {
pinMode(i, OUTPUT);
}
Serial.begin(115200);
temp.begin();
// Grab a count of devices on the wire
int numberOfDevices = temp.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (temp.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
}
void loop() {
if(count == -1 || count++ == 500)
{
Serial.print("Requesting temperatures...");
temp.requestTemperatures();
Serial.println("DONE");
tempRead = temp.getTempCByIndex(0);
/*if (tempRead == -127)
{
sample = 0;
}
else
{
sample = tempRead;
}*/
Serial.print("Temperature is: ");
Serial.println(tempRead, DEC);
count = 0;
}
writeNumber(sample);
}