I was as excited as anyone else when the Raspberry Pi Zero (RPi-0) was announced over Thanksgiving. This $5 computer is 86% cheaper than the Raspberry Pi 2 which is already a great deal at $35. I thought I would try it out as a temperature data logger. In the end, it worked beautifully, and I hope to build some more probes for my students to use.
Additional HardwareTo run the RPi-0, some additional hardware is needed:
- A micro-USB power supply.
- A mini-HDMI to HDMI adapter.
- A micro-USB to USB female adapter (also called On-The-Go cable).
- A micro SD card with the operating system.
Operating System A more up to date operating system is needed for the RPi-0. Raspbian-Jessie or later is recommended.
You can get the image here: Download Rasbian-Jessie Image You can then use a program (like Win32 Disk Imager) to write the image to an SD card.
The image is a little more than 1Gb while zipped and a little over 3Gb when unzipped. You will need some space on a local machine when you are working with it.
Booting Up Once the operating system is loaded up, you can plug it into the RPi-0. It doesn't click in like the RPi-2 (you have to get to $5 somehow). Plug in power, and look for an intermittently blinking green light to see that all is working (the red LED is also gone). Attach pins to the GPIO Another thing that had to go was the pins on the GPIO. The RPi-0 can still interact with the real world, you will just need to attach some pins. I did this with a new Holiday Bundle from Circuit Specialists (video below).
I used the soldering kit to attach 4 header pins to the top of the Raspberry Pi. You can see the attached pins on the top right of the RPi-0 here. I used regular headers, and just pulled the ones I didn't need out with a pair of pliers. I found that this was easier than just making the single connections I needed. We will need three connections.
- Power - 3.3v
- Data - GPIO 4
- Ground
The GPIO layout is the same as the 40 pin layout for the other Raspberry Pi models. The connections that are needed are labeled here with arrows.
Again I found it easier to attach four headers and just pull out the ones I didn't need before soldering. Make the Circuit To set up the temperature sensing circuit you will need:
- A waterproof DS18B20 Temperature Probe
- A 4.7k Ohm Resistor (sold with the probe)
- Protoboard
- Solder
- Male to female jumper wires (I used 6)
There is a 4.7k resistor between data and power. This tutorial from Adafruit is wonderful for setting things up. Also, you can do this with a solderless breadboard. I wanted something a little more robust, and went with soldered connections. When it is soldered together, it should look like this:
The female ends can be attached to the pins on the Raspberry Pi. The connections are:
- Probe Red - 3.3V
- Probe Yellow - GPIO4
- Probe Blue - Ground
Now the circuit is complete and ready to be tested. Testing the CircuitIn the RPi terminal, type:
sudo nano /boot/config.txt
Go to the bottom of the file, and add this line:
dtoverlay=w1-gpio
Close this by hitting Control-X and then Y. Reboot the Raspberry Pi. Once it has rebooted, type the following lines into the command prompt:
sudo modprobe w1-gpio sudo modprobe w1-therm
This activates the sensor on the GPIO. Now we need to find it.
cd /sys/bus/w1/devices/ ls
The circled number here is the identity of the temperature sensor, like its serial number. We need to go into that directory, and tell it to take a temperature.
cd 28* cat w1_slave
The readout will contain the temperature in units of milli-Celsius. Look for t= If you have seen all this, your temperature probe is working. Well done! If not you might see nothing when you changed directory into /sys/bus/w1/devices/. Chances are the wiring and/or soldering did not go right, and that would be the first thing to check.
Working with ThingSpeak
Go to ThingSpeak.com and create an account if you don’t already have one. Once created, you will need to create a channel. When you set this up, you can give it any title you want. I have it filled out for my channel below. Also, there is a box you can check that will make it public. If you do, the public can see your graph (but not your account information).
Go to the bottom and click on Create Channel. Then go to the Data Import/Export tab. On the right-hand side, there will be a Update Channel Feed GET box. It contains a URL that you are going to put into a python program below. I am not showing the full one here, because it contains an API key that is unique to my channel. You will need to copy that URL.
Make the Program
Now we want a program that will log the data from your sensor directly to the Internet using ThingSpeak. The program is broken up into a few parts
- Libraries to import
- Functions for sensor readings
- Value Writing
In short, this program uses a library give commands to the operating system (using the library 'os'). The Raspberry Pi will return the temperature in that 2 line response we saw before. Then the program looks for the temperature and gives reports it to ThingSpeak, where it can be plotted.
import os
import glob
import time
import sys
import datetime
import urllib2
baseURL = "https://api.thingspeak.com/update?api_key=YOURAPIKEY"
#initiate the temperature sensor
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
#set up the location of the sensor in the system
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw(): #a function that grabs the raw temperature data from the sensor
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp(): #a function that checks that the connection was good and strips out the temperature
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
while True: #infinite loop
tempin = read_temp() #get the temp
values = [datetime.datetime.now(), tempin]
g = urllib2.urlopen(baseURL + "&field1=%s" % (tempin))
time.sleep(60)
This is the program, and it works pretty well. The plot below shows the temperature in my kitchen. (If its really high, that is ok. I am doing some experiments on how well containers retain heat.)
Top Comments