Read Temperature from DS18B20

One of the details to consider when you want to make an accurate SPL measure is the temperature and the barometric pressure of the environment.

Sound travels as pressure waves into the air, and so, if you really want to be precise, you need to know the status of the air as a "fluid".

I agree, the tolerance on my SPL levels is probably higher then the variation due to temperature/pressure, but I wanted to play with my RPi anyhow, and so I added a couple of sensors to track these values :-)

RPi has many GPIO pins, but you don't want to waste precious GPIO outlet just to connect an A/D converter and read a temperature value. The DS18B20 is a well known and very accurate integrated sensor that you can hook up to your I2C interface and use to read immediately the temperature value.

I've got it from Robot-Italy here : DS18b20

There is also a water-proof version, which is actually the one I've got: DS18b20-waterproof

I think these are all imported from AdaFruit, because I can clearly see Adafruit stamps on the envelope ;-)

 

So ... once you have your sensor you need to prepare RPi to work with I2C. There are tons of tuturials and howto around, but the one I've found more useful are theese ones:

  1. Adafruit: GPIO configuration
  2. Adafruit: Python setup
  3. Adafruit: DS18b20 temperature sensing

The last one is actually all you need to be able to connect the sensor to your RPi and read the temperature value.

I've found the DS18b20 sensor very accurate and stable. The "speed" to adapt to a different temperature is around 20seconds or so, and the measue is usually clean, with very small ripple. From the example of Adafruit it is trivial to write a "get_temperature.py" script that does what I need for my "rpi_em_sampling_loop.pl".
 

#!/usr/bin/python

#
#=BEGIN BRAINWORKS GPL
#
# This file is part of the BrainWorks RPi Environmental Monitor.
#
# Copyright(c) 2013 Gianluca Filippini
# http://www.brainworks.it
# info@brainworks.it
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see .
#
#=END BRAINWORKS GPL
#

import os
import glob
import time
     
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
     
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
     
def read_temp_raw():
   f = open(device_file, 'r')
   lines = f.readlines()
   f.close()
   return lines
     
def read_temp():
  lines = read_temp_raw()
  while lines[0].strip()[-3:] != 'YES':
     time.sleep(0.2)
     lines = read_temp_raw()
  equals_pos = lines[1].find('t=')
  if equals_pos != -1:
     temp_string = lines[1][equals_pos+2:]
     temp_c = float(temp_string) / 1000.0
     temp_f = temp_c * 9.0 / 5.0 + 32.0
     return temp_c


#
# MAIN
#
print read_temp()	

Important: do not forget to add "w1_gpio" and "w1_therm" in your /etc/modules. Here is mine:

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

snd-bcm2835

i2c-bcm2708
i2c-dev

w1_gpio
w1_therm