End Presentation


TWiki Slide Show
Next
DHT11 Temperature and Humidity Sensor

Lecture 8

Uli Raich

UCC semester 2017/2018

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 1 of 32





















TWiki Slide Show
Next
A single GPIO pin
We have seen how we can drive an LED from a single GPIO pin

programmed as output pin or how we can read its state

through another GPIO pin, programmed as input pin.

Can one do more with a single pin?

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 2 of 32





















TWiki Slide Show
Next
DHT11 pinout
The DHT11 has only
3 pins:

  • Vcc
  • Ground
  • one data pin

Be careful:
The pinout of the DHT11 in our sensor kit
is different from the one shown on the photo!

The PCB layout of the PCB board in my
sensor kit is different!

dht11.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 3 of 32





















TWiki Slide Show
Next
Serial Protocol
How can we, with a single pin

  • initiate a measurement
  • read humidity and temperature data
  • make sure the data are correct?
The answer is: a serial protocol
and the DHT11 implements its own serial protocol

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 4 of 32





















TWiki Slide Show
Next
Reading and understanding the data sheet
Let’s have a look at the DHT11 data sheet:

Most of the following information is just a copy from the data sheet.

dht11Intro.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 5 of 32





















TWiki Slide Show
Next
How does a resistive humidity measurement work?
resistiveHumiMeas.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 6 of 32





















TWiki Slide Show
Next
Resistive Humidity Measurement(2)
  • Variations in relative humidity produce changes variations
    of resistivity,which is measured.
  • The resistance of certain hygroscopic materials such as
    hygroscopic salts have such a property
  • These materials are mounted as thin films on
    the substrate with metal contacts
  • The devices can be very small

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 7 of 32





















TWiki Slide Show
Next
The NTC Thermistor
ntcThermistor.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 8 of 32





















TWiki Slide Show
Next
A processor on chip
In order to convert these measurements into numeric values

and send them to the end user through a serial protocol,

a preprogrammed micro-controller must be implemented on the chip.

In the case of the DHT11 this is an 8 bit micro-controller,

which does the conversion into binary and which creates the serial protocol

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 9 of 32





















TWiki Slide Show
Next
Text from the data sheet
humiCalib.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 10 of 32





















TWiki Slide Show
Next
Measurement Precision
dht11Specs.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 11 of 32





















TWiki Slide Show
Next
How to connect the device
connections.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 12 of 32





















TWiki Slide Show
Next
Powering the device
As we can see from the specs below, the DHT11 power line

can be directly connected to the cobbler 3.3V (or the 5V) line

power.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 13 of 32





















TWiki Slide Show
Next
Single Wire two way interface
twoWay.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 14 of 32





















TWiki Slide Show
Next
Overall Communication Process
overall.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 15 of 32





















TWiki Slide Show
Next
How to initiate a Measurement?
initiate.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 16 of 32





















TWiki Slide Show
Next
What does this mean for our program?
We must:
  • Program the GPIO pin onto which we connected our DHT11 as output
  • We must pull this line done (send a login level zero) and
    keep it low for a minimum of 18 ms
  • We must pull it high again and wait for another 20-40 μs
  • Finally we must re-program the pin as input and wait for the DHT11 to respond.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 17 of 32





















TWiki Slide Show
Next
Response from the DHT11
response.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 18 of 32





















TWiki Slide Show
Next
A zero bit
zero.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 19 of 32





















TWiki Slide Show
Next
A one bit
one.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 20 of 32





















TWiki Slide Show
Next
Complicated?
Wow, this looks complicated. How can we write a program to do all this?

Let's start slowly:

  • Initiating a measurement seems do-able
  • Why not just read the data from the device every 5 µs
    and print out the information?
The zeros and ones do not look too convincing.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 21 of 32





















TWiki Slide Show
Next
What the user of the device wants
The user of the device would like to have a library which hides

all these details. He wants functions to

  • Initialize the device
  • Start a measurement
  • Make sure the checksum is ok
  • Read temperature and humidity values
  • Maybe know when the measurement was made

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 22 of 32





















TWiki Slide Show
Next
The library
How do we write a library?

A library consist of min. 2 files:

  • an include file (dht11.h)
  • and implementation file (dht11.c)
Very often you have a single include but several implementation files.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 23 of 32





















TWiki Slide Show
Next
The include file of the dht11 library
dht11H-1.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 24 of 32





















TWiki Slide Show
Next
dht11.h (2)
dht11H-2.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 25 of 32





















TWiki Slide Show
Next
The library code
  • dht11Init simply calls wiringPiSetup()
  • dht11Measurement
  • Programs the gpio pin as output and sends the start sequence
  • Switches back the gpio pin to input and reads the data coming from the dht11 every 5 µs and saves the data into an array
  • Analyses the data and extracts temperature, humidity and the checksum
  • Calculates the checksum and compares it with the one coming from the device
  • If checksums are ok, sends the data to “validData”
  • Keeps the time stamp of the measurement

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 26 of 32





















