Arduino + DS18B20 + I2C LCD

Since I had all of the parts on hand....

iPhone photo - Arduino Nano + DS18B20 + I2C LCD

iPhone photo - Arduino Nano + DS18B20 + I2C LCD

Since I already put together this breadboard with some building blocks for an RF project it has become a nice platform for a couple of other learning projects. A couple of nights ago I used it for figuring out how to use an I2C LCD. Tonight I removed the rotary encoder from the bottom right section of the board and installed a DS18B20 temperature sensor. You can Google for the pinout of the DS18B20 as it is a pretty simple layout. The data is being sent to Digital Pin 2.

I found an initial code sample at:
http://bildr.org/2011/07/ds18b20-arduino/

The initial code wrote the temperature (in celsius) to the serial monitor.

Screen Shot 2013-01-31 at 9.36.47 PM.png

The next step was to merge this code with the code for the I2C LCD from Sunday night. The output of that sketch was the first image that you saw in this blog post.  The code is displayed below.

//Initial I2C LCD example from:
// http://arduino-info.wikispaces.com/LCD-Blue-I2C

// Initial DS18B20 example from:
// http://bildr.org/2011/07/ds18b20-arduino/

// Read the DS18B20 and then write to an I2C LCD and serial monitor
// http://www.hoaglun.com/blog/2013/1/31/arduino-ds18b20-i2c-lcd



#include <Wire.h>
#include <OneWire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display


int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2

//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2

void setup(void) {
Serial.begin(9600);
lcd.init(); // initialize the lcd

// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Nano+I2C+DS18B20");
// lcd.setCursor(0, 1);
// lcd.print("Second Line");



}

void loop(void) {
float temperature = getTemp();
Serial.println(temperature);
lcd.setCursor(0, 1);
lcd.print(temperature);


delay(100); //just here to slow down the output so it is easier to read

}


float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius

byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}

if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}

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

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


for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}

ds.reset_search();

byte MSB = data[1];
byte LSB = data[0];

float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;

return TemperatureSum;

}

My total time on this project tonight was about 30 minutes. These are a couple of building blocks for a future project.  This particular breadboard is headed for some DDS and RF related projects in the near future.

73 de NG0R