0% found this document useful (0 votes)
2 views

Assignment 5 1

The document outlines an assignment to interface a DHT22 sensor and relay with a Raspberry Pi to display temperature and humidity data. It includes a Python script that reads the sensor data, turns on a relay if the temperature exceeds 30°C, and turns it off otherwise. The result shows a successful execution with temperature and humidity readings displayed on the terminal.

Uploaded by

Dugu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 5 1

The document outlines an assignment to interface a DHT22 sensor and relay with a Raspberry Pi to display temperature and humidity data. It includes a Python script that reads the sensor data, turns on a relay if the temperature exceeds 30°C, and turns it off otherwise. The result shows a successful execution with temperature and humidity readings displayed on the terminal.

Uploaded by

Dugu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Assignment – 5

Interface DHT22 Sensor and Relay with Pi and Display


the current Temperature in Celsius and Percentage
Humidity on Pi Terminal and Turn ON the RELAY if
Temperature reaches the Threshold value & Turn OFF
RELAY otherwise.
Threshold Temperature = 30*C

Solution
# Connecting Pin-2 of DHT Sensor to Pin 17 of Raspberry Pi.
# Connecting Input/Signal Pin of RELAY to Pin 13 of Raspberry Pi.

import RPi.GPIO as GPIO


from time import sleep
import Adafruit_DHT #importing the Adafruit library for DHT Sensor

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(13,GPIO.OUT)

sensor = Adafruit_DHT.AM2302 #create an instance of the sensor type


print (‘Getting data from the sensor’)
#humidity and temperature are 2 variables that store the values received from the sensor

humidity, temperature = Adafruit_DHT.read_retry(sensor,17)


print ('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))

# Set the RELAY Pin high when the temperature reaches Threshold value 30 Degree Centigrade

if temperature >= 30:


GPIO.output(13,0) # Relay is turned on
print(‘Relay is ON')
sleep(5)
GPIO.output(13,1) # Relay is turned off after delay of 5 seconds
else:
GPIO.output(13,1) # Relay is turned off
print(‘Relay is OFF')
Result
pi@raspberrypi:~ $python DHT_REL.py
Getting data from the sensor
Temp=35.5 Humidity=55.8%
Relay is ON

You might also like