Temperature Sensor Dallas DS18B20  Help Please

I see there is a working code for this guy and it needs Onewire library. However, I haven't learned the trick on what plugs into what. There are three legs but as I read you use only 2 and a resistor. Would somebody put me out of my misery?

Hank :-/ :-/

I am using DS18S20, but they have same connection method.

As pic below. To get it working in parasite mode, connect pins 1 and 3 to GND. Pin 2 to Arduino digital pin and 4k7 resistor between pin 2 and power.

                          __________
                         |          |
                         | DS18B20  |
       Vin               -----------
          |              1|  2|   3|
          |               |   |    |
          _               o---|----|
         | |              |   |
      4k7|_|            GND   |
          |                   |
          |                   |
uC -------o--------------------

Here's the datasheet:

http://datasheets.maxim-ic.com/en/ds/DS18B20.pdf

Thank you. That got it working on digital pin 10. I am using the sketch from the Arduino website. Here is what the output looks like. I am a little confused. As usual!

R=28 55 AA F6 1 0 0 86 Device is not a DS18S20 family device.
No more addresses.

Hank

It is a bit confusing documentation on Arduino OneWire library. There is mixed code for DS18S20 and DS18B20. But anyway...

The first bit on address states type of device
0x10 = DS18S20
0x28 = DS18B20
0x22 = DS1822

Change code

  if ( addr[0] != 0x10) {
      Serial.print("Device is not a DS18S20 family device.\n");
      return;
  }

to

  if ( addr[0] != 0x28) {
      Serial.print("Device is not a DS18B20 family device.\n");
      return;
  }

MaHa is right.. unless you get DS1820P+ which works only in parasite mode so the third leg is useless.
I posted code to display readings coming from two 18B20 to a 4 bit LCD.

MaHa:

That worked fine. I commented out everything but the temperature output and it seems to be around 71 F in this room. Much colder outside.

Anyone seen code to port this to LCD?

Thx

Hank

I have 2x16 LCD where I print temperature readings.
I am using Arduino library LiquidCrystal, to connect display and then print temperatures to LCD instead of Serial output.
Serial.print and lcd.print works the same way, except with lcd you have to set cursor position with setCursor to print text on the right place of display.

Thanks I got the LCD to work. Thanks.

Hi, So using bits and pieces of information from this forum and many other sites. I finally was able to get the temp. sensor to display the appropriate data on my computer, via serial. My next goal is to display that on my LCD so its own seperate unit.. After my brother and I, both who aren't experienced in the C++ language. We have got to a point where the lcd displays letters instead of numbers( F=70 degrees, I=73degrees). Any thoughts on how to change from chars to numbers?

#include <OneWire.h>
#include <LCD4Bit.h>

#define TEMP_PIN  3

void OneWireReset(int Pin);
void OneWireOutByte(int Pin, byte d);
byte OneWireInByte(int Pin);
LCD4Bit lcd = LCD4Bit(1);

void setup()
{
    Serial.begin(9600);
    pinMode(13, OUTPUT);  //we'll use the debug LED to output a heartbeat
    lcd.init();
  //  lcd.printIn("temperature measurement setup in progress\n");

    pinMode(TEMP_PIN, INPUT);      // sets the digital pin as input (logic 1)
    digitalWrite(TEMP_PIN, LOW);
    delay(100);
}

void loop()
{
  int HighByte, LowByte, TReading, SignBit, Tc_100, Tf_100, Whole, Fract;
  digitalWrite(13, HIGH);  //light the debug LED
  OneWireReset(TEMP_PIN);       // reset
  OneWireOutByte(TEMP_PIN, 0xcc); // skip ROM code
  OneWireOutByte(TEMP_PIN, 0x44); // perform temperature conversion, strong pullup for one sec

  OneWireReset(TEMP_PIN);         // reset
  OneWireOutByte(TEMP_PIN, 0xcc); // skip ROM code
  OneWireOutByte(TEMP_PIN, 0xbe); // Read scratchpad code (temperature in this case)

  LowByte = OneWireInByte(TEMP_PIN);
  //Serial.print("Low byte = ");
  //Serial.print( LowByte );
  //Serial.println();
  
  HighByte = OneWireInByte(TEMP_PIN);
  //Serial.print("High byte = ");
  //Serial.print( HighByte );
  //Serial.println();

  TReading = (HighByte << 8) + LowByte;
  //Serial.print("Raw temp = ");
  //Serial.print( TReading );
  //Serial.println();

  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's complement the answer
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;

  if (SignBit) // If its negative
  {
     lcd.printIn("-");
  }
  
  if (Fract < 10)
  {
     //lcd.printIn("0");
  }
  
  //lcd.print("C\n");

  Tf_100 = ((Tc_100 * 9) / 5) + 3200;
  Whole = Tf_100 / 100;  // separate off the whole and fractional portions
  Fract = Tf_100 % 100;

  
  if (SignBit) // If its negative
  {
     lcd.printIn("-");
  }
  lcd.clear();
  lcd.print(Whole);
  Serial.print(Whole);
  Serial.println();
  if (Fract < 10)
  {
     //lcd.printIn("0");
  }
  //lcd.print(Fract);
  
  //scroll entire display 20 chars to left, delaying 50ms each inc
  //lcd.leftScroll(20, 50);
  delay(2000);      // 2 second delay.  Adjust as necessary
  //lcd.clear();
}

void OneWireReset(int Pin) // reset.  Should improve to act as a presence pulse
{
     digitalWrite(Pin, LOW);
     pinMode(Pin, OUTPUT); // bring low for 500 us
     delayMicroseconds(500);
    
     pinMode(Pin, INPUT);
     delayMicroseconds(500);
}

void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
{
   byte n;

   for(n = 8; n != 0; n--)
   {
      if ((d & 0x01) == 1)  // test least sig bit
      {  // if it is "1", pull low for just 5uS
         //digitalWrite(Pin, LOW);
         pinMode(Pin, OUTPUT);
         delayMicroseconds(5);
        
         // then wait (external resistor pulls line back high)
         pinMode(Pin, INPUT);
         delayMicroseconds(60);
      }
      else
      {  //lsb is "0", so pull low for 60uS
         //digitalWrite(Pin, LOW);
         pinMode(Pin, OUTPUT);
         delayMicroseconds(60);
        
         // release, allowing external resistor to pull high
         pinMode(Pin, INPUT);
      }

      d = d >> 1; // now the next bit is in the least sig bit position.
   }
  
}

byte OneWireInByte(int Pin) // read byte, least sig byte first
{
    byte d, n, b;

    for ( n = 0; n < 8; n++ )
    {
        // digitalWrite(Pin, LOW);
        // pull pin low for 5uS
        pinMode(Pin, OUTPUT);
        delayMicroseconds(5);
        
        // release pin, allowing external resistor to pull line back high for 5uS
        pinMode(Pin, INPUT);
        delayMicroseconds(5);
        
        // DS18B20 either pulls line low, or leaves it high
        // find out which...
        b = digitalRead(Pin);
        
        // wait bit time
        delayMicroseconds(50);
        
        // assemble our collected bits
        d = (d >> 1) | (b << 7); // shift d to right and insert b in most sig bit position
    }
    return(d);
}

Try putting the temperature value into a float rather than splitting it between two byte values.
Version 13 of the ide will print it to serial and the lcd to 2 decimal places.
It will even handle the - numbers as well.

Gordon

I was wondering if you could elaborate a little more? Im having a little trouble trying to figure out where to change to a float?