16×2 LCD display and DS18B20

Here I will show you a small circuit, how you combine a 16×2 LCD display with the DS18B20 temperature sensor.

You need only three additional parts (additional to the sensor and the display): 1x 4.7 KOhm resistor, 1x 1 KOhm resistor and 1x 100 KOhm resistor (changeable).

First, this is our goal:

20160519_201631

On the left side is the DS18B20 temperature sensor. In this case with a metal chassis.

The first part: The digital thermometer.
This part is a special sensor. It communicates over a 1-Wire bus (reqired only one data line). Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus.
You can use the DS18B20 in two modi: parasite or with an external supply. In this litte circuit we use the parasite mode.

DS18B20-parasitär
We connect ground to arduino ground and the data line to port 10 (arduino). Additional, we connect this data line port with the 4.7 KOhm to 5V from the arduino (power supply).

The second part: The display.
Here you see the pin layout:

16x2 lcd display

What we need:
1 -> connect to ground
2 -> connect to +5V
3 -> see below
4 -> connect to pin 12 (for example)
5 -> connect to ground
6 -> connect to pin 11 (for example)
7
8
9
10
11 -> connect to pin 5 (for example)
12 -> connect to pin 4 (for example)
13 -> connect to pin 3 (for example)
14 -> connect to pin 2 (for example)
15 -> see below
16 -> connect to ground

16x2 lcd display-1

So, thats all.
And here is the source code. We need two additional libraries – OneWire.h and LiquidCrystal.h. Both are available in the internet.

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

/*
 LiquidCrystal Library - Hello World
 The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 */
 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary)

void setup(void) {

// set up the LCD's number of columns and rows: 
 lcd.begin(16, 2);
 lcd.print("Temperatur");
}

void loop(void) {
 byte i;
 byte present = 0;
 byte type_s;
 byte data[12];
 byte addr[8];
 float celsius, fahrenheit;
 
 if ( !ds.search(addr)) {
 ds.reset_search();
 delay(1250);
 return;
 }
 
 if (OneWire::crc8(addr, 7) != addr[7]) {
 return;
 }
 
 // the first ROM byte indicates which chip
 switch (addr[0]) {
 case 0x28:
 type_s = 0;
 break;
 default:
 return;
 }

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();
 }

// Convert the data to actual temperature
 // because the result is a 16 bit signed integer, it should
 // be stored to an "int16_t" type, which is always 16 bits
 // even when compiled on a 32 bit processor.
 int16_t raw = (data[1] << 8) | data[0];
 if (type_s) {
 raw = raw << 3; // 9 bit resolution default
 if (data[7] == 0x10) {
 // "count remain" gives full 12 bit resolution
 raw = (raw & 0xFFF0) + 12 - data[6];
 }
 } else {
 byte cfg = (data[4] & 0x60);
 // at lower res, the low bits are undefined, so let's zero them
 if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
 else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
 else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
 //// default is 12 bit resolution, 750 ms conversion time
 }
 celsius = (float)raw / 16.0;
 fahrenheit = celsius * 1.8 + 32.0;

lcd.setCursor(0, 0);
 for (byte a=0;a<16;++a) lcd.print(" ");
 lcd.setCursor(0, 1);
 for (byte a=0;a<16;++a) lcd.print(" ");
 
 
 lcd.setCursor(0, 0);
 lcd.print(celsius); lcd.print(" ");lcd.write(0xDF);lcd.print("C");
 
 
 lcd.setCursor(0, 1);
 lcd.print(fahrenheit); lcd.print(" ");lcd.write(0xDF);lcd.print("F");
 delay(2000);
}

One thought on “16×2 LCD display and DS18B20

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s