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.
Interfacing DS18B20 with Raspberry Pi
Build the circuit as per the above diagram with your Raspberry Pi turned off.
- 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.
Inorder to use ThingSpeak service, install the following libraries from the command line.
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
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.
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.
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.
Python Code
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.
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.
The final step is to visit your ThingSpeak channel and you will see the DS18B20 temperature sensor data values being updated in graphical form.
1 Comments
Thank you for this comprehensive guide!
ReplyDelete