Set Up a Raspberry Pi DS18B20 Temperature Sensor
The 1-Wire DS18B20 waterproof temperature sensor is a very common sensor that is used with the Raspberry Pi . The use of a 1-Wire connection allows you to add multiple thermometers while using only one GPIO port. With several of these sensors connected to the Raspberry Pi, you could monitor water temperature, ambient air temperature for swimming pools, aquariums or solar heaters and placing your temperature sensor into a thermowell will allow you to install the probe into any plumbing.
If you are looking to connect several different monitoring sensors to your Pi then you should consider the more advanced range of sensors from Atlas Scientific, their temperature sensors provide data logging and an I2C connection to your Pi. You can find out more information about these in the sensors section.
To follow this procedure I am assuming that you are running the latest version of Raspbian and have the ability to connect to your Pi either through SSH with Putty and FTP with FileZilla or directly with a keyboard and monitor. If you haven’t set-up your RPi yet then check out my getting started section.
In this tutorial I will be using the following materials:
- Raspberry Pi (2, 3 or 4)
- Micro SD Card
- Power Supply
- DB18B20+ (waterproof temperature sensor)
- 4.7kΩ (or 10kΩ) resistor
- Jumper Wires
- Adafruit T-Cobbler Plus (Optional)
- Raspberry Pi Case (Optional)
The first thing we need to do is enable the w1-gpio from the config.txt. Enter the following command:
then add this line to the bottom of the file if not already present.
With this done we are now able to start connecting our sensor to the Pi.
When describing the physical pin connections I will be following the Broadcom GPIO pin numbering convention shown below.
Connect the Sensor
- Connect GPIO GND on the Pi to the negative rail on the breadboard and connect GPIO 3.3V on the Pi to the positive rail on the breadboard.
- Connect the black (-)wire of your DS18B20 to the breadboard negative rail.
- Connect the red (+)wire of your DS18B20 to the breadboard positive rail.
- Connect one end of your 4.7kΩ resistor to the positive rail and the other end to an empty row on your breadboard.
- Connect the yellow (Data) wire of your DS18B20 to the same row on your breadboard as the 4.7kΩ resistor.
- Finally, connect GPIO4 on the RPi to the same row on your breadboard as the 4.7kΩ resistor and yellow (Data) wire.
If all has gone well then your setup should look like the diagram below.
Note: The bare metal screening wire does not need to be connected.
Next, we will need to identify the registered serial number of our sensor, to do this we add the 1-Wire and Thermometer drivers to the RPi.
To load the drivers we will use the modprobe command. From the command prompt enter the following:
We then need to check that the DB18B20 has connected correctly to our Pi, change directory to your 1-Wire device folder and list the devices.
In the device drivers folder, your sensor should have created a folder using its mac address. In my case, the device is registered as 28-01157127dfff (Yours will be different). If you can’t see any folder then perform a reboot of the Pi and check again.
You can read what has been saved to the file by entering
This will print 2 lines with error checking. In the first line we see that it finishes with the word YES, this indicated that there has been a successful reading. In the second line, there is a t= indicating the temperature in °C.
The temperature sensor is now successfully connected to our Pi so we can start our Python programming. Your code can be written in any simple text editor such as Notepad, however, if you would like something a little more suitable then I would recommend downloading a dedicated code editing program such as VS Code.
First, we need to import the required python modules.
We then need to load our drivers:
From the steps above we now need to define where our sensors output file is located
We next need to read into a variable our raw sensor output file, python is quite particular about the indentation so all common pieces of code must be indented by the same amount.
This function opens, reads and then saves the 2 lines of raw data to the variable “lines” , we use the return function so that the result can be used later in our code.
Now we need to analyse our raw data, first we will check for errors
This error check reads the first line from the variable lines (lines) and then strips all but the last 3 digits, it is then compared to the value ‘YES’ (In python “!=” is defined as “not equal to”). If the value does not equal ‘YES’ then the program pauses for 0.2s then reads the new data and does the comparison again.
Once we have a successful ‘YES’ result we move on to read the second line of data from the variable lines (lines). We search the string and count how many digits until we find the string ‘t=’ and store it in temp_result. Provided the result of this is not -1 we proceed to strip all the digits prior to that number plus 2 (to remove the ‘t=’) leaving us with just the temperature stored in temp_string. Finally, we calculate the temperature in Celsius or Fahrenheit ready for printing.
We now loop or final code to print out the data every 2 seconds
You now need to save it to your Pi. If you created your code outside of your Pi then you will need to FTP it to your chosen directory.
If all has gone well when you run the program your output should look something like this
I have added the python code to my GitHub repository
The GitHub code has some additional lines that allow for multiple temperature sensors, Fahrenheit readings, and setting the level of accuracy (no. of decimals).
When cabling additional sensors just connect the red (+) and black wire (-) to your breadboard power rails. Connect the yellow (data) wire to the same row on your breadboard as the existing 4.7kΩ resistor and yellow wire.
If you would like to see how I have implemented temperature sensors why not check out my Hydropi overview page which has all the articles related to my own personal project.
If you have any thought’s about this article, improvements or errors let me know in the comments below and if you found this helpful, why not share it with others.