DS18B20 Temperature Sensor with Raspberry Pi and ThingSpeak Cloud

DS18B20 sensor with raspberry pi

In this article I have briefly explained how to interface DS18B20 Temperature Sensor with Raspberry Pi and ThingSpeak cloud.


Overview

Raspberry Pi is a tiny computing device capable of performing many tasks that your computer does. The GPIO Pins of Raspberry Pi are used to control, monitor and transmit digital data that comes from the DS18B20 sensor. The DS18B20 is a 1- Wire Programmable waterproof temperature sensor which can measure a range of temperature from -55°C to +125°C with ±0.5°C accuracy.

In this project, the DS18B20 temperature sensor is integrated with Raspberry Pi to collect temperature data. Later, Raspberry Pi will send the retrieved sensor data to ThingSpeak Cloud for visualization and monitoring.

Hardware Required

  • Raspberry Pi (Any Model)
  • DS18B20 Temperature Sensor
  • 4.7 KΩ Resistor
  • Jumper Wires
  • Breadboard

Software Required

  • Python

  • ThingSpeak


Circuit Diagram

The circuit diagram of interfacing DS18B20 temperature sensor with Raspberry Pi is shown below.

Circuit diagram for DS18B20 sensor with raspberry pi

Interfacing DS18B20 with Raspberry Pi

Build the circuit as per the above diagram with your Raspberry Pi turned off.

Circuit diagram for DS18B20 sensor with raspberry pi

  • Place the temperature sensor onto the breadboard.
  • Do the following circuit connections
    • Place a wire from GND of sensor to Ground Pin of the Pi.
    • Place a wire from the DQ signal to Pin 7 (GPIO4 1-Wire).
    • Place a wire from Vdd of sensor to Pin 2 (5V) of the Raspberry Pi.
    • To ensure stable data transfer, place a 4.7 KΩ resistor between the DQ signal and power pin of the sensor.

Configuring the Raspberry Pi

The next step is to configure Raspberry Pi as per the requirements. We need to perform two tasks to enable our DS18B20 sensor for use.

Installing Required Libraries

To begin with we need to install a Python library "w1thermsensor" that enables the python code to communicate with the sensor. This package is supported for temperature sensors like DS18B20, DS1822 and DS18S20.

To install w1thermsensor library, run the following command from the command line.

pi@raspberrypi:~ $ sudo pip install w1thermsensor

For Python3, run the following code.

pi@raspberrypi:~ $ sudo pip3 install w1thermsensor

Inorder to use ThingSpeak service, install the following libraries from the command line.

pi@raspberrypi:~ $ sudo pip install thingspeak

The command httplib and urllib is used to send sensor data to ThingSpeak via HTTP.

pi@raspberrypi:~ $ sudo apt-get install httplib
 
pi@raspberrypi:~ $ sudo apt-get install urllib


Enable 1 Wire Serial Interface

The DS18B20 sensor uses one wire bus system for communication with the Raspberry Pi. To add support for one wire, we need to enable "1 Wire Serial Interface" option which can be accomplished using "Raspberry Pi Configuration". Let's move to the steps.

Click on Applications menu > Preferences > Raspberry Pi Configuration

This window will show up

Enable 1 Wire Serial Interface

Go to Interfaces tab and click on "Enabled" radio button for the 1 Wire interface option. Finally  Click on OK button and reboot the Raspberry Pi.

Enable 1 Wire Serial Interface


Setting Up ThingSpeak Account

ThingSpeak is an open source analytics platform mainly used in IOT applications to retrieve, store and monitor data collected from things using HTTP. The data are sent to ThingSpeak via LAN or internet.

Before we proceed, you need to create a ThingSpeak account. Click on this link to create an account. After creating the account, the following window will show up. Click on the "New Channel" button. 

thingspeak account

Now define your Channel by entering Name, Description, required field details. The field value defines your sensor data. The data retrieved from particular sensor will be posted in that field. Here I have used only one field to store data collected from DS18B20 sensor. Finally click on Save Channel.

thingspeak account

Once you have clicked "Save Channel", you will be redirected to Window that contains your channel details like "Channel ID", "Author" and "Access". Click on the "API Keys" tab. Here you will find two API key values "Write API Key" and "Read API Key". Have a copy of these key values to use it in code. 


thingspeak account
The right side of the window contains API Request Link for Read and Write Channel Feed. Make a copy of these links for later use.

thingspeak account


Python Code

To write Python code, go to any Python editor found under the Programming menu and create a new blank file. 

Following is the python code for interfacing DS18B20 sensor with Raspberry Pi and upload the retrieved sensor data to ThingSpeak cloud for visualization. It is very much important to replace the API Key values with that of yours.


from time import sleep
import urllib.request
from w1thermsensor import W1ThermSensor
sensor1 = W1ThermSensor()
api='XJGBTCSSJIJYRV8K'
delay = 5
def water():
        temperature = sensor1.get_temperature()
        print("The temperature is %s celsius" % temperature)
        return(str(temperature))
def main():
        print ('starting...')
        baseURL = 'https://api.thingspeak.com/update?api_key=XJGBTCSSJIJYRV8K'
        print (baseURL)
        while True:
            try:
                watertemp=water();            
                f = urllib.request.urlopen(baseURL + "&field1=%s" % (watertemp))
                print (f.read())
                f.close()
                sleep(int(delay))
            except:
                print ('exiting.')
                break
if __name__ == '__main__':
        main()

To run the code, click on Run >> Run Current Script and you will see the Python Shell window pop up and start displaying temperature data.

python code

The final step is to visit your ThingSpeak channel and you will see the DS18B20 temperature sensor data values being updated in graphical form.

DS18B20 in thingspeak

Thanks for reading this tutorial. If you have any technical inquiries post it in comment section.


Post a Comment

1 Comments