Ground Temperature
Published by Martin on May 18, 2020
The original project also included a Ground Temperature sensor. Now the best ones have always been a weatherproof sensor that can be put in the dirt and gives a reading of the ground temperature. The DS18B20 is based on a system called 1-Wire from Dallas Semiconductors. The one I purchased was a 3metres long. My idea was to run this from the Pi installed on the Wall down the side of the drain pipe and into the ground. The biggest problem was it arrived and I opened it to find 5 x 1 metre cables. Happy Ebay guy sent on the correct one and a few days later this arrived
Published by Martin on May 10, 2020
So today in the mail the Ground Sensors arrived.. Even more excitement. Ripped open the Post Bag and then the Anti Static bag to find 5 Sensors.. Cool.. Bad oops.. Then opened them out to find they were only 1.2 M cables. I need a 3M one to run from the roof to the ground at a minimum 2.4 Metres.. Anyway back to Ebay and say hey this is not what I ordered and now I wait for my order. The funny story someone else is enjoying my 3 Metre sensor, while I enjoy his 5. At least I was able to test out the code to get the sensor working !
Published by Martin on November 13, 2020
The DS12 is one of the easiest to get running. Many months ago I wrote a doco on it and now lost it.. This is a duplicate of the the pihuts install to get it up and running quickly.
Building the Circuit
You will need:
- A Dallas DS18B20 (either version)
- 4.7k ohm Resistor
- If there are four bands, the colours will be Yellow, Purple, Red, and then Gold
- If there are five bands, the colours will be Yellow, Purple, Black, Brown, Brown
- 4 female to male jumper wires
- 2 male to male jumper wires
Before building this circuit, you must turn the Raspberry Pi off.
The circuit will be using a ground (GND) pin to act like the negative or 0 volt ends of a battery. One of the pins marked 3v3 will provide the power for the sensor. 3v3 means that it is a 3.3 volt power supply.
Use two female to male jumper wires to connect the GND and 3v3 GPIO pins to the bottom two rows of holes on the breadboard. Match up the colours marked on the breadboard - red and blue - with the jumper wires from the Raspberry Pi connect 3v3 to the red row, and GND to the blue row. These two ‘rails’ (as they are known) will provide the ground and power supply for the whole of the breadboard. Connect the temperature sensor as shown, with a male/male jumper wire going to the bottom rail attached to the Raspberry Pi's ground (GND). Connect the red wire using a jumper to the 3v3 rail at the bottom. This supplies the temperature sensor with its power.
Terminal Block

If you are using the waterproof sensor, you may have problems pressing the wire strands into the breadboard holes. I use a terminal block and screw the wires into that.
The other end of the resistor should be inserted into another column of the breadboard, between the red lead of the temperature sensor and the jumper wire connected to the 3v3 rail. The yellow lead goes into a column with one end of the 4.7k ohm resistor and another jumper wire (shown in yellow) that goes to GPIO pin 4. The program will read the temperature from this pin.

Setting up the Raspberry Pi
Before you can use any 1-wire devices you must first tell the Raspberry Pi how to read them. Open a Terminal Window and type the following to edit the Raspberry Pi's configuration file:
sudo vi /boot/config.txt
Look to see whether there is a line that has dtoverlay=w1-gpio in it. If not, add the following to the end of the file:
dtoverlay=w1-gpio
Now reboot the Raspberry Pi:
sudo reboot
To test the configuration, set-up the circuit above to connect the DS18B20 and type the following into a terminal window:
sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices
ls
This will list all the devices that are connected to the 1-wire interface. The Dallas DS18B20 sensor starts with ‘28-’ followed by a long number. Type in the following, replacing the ‘xxxx’ with the text following the ‘28-’:
cd 28-xxxx
cat w1_slave
In response, you should get the following showing that the DS18B20 is working:
a3 01 4b 46 7f ff 0e 10 d8 : crc=d8 YES
a3 01 4b 46 7f ff 0e 10 d8 t=32768
Reading from the Sensor
Create a new python script either from a terminal window (with nano 3-temperature.py) or from IDLE, the 'Interactive Development Environment'. Enter the following example code:
# Import Libraries
import os
import glob
import time
# Initialize the GPIO Pins
os.system('modprobe w1-gpio') # Turns on the GPIO module
os.system('modprobe w1-therm') # Turns on the Temperature module
# Finds the correct device file that holds the temperature data
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
# A function that reads the sensors data
def read_temp_raw():
f = open(device_file, 'r') # Opens the temperature device file
lines = f.readlines() # Returns the text
f.close()
return lines
# Convert the value of the sensor into a temperature
def read_temp():
lines = read_temp_raw() # Read the temperature 'device file'
# While the first line does not contain 'YES', wait for 0.2s
# and then read the device file again.
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
# Look for the position of the '=' in the second line of the
# device file.
equals_pos = lines[1].find('t=')
# If the '=' is found, convert the rest of the line after the
# '=' into degrees Celsius, then degrees Fahrenheit
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
# Print out the temperature until the program is stopped.
while True:
print(read_temp())
time.sleep(1)
To run the code, you need to be the Super User, so run the code with:
sudo python temperature.py
Now watch the temperature of the sensor as you hold it or put it in a glass of water (if you've got the waterproof one!).