DS18B20 Temperature Sensor

Arduino LCD Display Reading DS18B20 Temperature Sensor

by Lewis Loflin

This version of using an LCD display differs from the video in that we no longer are using the I2C version LCD controller. Due to changes in the Arduino compiler the original user supplied libraries no longer work. I wrote my own code which anyone can see how it works and modify at will.

A Sn74164 is connected to Arduino to operate the LCD 8-bit date lines. Only four connections, two to the LCD display and two to the Sn74164 are all that's needed to operate the display. The purpose here is to illustrate how to connect electronics to a micro-controller.

The routines enable one to display integers, floats, strings, and characters on the display. I also introduced two functions not even in the Arduino reference such as itoa() and dtostrf().




LCD display with 74164



#define Bit_out 12 
#define CLK 11
#define RS 7 
#define E 8

#include <OneWire.h>

#define Line1 0x80  // location LCD row 0 col 0 or line 1 LCD
#define Line2 0x80 + 0x40  // location row 1 col 0 or line 2 LCD

// on pin 4 (a 4.7K resistor is necessary)
OneWire  DS(4);  

void setup() {
  pinMode(Bit_out, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(RS, OUTPUT);
  pinMode(E, OUTPUT);

  digitalWrite(CLK, LOW); 
  digitalWrite(RS, LOW); // LCD in command mode default
  digitalWrite(E, HIGH);

  initLCD(); // se below
  delay(1000); // delay for DS18B20

}

void loop() {

  DS.reset();
  // skip address if only one sensor present
  DS.skip();
  DS.write(0x44);  // start conversion

  // defaults to 12-bit conversion
  // wait at least 759 mSec.
  delay(1000);
  


  DS.reset();
  DS.skip();  // one device present
  DS.write(0xBE);  // Read Scratchpad

  byte LowByte = DS.read();
  byte HighByte = DS.read();
  // convert two bytes to int
  int Treading = (HighByte << 8) + LowByte;
  // convert int to float
  float temperatureC = float(Treading) * 0.0625;
  setLcd(Line1);
  typeln("TC = ");
  typeFloat(temperatureC);
  
  // temp F = C  x  9/5 + 32
  float temperatureF = (temperatureC * 9) / 5 + 32;
  setLcd(Line2);
  typeln("TF = ");
  typeFloat(temperatureF);

} // end loop


// move cursor on LCD
void   setLcd(int x)   {
  writeCommand(x);
}

void   clrLcd(void)   {
  writeCommand(0x01); // clear
  writeCommand(0x02); // home
}


void typeFloat(float localFloat)   {
  
  char myBuffer[10];
  dtostrf(localFloat, 4, 2, myBuffer);
  typeln(myBuffer);
}


void typeInt(int k)   {
  char array1[10];
  itoa(k, array1, 10); // int to string
  typeln(array1);
}

void typeChar(byte val)   {

  ssrWrite(val);
  digitalWrite(RS, HIGH);
  pulsout(E);
  digitalWrite(RS, LOW);
}

void writeCommand(byte val)   {
  ssrWrite(val); // send byte to 74164
  digitalWrite(RS, LOW); // make sure RS in Com mode
  pulsout(E);
}

// Below we pass a pointer to array s.
void typeln(char *s)   {
  delayMicroseconds(1000);
  while (*s)  typeChar(*(s++));
}  // end typeln



// inverts state of pin, delays, then reverts state back
void    pulsout(byte x)   {
  byte z = digitalRead(x);
  delayMicroseconds(10);
  z = !z; // reverse state
  digitalWrite(x, z);
  z = !z; // return to original state
  digitalWrite(x, z);
} // end pulsout()

/*
To shift LSB out first:
 byte  temp = val & B00000001; 
 if (temp == 0x01) digitalWrite(Bit_out, HIGH); 
 else digitalWrite(Bit_out, LOW); 
 pulsout(CLK); 
 val = val >> 1; // shift one place right
 */

void ssrWrite(byte val)  {  // shift data to 74164
  for (int j=1; j<=8; j++)  {   // shift out MSB first
    byte  temp = val & B10000000; // MSB out first
    if (temp == 0x80) digitalWrite(Bit_out, HIGH); 
    else digitalWrite(Bit_out, LOW); 
    pulsout(CLK); 
    val = val << 1; // shift one place left
  }  // next j
}  // end byteWrite


/*
Hd44780 display commands:
 0x0f = initiate display cursor on blinking
 0x0c = initiate display cursor off
 0x01 = clear display fills display with spaces (0x20).
 0x02 = HOME returns to line one first character
 0x38 = 2 lines X 16 char 8 bits mode. Defaults to 1 line mode.
 0x10 = cursor left
 0x14 = cursor right
 0x18 = Shifts entire display left
 0x1c = Shifts entire display right 
 
 One can also go to a specific location.
 writeCommand(0x80); // begin on 1st line
 writeCommand(0x80 + 0x40); // begin on 2nd line 
 
 writeCommand(0x38); // setup for 2 lines
 writeCommand(0x0F); // blinking cursor
 
 writeCommand(0x02); // home
 writeCommand(0x01); // clear
 
 */


void initLCD(void)   {

  writeCommand(0x38); // setup for 2 lines
  writeCommand(0x0F); // blinking cursor
  writeCommand(0x01); // clear
  writeCommand(0x02); // home
}

You Tube Arduino Microcontroller Video Series March 2012: