After Hours

with Monirul Pathan

Navigation Menu

Raspberry Pi + Temperature Sensors

Raspberry Pi + Temperature Sensors

Posted by on Sep 25, 2012 in Uncategorized | 8 comments

I have a Pi, and a couple of sensors to test with. Namely an LM-35, LM-335 (aka TMP36), DS18B20 and RHT03 (which is the same as the DHT22)

I2C – DS18B20

Occidentalis so far only has I2C support on GPIO pin #4 as far as I can tell, so that’s where the I2C based DS18B20 goes. Here is a bit of code to poll the chip every few seconds:

# This little example code checks and parses the temperature from
# a DS18B20 connected to GPIO 4 on the Raspberry Pi running Occidentalis
# By Monirul Pathan

# Connect a DS18B20 with VCC to 3V, ground to ground and Data
# to GPIO #4. Then connect a 4.7K resistor from Data to VCC.

# Rus on Occidentalis: http://learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/occidentalis-v0-dot-1
# Thanks Adafruit for the awesome RPi distro =)

import glob
import time
import os
import datetime

#initialize the device
os.system("sudo modprobe w1-gpio")
os.system("sudo modprobe w1-therm")

print "DS18B20 - Raspberry Pi"
#find the device
devicedir = glob.glob("/sys/bus/w1/devices/28-*")
device = devicedir[0]+"/w1_slave"

while True:
#open up the file
f = open (device, 'r')
sensor = f.readlines()
f.close()

#parse results from the file
crc=sensor[0].split()[-1]
temp=float(sensor[1].split()[-1].strip('t='))
temp_C=(temp/1000.000)
temp_F = ( temp_C * 9.0 / 5.0 ) + 32
now = datetime.datetime.now()
dtime = datetime.time(now.hour, now.minute, now.second)

#output
if 'YES' in crc:
print dtime,":\t",temp_C,"*C\t",temp_F,"*F"
else:
print dtime,"\tCRC check failed."

#wait 5 seconds and repeat
time.sleep(5)

Wire it all up with a Cobbler, a breadboard, some wires, and the 4.7k resistor. Then put the above code into a whatever.py file and run “sudo python whatever.py” and voila, you’re getting temperature readings from the DS18B20 every 5 seconds.

Note: It often fails on my end (about 10% of the time) but that’s not terrible.

 

Analog – MCP3008 + LM35/LM335

The MCP3008 is an analog to digital converter that works beautifully with the Pi. This tutorial on using a potentiometer to control volume got me started. So I hooked up an LM35 and an LM335 (which I believe is the same as the TMP36) to the first 2 of 8 analog inputs on the chip.

The conversions were a bit trickier and I still don’t have it down to something I can fully trust but it gets me within 1*C of the room temperature.

#!/usr/bin/env python
import time
import os
import RPi.GPIO as GPIO
import glob
import datetime

#initialize the DS18B20
os.system("sudo modprobe w1-gpio")
os.system("sudo modprobe w1-therm")
#find the initialized device
devicedir = glob.glob("/sys/bus/w1/devices/28-*")
device = devicedir[0]+"/w1_slave"

#get values from the ds18b20
def getC () :
#open up the file
f = open (device, 'r')
sensor = f.readlines()
f.close()

#parse results from the file
crc=sensor[0].split()[-1]
temp=float(sensor[1].split()[-1].strip('t='))
temp_C=(temp/1000.00)

#output
if 'YES' in crc:
return temp_C
else:
return 999.99

 

GPIO.setmode(GPIO.BCM)
DEBUG = 1

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
if ((adcnum > 7) or (adcnum < 0)):
return -1
GPIO.output(cspin, True)

GPIO.output(clockpin, False) # start clock low
GPIO.output(cspin, False) # bring CS low

commandout = adcnum
commandout |= 0x18 # start bit + single-ended bit
commandout <<= 3 # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)

adcout = 0
# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout <<= 1
if (GPIO.input(misopin)):
adcout |= 0x1

GPIO.output(cspin, True)

