logo

Project 1: Raspberry Pi with One Wire Temperature Sensor

2017-09-08

Updated: 2020-08-30


Objective:

To use a Raspberry Pi and 1-wire digital thermometer to log time and temperature to a file on the Raspberry Pi SD storage. Then use the data to create a plot of temperature versus time.

Materials:

1. Raspberry Pi Model B with Raspbian OS. Prefer to use in a headless set-up but will need monitor and peripherals to set-up

2. Maximum Integrated DS1822 Econo 1-Wire Digital Thermometer.

Click here to download the manual.


3. 4.7 kΩ resistor (in this case used two 10 kΩ resistors wired in parallel).

4. Misc wire and connectors to attach the temperature sensor to Raspbery Pi's GPIO.

5. Recommended equipment is a workstation running linux to allow headless Raspberry Pi set-up.

Software:

1. Linux OS (Raspbian for the Raspberry Pi, and a workstation version)

2. Python version 2.7.3

3. Gnuplot 5.0

Hardware:

1. Solder black wire to ground and connect to Ground (pin 6).

2. Solder red wire to power and connect to GPIO power (pin 1).

3. Solder white wire to data and connect to GPIO 4 (pin 7).

4. Connect pullup resistor to data and power wires.

Raspi with 1 wire Temp Sensor
NOTE 1: I am not spending much time describing this because a web search will reveal a number of tutorials/blogs. For a schematic of GPIO pins see below.

NOTE 2: For full electronic details on the DS1822 download the maxim integrated DS1822 spec sheet. You will find the DS1822 works using a 64 bit code unique to the device. This allows multiple temp sensors to be wired into the one data pin. This sensor is used in HVAC controls, temperature monitoring systems, and industrial systems.

GPIO Pins

Software Set-Up:

You likely already have your Raspberry Pi (Raspi) running on raspbian, if not there are many sources to help get you running. The next which I will not delve into here is to set-up raspbian in a headless configuration. This means running the Raspi without user peripherals (i.e. mouse, keyboard, and monitor. This is highly recommended as it minimums the power draw for these devices and leaves the Raspi in a more stable configuration.

Next get familiar with modprobe we are going to use it to add one-wire interface to the linux kernal. First SSH into the raspberry pi terminal, then:

  • sudo modprobe w1-gpio
  • sudo modprobe w1-therm
GRA Added correction on 2018-08-21:

Had issues with using the device tree. Got this working by using overlay, add to /boot/config.txt file:

dtoverlay=w1-gpio

sudo modprobe w1-gpio

To test the one-wire interface and hardware set-up:

  • cd /sys/bus/w1/devices/
Then find you devices serial number and print output of w1_slave using cat command.

Output Temp
Now your one-wire interface and hardware are working. At this point double check the voltage drop between ground and power. This set-up is reading 3.3V, the DS1822 needs between 3.0V to 5.5V. The Raspi server is running stable now is it time to work on data processing. I chose to log data into python.

I quickly wrote a python script to log data to text file. The code requires the time module. It simple runs a for loop, indefinitely. This is set-up by lopping for one million cycles. After the for loop the first line of code just opens a file handle, the following 7 lines interact with the sensor and create a data point in variable named temperature. The next line writes time stamp in "2017-09-06" format, this is done for reason (gnuplot requires all time date in number format). The next 2 lines just write the data and close filehandle. An indoor temperature controlled by an HVAC system is stable so the final line waits 60 seconds using the sleep command. This set-up creates a file that records time/temp data every 60 seconds, see below for sample of data.

2017-09-4 11:47 27.562

2017-09-4 11:48 27.625

2017-09-4 11:48 27.625

2017-09-4 11:49 27.625

2017-09-4 11:49 27.625

2017-09-4 11:50 27.687

2017-09-4 11:50 27.687

For a copy of the script file click here.

Now that we have all this data let's plot it. This project uses gnuplot, my preferred way to use GNU plot is to create a script file using vi editor. The script file is invoked in the terminal using:

  • gnuplot > load "script file"

For a copy of the GNU plot script file click here.

The plot is set-up to for time on the x-axis and temperature on the y-axis. The primary axis is reserved for Celsius, and Fahrenheit on the secondary axis. Below is an example graphic of the plot. There is little to observe as the temperature is steady, but there is a a few spikes when someone grabs and holds the sensor.

Plot Temp

2020 Set-up

Previous set-up failed, one of the sensor legs pulled out. Created this for a replacement. Better to hang off the wall to get out of the way. Also, found it better to pay a few bucks and use a CPU fan extension connector to eliminate solder points.




Conclusion

That is it for this project. You can take it from here, here are some ideas:

  • Add multiple sensors to different rooms. Take care to assess the power drop from your conductor.
  • Try a different type of One Wire sensors or for more of a challenge use different technology, such as RTDs.
  • Work on porting the data to your own webserver and auto update your chart.
  • Wirelessly send temp data to the webserver. User will learn to configure dhcpcd and ip port forwarding.

Design credit knxops.com in cooperation with Ltimas and co.
Copyright © 2019 by Gregory Altimas aka Greg Altimas