Dallas 18b20 not updating

Hi there forum,

Feeling frustrated is usually the edge of learning something... and I'm frustrated.... feeling a bit stupid even. Hope you guys can make me learn. Guess my bigest problem is with C, programming for the arduino is my first experience with C.

I've been playing around with all sorts of sensors, my latest "project" is reworking a sample sketch to get a function which can give me the reading of a Dallas 18c20. Not exactly rocket science, but I seem to have goofed somewhere. And I can't find where.

The sample sketch I've got reads the temperature just fine... if I put my finger on the sensor the reading goes up, if I take my finger away the reading goes down. Just as you would expect.

My own sketch gives a reading... and that reading never changes.

This is the sketch

// ds18b20 stuff
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress MyDallas = { 0x28, 0x32, 0x11, 0x4E, 0x07, 0x00, 0x00, 0xA3 };



void setup(void) {
  // put your setup code here, to run once:
  Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(MyDallas, 10);
}

void getTemp(DeviceAddress deviceAddress){
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print(tempC);
  //return tempC;
}

void loop(void) {
  // put your main code here, to run repeatedly:
  delay(2000);
  //float temp = getTemp(MyDallas);
  Serial.print("Temperatuur is: ");
  //Serial.print(temp);
  getTemp(MyDallas);
  Serial.print(" graden C\n\r");
 // temp = 0;
}

I've already build the getTemp() function back to a void function printing the value. It should return the reading as a float. But that didn't make a difference.

At the moment I'm quite at a loss on what the problem is. Any help - even a rtfm (with pointer to) - would be apreciated.

Peter

You aren't requesting a temperature conversion.
In setup() add this:

 sensors.setWaitForConversion(false);

and in loop(), put this before the delay:

 sensors.requestTemperatures();

Pete

PeterKaagman:
My own sketch gives a reading... and that reading never changes.

And that reading is?

Putting the sensor on the Rx pin 0 can't possibly be a good idea. Use pin 3 like everybody else does.

More typical code for the address is

byte Thermo1[8] = {0x28, 0x39, 0xFD, 0x50, 0x04, 0x00, 0x00, 0X69};

I don't think your request is right but I'm going to bed

Try adapting this

/* Basic 2xDS18B20 code for serial monitor, bluetooth, Excel or w.h.y.
 Derived from Hacktronics. Use their address sniffer and substitute your 
 numbers. Use Hacktronics connections diagram. 
 Stay away from using parasite power
 -127C means bad connection
 85 means you haven't gotten a read yet, probably wrong order of commands
 Use your own LCD connections
 */

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(8,9,A2,5,6,7); 

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

byte Thermo1[8] = {
  0x28, 0x39, 0xFD, 0x50, 0x04, 0x00, 0x00, 0X69};
byte Thermo2[8] = {
  0x28, 0x09, 0xA9, 0xC0, 0x03, 0x00, 0x00, 0x95};
float tempC,Temp1,Temp2;  

void setup(){

  Serial.begin(9600);
  sensors.begin();
  lcd.begin(16, 2);
  lcd.print("temp in     out");  

  delay(500);//Wait for newly restarted system to stabilize
  
/* // No resolution command means default to 12 bit
  sensors.setResolution(Thermo1, 10); 
  sensors.setResolution(Thermo2, 10);
*/
}

void loop() {
  sensors.requestTemperatures();  // call readings from the addresses
  Temp1 = sensorValue(Thermo1);
  Temp2 = sensorValue(Thermo2);  

  Serial.println("      Temp1 = ");
  Serial.print(Temp1);
  Serial.print("      Temp2 = "); 
  Serial.print(Temp2);

  lcd.setCursor (1,1);
  lcd.print (Temp1);
  lcd.setCursor (11,1);
  lcd.print (Temp2); 

  delay(1000);
}

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  tempC = sensors.getTempC (deviceAddress);
  return tempC;
}

This

void loop(void)
{ 
  delay(2000);
  Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();
  
  Serial.print("Inside temperature is: ");
  printTemperature(insideThermometer);
  Serial.print("\n\r");
}

is from the sample sketch I've got. and it does have the "sensors.requestTemperatures()" call in the loop. Seems I managed to mis that call.

Thanks for pointing that out for me :smiley:

That version uses the blocking form of requestTemperatures - it will wait until the conversion is done before returning. If you aren't trying to multitask, it's probably best and is the default in which case, remove the setWaitForConversion(false).

You don't need to change your declaration of MyDallas. The definition of DeviceAddress implies MyDallas[8].

Pete

Works like a dream el_

thanks!