adcout /= 2 # first bit is 'null' so drop it
return adcout

# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 18
SPIMISO = 23
SPIMOSI = 24
SPICS = 25

# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)
# temperature sensor connected channel 0 of mcp3008
lm35_adcnum = 1
lm335_adcnum = 0
while True:
# read the analog pin (temperature sensor LM35)
read_lm35 = readadc(lm35_adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS)
time.sleep(0.5)
read_lm335 = readadc(lm335_adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS)

# convert analog reading to millivolts = ADC * ( 3300 / 1024 )
lm35mv = read_lm35 * ( 3300.00 / 1024.00)
lm335mv = (read_lm335-760) * ( 3300.00 / 1024.00)
# 10 mv per degree

lm35_C = (((lm35mv) / 10.00)-1.50) #LM35
lm335_C = ((lm335mv/10.00)-1.00) #LM335
ds18_C= getC()

# convert celsius to fahrenheit
lm35_F = ( lm35_C * 9.00 / 5.00 ) + 32.00
lm335_F = ( lm335_C * 9.00 / 5.00 ) + 32.00
ds18_F = ( ds18_C * 9.00 / 5.00 ) + 32.00

# remove decimal point from millivolts
lm35mv = "%d" % lm35mv
lm335mv = "%d" % lm335mv

# show two decimal place for temprature and voltage readings
lm35_C = "%.2f" % lm35_C
lm335_C = "%.2f" % lm335_C
ds18_C = "%.2f" % ds18_C

lm35_F = "%.2f" % lm35_F
lm335_F = "%.2f" % lm335_F
ds18_F = "%.2f" % ds18_F

if DEBUG:
print "DS18B20 :\t ADC: NA \t MV: NA \t C:",ds18_C,"\t F:",ds18_F
print "LM-35 :\t ADC:",read_lm35,"\t MV:",lm35mv,"\t C:",lm35_C,"\t F:",lm35_F
print "LM335 :\t ADC:",read_lm335,"\t MV:",lm335mv,"\t C:",lm335_C,"\t F:",lm335_F

#os.system("cat /sys/bus/w1/devices/28-*/w1_slave")
#print "temp_F:\t\t", temp_F
print "\n\n"
# hang out and do nothing for 10 seconds, avoid flooding cosm
time.sleep(5)

Because the MCP3008 is talking to the Pi through SPI, it is a lot more reliable and I have yet to see a wacky reading (except that one time my cat stepped on the breadboard)

 

1-Wire (ish?) – DHT-22

There is some example code on the interwebs with the DHT22 but I have yet to get any readings out of it. Its a shame since it is one of the cooler components as it has a humidity sensor built in as well. I’m pretty sure this will get sorted out in a few weeks though.

Read More

Look ma, no images!

Look ma, no images!

Posted by on Sep 21, 2012 in Uncategorized | 0 comments

I OCD sometimes. This is one of them but in the process I turned that last splash page to be fully image free. Its now entirely in CSS+HTML. This serves the obvious benefit of quicker load times, not that it really matters on a webpage this size but like I said, I obsess about details. It also shows up much nicer across more platforms according to Adobe’s BrowserLabs and Browsershots.

Few cool things I learned from this: you can do pretty cool gradient and other effects like shadows in CSS. Have a look at this Gradient Generator. Pretty neat.

Lastly, not sure how long Web Fonts have been around but they’re pure awesomeness. Here’s a good tutorial on CSS3 web fonts.

 

Download all files here:  MP-Splash2

Read More

New splash page – PSD + files

New splash page – PSD + files

Posted by on Sep 20, 2012 in Uncategorized | 0 comments

Since I’ve added on this blog to my site, I needed to add on a splash page. I’m usually very anti-splash-page for the main reason that it adds on an extra step and in tech, thats rarely good. [See Three-click rule] But it was either that or confuse potential photog clients with this jibberish.

Anyway in the spirit of open source, here is a zip with all html,css and psd files if anyone ever wants to use it, modify it for their own purposes, whatever have you.

MP-Splash1

Read More