The DS18B20 Sensor.

I have been working on a sketch to control various aspects of a greenhouse environment for some time now. The most problematical aspect probably involves the DS18B20 sensor. There’s lots of code available but most of it seems to result in unreliable readings. There is reliable code available (http://playground.arduino.cc/Learning/OneWire), but it identifies device parameters with a lengthy bit of instructions which are unnecessary to me.

In trying to adapt it for my use I have hacked at it and this is the result:

#include <OneWire.h>
// DS18S20 Temperature chip i/o
  OneWire ds(10);  // on pin 10
void setup(void) {
  // initialize inputs/outputs
  // start serial port
  Serial.begin(9600);
}

void loop() {
  int HighByte, LowByte, TReading, SignBit, Tc_100;
  float temperature;
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];

  ds.reset_search();
  if ( !ds.search(addr)) {
    
   ds.reset_search();
     return;
 }

  for( i = 0; i < 8; i++) {
   }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end

  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.

  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
   
  }
  
  LowByte = data[0];
  HighByte = data[1];
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

  temperature=(float) Tc_100/100;
  
  Serial.println(temperature);

I need the temperature variable at the end which I get but as a beginner I’d love to understand exactly how the sketch works and if I can delete any more of the original sketch without detriment, commenting out any further lines seems to say no!
It would be useful to retain the original procedure to detect several device IDs in case I add them later but I don’t understand how to do this.

The best approach is to go through the sketch line by line, until you understand exactly what each line does. Then you will understand what is necessary and what is not. That includes studying the documentation of the library code (which may the comments in the code).

It is also essential to consult the DS18B20 data sheet.

If you have specific questions on one or more details, post them.

Adrifran39: The most problematical aspect probably involves the DS18B20 sensor. There's lots of code available but most of it seems to result in unreliable readings.

This is utter nonsense. There is quite a bit of old junky code out there, but even that can work. The only way you get [u]unreliable[/u] readings from a DS18B20 is through user incompetence.

There is reliable code available (http://playground.arduino.cc/Learning/OneWire), but it identifies device parameters with a lengthy bit of instructions which are unnecessary to me.

I'm not sure why that is, but I guess that is one example of the antiquated junk code - even if it does work.

You might find this more helpful

http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

Note that it involves the use of [u]two[/u] programmes, one to obtain the address of the device, and the other to use it. The value of this method is that it uses a library specifically for the DS18B20 - a library that the folks at Arduino seem to know about but don't to want to tell you about.