How to Use the DS18B20 Temperature Sensor with Raspberry Pi for Brewing Beer

I want to build a project where I use my RPi to measure the temperature of both the wort and the swamp cooler I use to ferment beer, and alert me to whenever I need to add ice packs (i.e., the temperature rises above 74*F for a certain yeast strain).

There are multiple ways to read temperature from the RPi. The most common, and effective, that I have come across is through the use of Arduino. However I have already spent so much money on the RPi that I decided to find another way, and Adafruit comes through with the answer.

I bought all the necessary parts from Adafruit: the DS18B20 temperature sensors, a half-sized breadboard, a jumper wire pack, and a Pi Cobbler, the last of which is Adafruit specific.

Configuring the RPi for External Hardware via Adafruit

The following was performed by following Adafruit's fourth lesson, GPIO Setup.

Adafruit has "produced an extensive and extremely useful collection of code to make life easy for those wishing to experiment with attaching electronics to their Pi."

If you don't have git, run the following:

sudo apt-get update  
sudo apt-get install git

Clone the Adafruit repository:

git clone  
cd Adafruit-Raspberry-Pi-Python-Code  

Execute the following

sudo apt-get update  
sudo apt-get install python-dev  
sudo apt-get install python-rpi.gpio

I already had python-rpi.gpio installed.

Connecting the Hardware to the Raspberry Pi

Hook up a DS18B20 probe to the breadboard according to Adafruit's instructions.

Modify /boot/config.txt

If you are using a Raspbian after 2015 you will need to edit /boot/config.txt with the following lines. Please refer to this post and this post for the reason. If you don't do this, your /sys/bus/w1/devices directory will be empty.


Quick Test to See if it Works

execute the following

sudo modprobe w1-gpio  
sudo modprobe w1-therm  
cd /sys/bus/w1/devices  
cd 28-xxxxxxxxxxxx ## From the ls command, find the directory starting with 28 and cd to it  
cat w1_slave

It appears to me that the first two lines (sudo modprobe w1-gpio ; sudo modprobe w1-therm) must be run every time the raspberry pi is turned on in order for the RPi to read the sensors.

Each DS18B20 you plug in will have its own directory in the format of 28-xxxxxxxxxxxx.

If all is well, it will display two lines of text. The first line will either have a YES or NO at the very end; if you receive a NO, there was a temporary error, so try again; if you receive a YES, the second line will display the temperature in Celcius.

Here was my result:

af 01 4b 46 7f ff 01 10 bc : crc=bc YES  
af 01 4b 46 7f ff 01 10 bc t=26937

Dividing the temperature by 1000 gets you the Celsius, in my case 26.937*C.

Set the correct timezone for your raspberry pi by issuing:

sudo dpkg-reconfigure tzdata

Writing the Python code to display the Temperature every Second

This is copy and pasted form the Adafruit page

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()  
    return lines

def read_temp():  
    lines = read_temp_raw()  
    while lines[0].strip()[-3:] != 'YES':  
        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, temp_f

while True:  

Making sure the Temperature will Always be Recording (even after a power


I want my RPi to be recording the temperature 24/7, and to resume recording the temperature after a power outage. The /etc/rvc.local file is one way of controlling what script executes on start up. Edit the file by issuing:

sudo nano /etc/rc.local

and write the following lines before the "exit 0":

# Allow RPi to See Sensors  
sudo modprobe w1-gpio  
sudo modprobe w1-therm  
# Run Python Script  

My entire /etc/rc.local file looked like this afterwards:

Download MySQL and install it. Change the password to something simple, like pi.

sudo apt-get install mysql-server

Loggin to mysql and create a new database:

mysql -u root -p  
create database brew;  
use brew;

Create a temperature table.

CREATE TABLE temperature (  
dt datetime DEFAULT NULL,  
temperature decimal(4,1) DEFAULT NULL,  
temperature1 decimal(4,1) DEFAULT NULL,  
temperature2 decimal(4,2) DEFAULT NULL,  
KEY temperature_dt_ind (dt),  
KEY temperature_notes_ind (notes)  

Create a Batch table:

CREATE TABLE batch (  
material VARCHAR(10) DEFAULT NULL,  
start_date DATE DEFAULT NULL,  
active CHAR(1) DEFAULT 'Y',  
end_begin_date DATE DEFAULT NULL,  
end_end_date DATE DEFAULT NULL,  

Create a Water table:

CREATE TABLE water (  
bottle_n int(11) DEFAULT NULL,  
KEY water_ind (dt)  

I created a water table because I use a swamp cooler and wish to correlate the number of water bottles necessary to cool a batch, given a starting temperature.

The batch table records the name, start date, a range of end dates (I use 4-6 weeks), and a range of temperatures for the optimum temperature of the batch's yeast.

Make sure MySQL boots on startup in case of a power failure:

<code>sudo update-rc.d mysql enable</code>

I created a python script to handle everything. Here it is:


You may notice that some of my batches don't have a probe for the "temperature" column, which is the swamp cooler. For some yeast strains, and during certain parts of the year, I don't need a swamp cooler.


Python and MySQL

Issue the following to download the module:

<code>sudo apt-get install python-mysqldb</code>

Issue python to get into the interpreter, and then issue:

import MySQLdb

If you see no error, then it is good.

Python and MongoDB

If you plan to use MongoDB instead of, or in conjunction (as I do) with MySQL, issue the following:

sudo apt-get install python-pymongo

Go into python and issue import pymongo to test the installation.

Download and use Screen

sudo apt-get install screen

When you are ready to run your script, issue the following:

screen -S brew  
cd /home/pi  
python ./


Add Comment




Are you human? + five = 11