Two Temperature Probes with improved Python Code

I am interested in getting multiple temperature readings at the same time, because there are some interesting experiments that require two temperatures.  I started looking around and it seems like the Raspberry Pi and waterproof DS18B20 probe are more than capable of doing the job.

So this tutorial documents the process for getting two temperature probes to report temperatures to Google, using gspread.  It also uses W1thermsensor, a very handy Python package to make the code very manageable.

Materials

Connect the Probes:

Connecting the DS18B20 temperature probe is very straightforward.  I have documented the details in this tutorial here.  They are the instructions for one probe.  In short, these are the connections:

  • Probe Red – Power – 3.3V RPi (GPIO pin 1)
  • Probe Yellow – Data – RPi GPIO4 (pin 7)
  • Probe Blue – Ground – RPi Ground (pin 9)

There needs to be a 4.7k Ohm resistor between data and power.  When you connect multiple sensors, they can all go on the same resistor.

Test the Probes

Once they are connected, and before you do much coding, you will want to make sure they are working.  Much of this is discussed in my previous tutorial.

sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices
ls

You’ll see a list of the probes you have connected.

You can cd into one of them and get a temperature

cd 28-00*
cat w1_slave

When you do that you’ll get a number that represents the temperature in millicelsius.  Here it is t=22187.  You can do this with the other sensor too, and then move on to the next step.

Load the W1thermsensor Python Package

I went to Tim Furrer’s website and simply followed the instructions.

Install the package with PIP

sudo pip install w1thermsensor

You can also do this from Raspian using apt-get

 sudo apt-get install python-w1thermsensor

Tim’s site gives some good guidance on a basic format for getting multiple sensors to give temperatures.  This one will give the sensor IDs and their readings.

Create a file called 2Templogger.py.

nano 2Templogger.py

In that file use this code.

from w1thermsensor import W1ThermSensor
for sensor in W1ThermSensor.get_available_sensors():
  print("Sensor %s has temperature %.2f" %(sensor.id, sensor.get_temperature()))

Run it.

python 2Templogger.py

What you will get is a list of the probes and their temperatures.  It is pretty fantastic.

This is the basic program, but there are others, and a couple things I figured out.  My goal was to make a program that would record these temperatures and put them in a Google Sheet, using the gspread library.  Check out the code here.

Since I just have the two probes, my program calls them individually.

sensor1 = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, "00000726c549")

Remember getting the list of probes?

When you add the probes individually using W1ThermSensor, you only use the part after the 28-.  So my probe “28-00000726c549” becomes just “00000726c549”.

I used this to plot the change in temperature when hot water mixes with cold water.  It may not sound exciting, but when you do mix them creatively like this, you get something pretty interesting.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *