Temperature sensor with Raspberry Pi (DS18B20)

by May 26, 2020Projects, Raspberry Pi projects, Sensors

This is a basic tutorial on how we can use a DS18B20 temperature sensor with a Raspberry Pi. For the communication between the two devices we will be using the 1-wire interface. This interface can be used to connect to plethora of inexpensive sensors. Each of these sensors have their own 64 bit serial code. This feature lets us connect more sensors to the same 1-wire bus. You can read more about it here. So let’s begin :

Contents

Hardware Requirements

  • DS18B20 temperature sensor
  • 4.7k ohm resistor
  • Jumper cables
  • Raspberry Pi 3

Circuit Diagram

fritzing diagram

NOTE : If you are using the water proof version of the same sensor, you can follow convention and conned the red, black and yellow wires to 3.3v, GND, and pin 4 as shown in the fritzing diagram above.

Setup

First we need to alter the Raspberry Pi’s config file to enable the 1 wire interfacing (use by this sensor).

  • Open /boot/config.txt with your favorite text editor
sudo nano /boot/config.txt
  • canAdd the following line to the bottom of the file
dtoverlay=w1-gpio,gpiopin=4
  • You should also load in the device kernel modules by running the following
sudo modprobe w1-gpio
sudo modprobe w1-therm

Testing the sensor

  • Open your terminal at the location of the temperature sensor.
cd /sys/bus/w1/devices
  • Write the command ls to list out all w1 devices. you will see an alpha numeric similar to 28-0314977912af, this number is the serial number of your sensor. You must cd into this directory with cd 28-0314977912af.
  • Next, we have to list out the contents of the file named w1_slave.
cat w1_slave
  • You will get an output similar to this
dd 01 55 05 7f a5 a5 66 81 : crc=81 YES
dd 01 55 05 7f a5 a5 66 81 t=29812

The number that follows t=is the temperature in micro degree celcius (10^-3).

test output

Python Script

def get_temp(dev_file):
    f = open(dev_file,"r")
    contents = f.readlines()
    f.close()
    index = contents[-1].find("t=")
    if index != -1 :
        temperature = contents[-1][index+2:]
        cels =float(temperature)/1000
        return cels
if __name__ =="__main__":
    temp = get_temp("/sys/bus/w1/devices/28-0314977912af/w1_slave")
    print(temp)
  • First, we open and read the file that contains the temperature.
  • We locate where the number is and store it in temperature.
  • We then divide the temperature by 1000 and thus return the temperature in degree celcius.
  • Finally we run the get_temp function.

Alternatives

Apart from the DS18B20 temperature sensor you can also use the DHT11 and DHT22 temperature sensors. These sensors contain two major components — a thermistor and a humidity sensor. These sensors have also been documented extensively and there many resources to follow. You can follow the installation process here and see some example code here.

Conclusion

With this I conclude this simple tutorial on how you can control a DS18B20 temperature sensor with your Raspberry Pi. I also hope you learnt a bit about the 1-Wire interface.

Thank you for reading

Happy Learning 😀

Creating a multiplication Skill in Alexa using python

Written By Sashreek Shankar

Hey reader! I am Sashreek, a 16 year old programmer who loves Robotics, IoT and Open Source. I am extremely enthusiastic about the Raspberry Pi and Arduino hardware as well. I also believe in sharing any knowledge I have so feel free to ask me anything 🙂

RELATED POSTS

How to Simulate IoT projects using Cisco Packet Tracer

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

Basics of Wireless Sensor Networks, Topologies and Application

Basics of Wireless Sensor Networks, Topologies and Application

This article will learn about Wireless Sensor Networks, the need to develop WSN, Applications, and their Topologies. However, before getting into the topic, let us brush up on some basic information about the sensor. A sensor is an appliance that detects changes in...

Image Sensor and Its Types

Image Sensor and Its Types

In this post, we are going to discuss and explain the concept of Image Sensor. In the world of technology, images play an important role. We are identifying and classifying the objects with the help of Machine learning concepts. This all possible with the help of...

How to design a Wireless Blind Stick using  nRF24L01 Module?

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

How to implement Machine Learning on IoT based Data?

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Sensor Technology in IoT and How it works

Sensor Technology in IoT and How it works

Senors Technology plays a key role when we use it in IoT. The sensor is a device which detects changes in an environment or measures physical property, record, indicates or respond to it. so, different types of applications require different types of sensors to data...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....