i2c LCD and Digital Temperature Sensor

DS18B20 temperature sensor on breadboard

DS18B20 digital temperature sensor – only three wires and a resistor needed!

These DS18B20 devices are pretty nifty. I remember all of the hassle connecting an LM35 to a computer, sorting out the changes in current into something a BBC analogue port can handle, calibrating and scaling. And now… One little device, a 4k7 resistor and a few lines of code.

Here I’ve combined the sensor and the i2c display together to give me the Pi temperature and the room temperature. It’s been quite mild this autumn even without the central heating on!

There are a number of tutorials on the internet (such as this one)showing how these devices are used. I’m more familiar with using them with a PICAXE but they’re just as simple with the Raspberry Pi. It’s important to note that each of the sensors has their own address, so make sure you check carefully.

The code is straightforward:

import os
import commands
import time
import pylcdlib
import datetime

lcd = pylcdlib.lcd(0x27,0)
lcd.lcd_puts("                 ",1) #display on line 1
lcd.lcd_puts("                 ",2) #display on line 2

def get_cpu_temp():
	tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
	cpu_temp = tempFile.read()
	return float(cpu_temp)/1000

while True:
	tfile = open("/sys/bus/w1/devices/28-0000011628bf/w1_slave")
	text = tfile.read()
	secondline = text.split("\n")[1]
	temperaturedata = secondline.split(" ")[9]
	floattemperature = (float(secondline[29: ])/1000)
	lcd.lcd_puts("Temp: "+str(floattemperature),2) #display on line 2

	lcd.lcd_puts("CPU :"+str(get_cpu_temp())+"       ",1) #display on line 1

The LCD is really hard to photograph. I’ve tried a whole number of settings on the point’n’squirt camera at home, as well as my phone, but neither really shows up how well these LCDs show text.

Having used the LCDs with the PICAXE, I’m sure it’s possible to access the degrees symbol! Another challenge is using i2c with Python3. It seems that the libraries are unavailable, although trawling the ‘net has shown some leads that I’ll follow up.

Display showing the temperature.

LCD showing processor temperature and the temperature returned by the DS18B20.

Where next? My earliest experiments involved using the sensor with a PICAXE, which then sent the data to the Raspberry Pi as a serial data stream. These results were then uploaded to COSM (now known as Xively, having had a brief spell as Pachube). It was interesting to watch COSM plot the graphs over a period of time. It wouldn’t take much imagination to see how this could easily become an environmental monitoring and control platform – and many have succeeded!


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