EasyManuals Logo

Freenove Ultimate Starter Kit User Manual

Freenove Ultimate Starter Kit
286 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #236 background imageLoading...
Page #236 background image
Chapter 21 Hygrothermograph DHT11
236
www.freenove.com
support@freenove.com
31
32
33
e xcept KeyboardInterrupt:
GPIO.cleanup()
exit()
In this project code, we use a module "Freenove_DHT.py", which provide method of reading sensor DHT. It
is located in the same directory with program files "DHT11.py". By using this library, we can easily read the
DHT sensor. First create a DHT class object in the code.
dht = DHT.DHT(DHTPin) #create a DHT class object
And then in the "while" cycle, use chk = dht.readDHT11 (DHT11Pin) to read the DHT11, and determine
whether the data read is normal according to the return value "chk". And then use variable sumCnt to record
number of reading times.
wh ile(True):
sumCnt += 1 #counting number of reading times
chk = dht. readDHT11(DHTPin) #read DHT11 and get a return value. Then
determine whether data read is normal according to the return value.
print ("The sumCnt is : %d, \t chk : %d"%(sumCnt,chk))
if (chk is dht. DHTLIB_OK): #read DHT11 and get a return value. Then
determine whether data read is normal according to the return value.
print("DHT11,OK!")
elif(chk is dht. DHTLIB_ERROR_CHECKSUM): #data check has errors
print("DHTLIB_ERROR_CHECKSUM!!")
elif(chk is dht. DHTLIB_ERROR_TIMEOUT): #reading DHT times out
print("DHTLIB_ERROR_TIMEOUT!")
else: #other errors
print("Other error!")
Finally print the results:
pr int("Humidity : %.2f, \t Temperature : %.2f \n"%(dht.humidity,dht.temperature))
Module "Freenove_DHT.py" contains a DHT class. And class functions def readDHT11 (pin) is used to read
sensor DHT11 and store the temperature and humidity data read to member variables humidity and
temperature.
Freenove_DHT Module
This is a Python module for reading the temperature and humidity data of the DHT sensor. Partial
functions and variables are described as follows:
Variable humidity: store humidity data read from sensor
Variable temperature: store temperature data read from sensor
def readDHT11 (pin): read the temperature and humidity of sensor DHT11, and return values used to
determine whether the data is normal.

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Freenove Ultimate Starter Kit and is the answer not in the manual?

Freenove Ultimate Starter Kit Specifications

General IconGeneral
BrandFreenove
ModelUltimate Starter Kit
CategorySingle board computers
LanguageEnglish