You are on page 1of 19

Course Code : U18EC603A

Course Name: INDUSTRIAL INTERNET OF THINGS


W10_CDT28
Class Discussion Topic (CDT)
CDT28 – Programming for interfacing
Analog/ Digital Temperature Sensor

Dr G RAGHOTHAM REDDY
Professor, Dept of ECE

04/27/2023
Lecture Outcomes

After completion of this lecture, students will be


able to
LO1 : Create a RPi circuit to measure temperature
and humidity using KY-015 (DTH sensor)

2
DHT11 Temperature And Humidity Sensor

The DHT11 Sensor is a


“Digital Humidity and
Temperature” used to
measure indoor Temperature
and Humidity (it is not
indicated for outdoor uses).
Features:
1. Input Supply voltage (VDC): 3.3 ~ 5
2. Supply Current (mA): measurement 0.3mA
standby 60μA.
3. Temperature measurement range: 0~50 degrees
4. Temperature measurement error: ±2 degrees
5. Humidity measurement range: 20%~95%RH
6. Humidity measurement error: ±5%RH
Raspberry Pi Pinout & Tech Spec
DHT11 Sensor With Raspberry PI and Python

 To configure the DHT11


humidity sensor on a
Raspberry Pi you need to
connect the VCC and GND
pins to +5V and GND from
Raspberry Pi and the Data
Pin of DHT11 to a GPIO PIN
of the Raspberry Pi,
according to the following
wiring diagram.

Interfacing process
Data pin of
sensor to GPIO
21 (or 40 pin)
of RPi

GND pin of
sensor to GND
(or 39 pin) of
RPi
Vcc pin of sensor to 3.3V (or 1 pin) of RPi
• Power up the Rpi
• Enable SSH (Wi-Fi) communication

Command window-Installing python packages


 sudo apt update
 sudo apt upgrade
 python3 –version
 sudo apt install python3 # install Python 3
 sudo apt install python3-pip
Enable I2C and SPI
Enable and configure I2C (standard designed to allow one chip to talk to
another)
sudo apt install -y python-smbus
sudo apt install -y i2c-tools
Enable Raspberry PI I2C kernel support with:
sudo raspi-config
• In the following config screen, go to Interfacing Options > I2C. Enable
it by pressing yes on next screen & following confirmation. Once
enabled, you should be forwarded again to the raspi-config home page.
• To enable SPI, go again to Interfacing Options >SPI. Enable it by
pressing yes on the next screen and following confirmation.
• Click Finish on raspi-config home page to go back ssh terminal.
Install Python Libraries
 sudo apt install python3-rpi.gpio # Install RPI.GPIO if not
installed
 pip3 install adafruit-blinka # install adafruit-blinka library.
#This includes also a few different libraries such as adafruit-
pureio (our ioctl-only i2c library), spidev (for SPI interfacing),
Adafruit-GPIO (for detecting your board), and adafruit-blinka.
Setup CircuitPython-DHT
Following adafruit dht library will be required to enable
communication of Raspberry PI with DHT11 sensor.
 pip3 install adafruit-circuitpython-dht
 sudo apt install libgpiod2
Implementation of python code

import time
import board
import adafruit_dht
#Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT11(board.D17)
# GPIO 17 pin, board11 connected to Data pin
while True:
try:
# Print the values to the serial port
temperature_c = dhtDevice.temperature
temperature_f = temperature_c * (9 / 5) + 32
humidity = dhtDevice.humidity
print("Temp: {:.1f} F / {:.1f} C Humidity: {}% "
.format(temperature_f, temperature_c, humidity))
except RuntimeError as error: # Errors happen fairly
often, DHT's are hard to read, just keep going
print(error.args[0])
time.sleep(2.0)
Simulation Results
pi@raspberrypi:~ $ python3 DHT11Test.py
Temp: 66.2 F / 19.0 C Humidity: 56%
Temp: 66.2 F / 19.0 C Humidity: 56%
Checksum did not validate. Try again.
Temp: 66.2 F / 19.0 C Humidity: 56%
A full buffer was not returned. Try again.
Temp: 66.2 F / 19.0 C Humidity: 56%
Checksum did not validate. Try again.
Temp: 66.2 F / 19.0 C Humidity: 56%
Temp: 66.2 F / 19.0 C Humidity: 56%
Temp: 66.2 F / 19.0 C Humidity: 56%
Temp: 66.2 F / 19.0 C Humidity: 56%
Summary
Working of DHT11
Measure temperature and humidity with RPi
using Python
Lecture Outcomes- Revisited

Having completed the discussion on Programming


for interfacing Analog/Digital Temperature
Sensor now students should be able to
LO1 : Create a RPi circuit to measure temperature
and humidity using KY-015 (DTH sensor)
LECTURE LEVEL PRACTICE PROBLEMS

1.LLP1(on LLO1): Develop a RPi circuit to


measure temperature and humidity using a DTH
sensor (KY-015)?
Further Reading
Refer:
https://peppe8o.com/using-raspberry-pi-with-dht11-
temperature-and-humidity-sensor-and-python/
Happy Learning

You might also like