TWiki Slide Show
Next
Reading the final data
  • dht11GetValidTemperature()
  • dht11GetValidHumidity()
  • dht11getValidMeasTime()
  • dht11getChecksum()
  • dht11getDeviceChecksum()

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 27 of 32





















TWiki Slide Show
Next
Starting the measurement
The data pin has been programmed as output earlier

measurement.png

You can print these data and have a look at them with gnuplot. Try analyzing be hand.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 28 of 32





















TWiki Slide Show
Next
Analyzing the data
This is the tricky bit and you are invited to give it a try. I have written a

routine reading a single bit, which is called 40 times for all the 40 bits in the data.

Please note one nasty problem:
Linux is a multi-tasking system and permanently receives interrupts.

While these interrupts are treated the DHT11 data are continuing to flow

but are not taking into account, leading to corrupt data.

Since the duration of the protocol takes ~ 4 ms the probability is non negligible

and the checksum test in obligatory to make sure the data are consistent.

In case of a checksum error you must repeat the measurement

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 29 of 32





















TWiki Slide Show
Next
Creating a shared library
The Makefile says it all

shareLib.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 30 of 32





















TWiki Slide Show
Next
Where is the include file, where the binary of the shared lib?
I installed the include file in /opt/ucc/include

and the library in /opt/ucc/lib.

These are non-standard directories and must be declared in the Makefile

-I /opt/ucc/include for the include file in CFLAGS

-L /opt/ucc/lib for the library path

Since the shared library is loaded separately when running the

main program, it must be found by the system:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/ucc/lib

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 31 of 32





















TWiki Slide Show
Next
Documentation
When writing a library that is supposed to be used by other people,

documentation is of utmost importance

Of course the function prototypes in the include file give

some indication but this normally not enough.

I use the doxygen in code documentation system which allows to

generate documentation from the source layout and

comments in the code with special tags.

It creates html of latex doc


First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 32 of 32





















First slide Previous End Presentation






























-- Uli Raich - 2017-10-20

Comments


I Attachment History Action Size Date Who Comment
PNGpng connections.png r1 manage 42.6 K 2017-10-20 - 16:27 UnknownUser  
PNGpng dht11.png r1 manage 597.9 K 2017-10-20 - 16:34 UnknownUser  
PNGpng dht11H-1.png r1 manage 24.8 K 2017-10-23 - 13:47 UnknownUser  
PNGpng dht11H-2.png r1 manage 22.3 K 2017-11-01 - 08:05 UnknownUser  
PNGpng dht11H-2v2.png r1 manage 22.3 K 2017-11-01 - 08:05 UnknownUser  
PNGpng dht11Intro.png r1 manage 26.5 K 2017-10-20 - 16:27 UnknownUser  
PNGpng dht11Specs.png r1 manage 11.5 K 2017-10-20 - 16:27 UnknownUser  
PNGpng humiCalib.png r1 manage 24.3 K 2017-10-20 - 16:27 UnknownUser  
PNGpng initiate.png r1 manage 153.8 K 2017-10-20 - 16:27 UnknownUser  
Unknown file formatodp lecture_8.odp r1 manage 1715.1 K 2017-11-01 - 09:25 UnknownUser  
PNGpng measurement.png r1 manage 29.9 K 2017-10-23 - 13:55 UnknownUser  
PNGpng ntcThermistor.png r1 manage 80.6 K 2017-10-20 - 16:27 UnknownUser  
PNGpng one.png r1 manage 149.6 K 2017-10-20 - 16:27 UnknownUser  
PNGpng overall.png r1 manage 180.3 K 2017-10-20 - 16:27 UnknownUser  
PNGpng power.png r1 manage 14.5 K 2017-10-20 - 16:27 UnknownUser  
PNGpng resistiveHumiMeas.png r1 manage 54.9 K 2017-10-20 - 16:27 UnknownUser  
PNGpng response.png r1 manage 28.8 K 2017-10-20 - 16:28 UnknownUser  
PNGpng shareLib.png r1 manage 27.9 K 2017-10-23 - 14:25 UnknownUser  
PNGpng twoWay.png r1 manage 32.0 K 2017-10-20 - 16:28 UnknownUser  
PNGpng zero.png r1 manage 181.6 K 2017-10-20 - 16:28 UnknownUser  

This topic: Embedded_Systems > WebHome > LectureSlides > Lecture8:DHT11TemperatureAndHumiditySensor
Topic revision: r7 - 2023-11-19 - UliRaich
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback