Arduio + DS18B20 + LCD = eek!

by paulfos on Thu Aug 06, 2009 8:48 am

Hey all,

Was hoping you guys could help me out here... i'm stuck!

I have a 16x2 LCD hooked up to an arduino (exact to lady ada's picture below with one exception... the 5th pin on the LCD is connected to ardunio digital input #4 [haktronics says pin 5 is used])
http://www.adafruit.com/images/large/LC ... rd_LRG.jpg

I have plugged in a DS18B20 Digital Temperature sensor in. (http://www.sparkfun.com/commerce/produc ... cts_id=245 datasheet link on same page also)
I was going to use an analog temp sensor that I bought from adafruit (i have both analog & digital) but think that since i'm running its 3 wires up and down a 25ft Cat6 cable, digital will be more accurate?

On the DS18B20, I have pin 1 to ground, pin 3 to 5v, and pin 2 to digital input #1 on the arduino.

I have used ladyada.net and http://www.hacktronics.com/Tutorials/ar ... orial.html as resources for this (i converted [hopefully correctly] the pin placements from haktronics diagram -> ladyadas... i've modified the code from haktronics to work with ladyada's LCD wiring below:

Code: Select all | TOGGLE FULL SIZE
#include <OneWire.h>
#include <LiquidCrystal.h>

// LCD Thermostat
// www.hacktronics.com

LiquidCrystal lcd(7, 4, 8, 9, 10, 11, 12);
int backLight = 13;    // pin 13 will control the backlight

OneWire  ds(1);        // ds18b20 pin #2 (middle pin) to Arduino pin 8

byte i;
byte present = 0;
byte data[7];
byte addr[1];
 
int HighByte, LowByte, SignBit, Whole, Fract, TReading, Tc_100, FWhole;

void setup(void) {
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  lcd.clear();                  // start with a blank screen
  lcd.setCursor(0,0);           // set cursor to column 0, row 0
 
    if ( !ds.search(addr)) {
      lcd.clear(); lcd.print("No more addrs");
      delay(1000);
      ds.reset_search();
      return;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      lcd.clear(); lcd.print("CRC not valid!");
      delay(1000);
      return;
  }
}

void getTemp() {
  int foo, bar;
 
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);
 
  present = ds.reset();
  ds.select(addr);   
  ds.write(0xBE);

  for ( i = 0; i < 9; i++) {
    data[i] = ds.read();
  }
 
  LowByte = data[0];
  HighByte = data[1];
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
 
  if (SignBit) {
    TReading = -TReading;
  }
  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 (Fract > 49) {
    if (SignBit) {
      --Whole;
    } else {
      ++Whole;
    }
  }

  if (SignBit) {
    bar = -1;
  } else {
    bar = 1;
  }
  foo = ((Whole * bar) * 18);      // celsius to fahrenheit conversion section
  FWhole = (((Whole * bar) * 18) / 10) + 32;
  if ((foo % 10) > 4) {            // round up if needed
       ++FWhole;
  }
}

void printTemp(void) {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp is: ");
  lcd.setCursor(0,1);   
 
  if (SignBit) { 
     lcd.print("-");
  }
  lcd.print(Whole);
  lcd.print(" C / ");
  lcd.print(FWhole);
  lcd.print(" F");
}

void loop(void) {
  getTemp();
  printTemp();
  delay(1000);
}


I am getting an output to the screen... but its always:
Temp is:
0*C / 32 F

its as if there is not temperature sensor connected.

Any help would be much appreciated!
Thanks.
paulfos
 
Posts: 21
Joined: Thu Jul 31, 2008 7:47 pm

Re: Arduio + DS18B20 + LCD = eek!

by adafruit on Thu Aug 06, 2009 1:40 pm

well at least the LCD works, did you EVER get the temp sensor to work?
User avatar
adafruit
 
Posts: 11663
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: Arduio + DS18B20 + LCD = eek!

by paulfos on Thu Aug 06, 2009 1:55 pm

how would i go about just testing the temp sensor?

and am i correct in thinking that sense the sensor is at the end of a 25ft wire, it is better to use a digital sensor rather then an analog? or is 25ft too short to consider voltage loss?
paulfos
 
Posts: 21
Joined: Thu Jul 31, 2008 7:47 pm

Re: Arduio + DS18B20 + LCD = eek!

by adafruit on Thu Aug 06, 2009 1:58 pm

Serial.print() of course
and, -obviously- you should just test the sensor without a 25 foot cable AND also make sure that the cable is assembled right...
right? :wink:
User avatar
adafruit
 
Posts: 11663
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: Arduio + DS18B20 + LCD = eek!

by Zener on Thu Aug 06, 2009 2:10 pm

Another solution to a long cable is use a current mode sensor (varies a current). The current is unaffected by cable length. I wold think the mfg (Maxim?) would provide guidance on cable selection and length. As was said, test without the long cable.
Zener
 
Posts: 2516
Joined: Sat Feb 21, 2009 1:38 am

Re: Arduio + DS18B20 + LCD = eek!

by paulfos on Thu Aug 06, 2009 2:42 pm

oh yes, i'm not trying to get it working with the 25ft cable!

Forgive my noobness here... I got my first arduino last night.

If i just want to test if the sensor is working i can run this code:
http://serverwillprovide.com/icuubi/exa ... S18B20.pde
which will print the temperature sensors readings to a serial port..

I have installed the FTDI mac usb-serial driver, (http://www.ladyada.net/learn/arduino/lesson0-mac.html) ... i've tried googling how to read the serial port data from the Terminal inside Mac OSX.. but have got nothing... Is it possible, or should i just boot into windows and use the command prompt? (in windows i assume i'll have to installed the FTDI drivers too?) and what command do I use to read it?
paulfos
 
Posts: 21
Joined: Thu Jul 31, 2008 7:47 pm

Re: Arduio + DS18B20 + LCD = eek!

by adafruit on Thu Aug 06, 2009 2:48 pm

please spend a week learning arduino by reading a book or the tutorials (that i spent a lot of time on)
if you cant get it working in a week, post again!
User avatar
adafruit
 
Posts: 11663
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: Arduio + DS18B20 + LCD = eek!

by paulfos on Fri Aug 14, 2009 8:07 pm

Got it working now, sorry for the premature thread posting.

Had to tinker with the code, and get a pull up resistor on the temp sensor.
paulfos
 
Posts: 21
Joined: Thu Jul 31, 2008 7:47 pm

Re: Arduio + DS18B20 + LCD = eek!

by adafruit on Fri Aug 14, 2009 8:50 pm

hooray! reading datasheets FTW :D
User avatar
adafruit
 
Posts: 11663
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: Arduio + DS18B20 + LCD = eek!

by franklin97355 on Sat Aug 15, 2009 11:11 pm

It might be helpful to others if you were to post the ammended code. Thanks.
User avatar
franklin97355
 
Posts: 4375
Joined: Mon Apr 21, 2008 1:33 pm

Re: Arduio + DS18B20 + LCD = eek!

by khelms on Fri Jan 04, 2013 7:23 pm

Do you have the new code? I looked through it and couldnt figure out what was wrong either. Now i have tunnel vision and cant get past it.
khelms
 
Posts: 10
Joined: Fri Dec 21, 2012 9:19 am

Re: Arduio + DS18B20 + LCD = eek!

by khelms on Fri Jan 04, 2013 7:32 pm

This works the same way. No problem.

[Edit - moderator - please use the 'code' button when submitting code]

Code: Select all | TOGGLE FULL SIZE
// LCD Thermostat
// http://www.hacktronics.com

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

// Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int backLight = 13;    // pin 13 will control the backlight

OneWire  ds(8);        // ds18b20 pin #2 (middle pin) to Arduino pin 8

byte i;
byte present = 0;
byte data[12];
byte addr[8];
 
int HighByte, LowByte, SignBit, Whole, Fract, TReading, Tc_100, FWhole;

void setup(void) {
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  lcd.begin(2,16);              // rows, columns.  use 2,16 for a 2x16 LCD, etc.
  lcd.clear();                  // start with a blank screen
  lcd.setCursor(0,0);           // set cursor to column 0, row 0
 
    if ( !ds.search(addr)) {
      lcd.clear(); lcd.print("No more addrs");
      delay(1000);
      ds.reset_search();
      return;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      lcd.clear(); lcd.print("CRC not valid!");
      delay(1000);
      return;
  }
}

void getTemp() {
  int foo, bar;
 
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);
 
  present = ds.reset();
  ds.select(addr);   
  ds.write(0xBE);

  for ( i = 0; i < 9; i++) {
    data[i] = ds.read();
  }
 
  LowByte = data[0];
  HighByte = data[1];
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
 
  if (SignBit) {
    TReading = -TReading;
  }
  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 (Fract > 49) {
    if (SignBit) {
      --Whole;
    } else {
      ++Whole;
    }
  }

  if (SignBit) {
    bar = -1;
  } else {
    bar = 1;
  }
  foo = ((Whole * bar) * 18);      // celsius to fahrenheit conversion section
  FWhole = (((Whole * bar) * 18) / 10) + 32;
  if ((foo % 10) > 4) {            // round up if needed
       ++FWhole;
  }
}

void printTemp(void) {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp is: ");
  lcd.setCursor(0,1);   
 
  if (SignBit) { 
     lcd.print("-");
  }
  lcd.print(Whole);
  lcd.print(" C / ");
  lcd.print(FWhole);
  lcd.print(" F");
}

void loop(void) {
  getTemp();
  printTemp();
  delay(1000);
}
khelms
 
Posts: 10
Joined: Fri Dec 21, 2012 9:19 am