Difference: Lecture8:DHT11TemperatureAndHumiditySensor (1 vs. 7)

Revision 72023-11-19 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: DHT11 Temperature and Humidity Sensor

Line: 29 to 29
 

Reading and understanding the data sheet

Changed:
<
<
Let’s have a look at the DHT11 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.
Line: 112 to 112
 
  • 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.

Changed:
<
<

Response from DHT11response.png

>
>

Response from the DHT11

response.png

 

A zero bit

Line: 132 to 134
 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?
Changed:
<
<
The zeros and ones do not look too convincing.
Let’s save those onto a file and look at it with gnuplot
---++
>
>
The zeros and ones do not look too convincing.

 

What the user of the device wants

The user of the device would like to have a library which hides

Line: 251 to 253
  It creates html of latex doc
Changed:
<
<
%SLIDESHOWEND%
>
>

%SLIDESHOWEND%
  -- Uli Raich - 2017-10-20

Comments

Changed:
<
<
<--/commentPlugin-->
>
>

<--/commentPlugin-->
 
META FILEATTACHMENT attachment="connections.png" attr="" comment="" date="1508516877" name="connections.png" path="connections.png" size="43672" user="uli" version="1"
META FILEATTACHMENT attachment="dht11Intro.png" attr="" comment="" date="1508516877" name="dht11Intro.png" path="dht11Intro.png" size="27160" user="uli" version="1"

Revision 62017-11-01 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: DHT11 Temperature and Humidity Sensor

Line: 278 to 278
 
META FILEATTACHMENT attachment="shareLib.png" attr="" comment="" date="1508768720" name="shareLib.png" path="shareLib.png" size="28547" user="uli" version="1"
META FILEATTACHMENT attachment="dht11H-2.png" attr="" comment="" date="1509523515" name="dht11H-2.png" path="dht11H-2.png" size="22822" user="uli" version="1"
META FILEATTACHMENT attachment="dht11H-2v2.png" attr="" comment="" date="1509523556" name="dht11H-2v2.png" path="dht11H-2v2.png" size="22822" user="uli" version="1"
Added:
>
>
META FILEATTACHMENT attachment="lecture_8.odp" attr="" comment="" date="1509528337" name="lecture_8.odp" path="lecture_8.odp" size="1756293" user="uli" version="1"

Revision 52017-11-01 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: DHT11 Temperature and Humidity Sensor

Line: 274 to 274
 
META FILEATTACHMENT attachment="zero.png" attr="" comment="" date="1508516904" name="zero.png" path="zero.png" size="185952" user="uli" version="1"
META FILEATTACHMENT attachment="dht11.png" attr="" comment="" date="1508517246" name="dht11.png" path="dht11.png" size="612270" user="uli" version="1"
META FILEATTACHMENT attachment="dht11H-1.png" attr="" comment="" date="1508766477" name="dht11H-1.png" path="dht11H-1.png" size="25351" user="uli" version="1"
Deleted:
<
<
META FILEATTACHMENT attachment="dht11H-2.png" attr="" comment="" date="1508766477" name="dht11H-2.png" path="dht11H-2.png" size="22372" user="uli" version="1"
 
META FILEATTACHMENT attachment="measurement.png" attr="" comment="" date="1508766902" name="measurement.png" path="measurement.png" size="30589" user="uli" version="1"
META FILEATTACHMENT attachment="shareLib.png" attr="" comment="" date="1508768720" name="shareLib.png" path="shareLib.png" size="28547" user="uli" version="1"
Added:
>
>
META FILEATTACHMENT attachment="dht11H-2.png" attr="" comment="" date="1509523515" name="dht11H-2.png" path="dht11H-2.png" size="22822" user="uli" version="1"
META FILEATTACHMENT attachment="dht11H-2v2.png" attr="" comment="" date="1509523556" name="dht11H-2v2.png" path="dht11H-2v2.png" size="22822" user="uli" version="1"

Revision 42017-10-26 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: DHT11 Temperature and Humidity Sensor

Line: 16 to 16
 

DHT11 pinout

Changed:
<
<
The DHT11 has only
3 pins:

  • Vcc
  • Ground
  • one data pin
dht11.png
>
>
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
 

Serial Protocol

Line: 215 to 215
 shareLib.png

Added:
>
>

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

 

Documentation

When writing a library that is supposed to be used by other people,

Revision 32017-10-23 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: DHT11 Temperature and Humidity Sensor

Line: 132 to 132
 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?
Changed:
<
<
The zeros and ones do not look too convincing.
Let’s save those onto a file and look at it with gnuplot

>
>
The zeros and ones do not look too convincing.
Let’s save those onto a file and look at it with gnuplot
---++

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

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.

The include file of the dht11 library

dht11H-1.png

dht11.h (2)

dht11H-2.png

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

Reading the final data

  • dht11GetValidTemperature()
  • dht11GetValidHumidity()
  • dht11getValidMeasTime()
  • dht11getChecksum()
  • dht11getDeviceChecksum()

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.

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

Creating a shared library

The Makefile says it all

shareLib.png

 

Added:
>
>

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

  %SLIDESHOWEND%
Line: 158 to 255
 
META FILEATTACHMENT attachment="twoWay.png" attr="" comment="" date="1508516903" name="twoWay.png" path="twoWay.png" size="32717" user="uli" version="1"
META FILEATTACHMENT attachment="zero.png" attr="" comment="" date="1508516904" name="zero.png" path="zero.png" size="185952" user="uli" version="1"
META FILEATTACHMENT attachment="dht11.png" attr="" comment="" date="1508517246" name="dht11.png" path="dht11.png" size="612270" user="uli" version="1"
Added:
>
>
META FILEATTACHMENT attachment="dht11H-1.png" attr="" comment="" date="1508766477" name="dht11H-1.png" path="dht11H-1.png" size="25351" user="uli" version="1"
META FILEATTACHMENT attachment="dht11H-2.png" attr="" comment="" date="1508766477" name="dht11H-2.png" path="dht11H-2.png" size="22372" user="uli" version="1"
META FILEATTACHMENT attachment="measurement.png" attr="" comment="" date="1508766902" name="measurement.png" path="measurement.png" size="30589" user="uli" version="1"
META FILEATTACHMENT attachment="shareLib.png" attr="" comment="" date="1508768720" name="shareLib.png" path="shareLib.png" size="28547" user="uli" version="1"

Revision 22017-10-20 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: DHT11 Temperature and Humidity Sensor

Added:
>
>

Lecture 8

Uli Raich

UCC semester 2017/2018

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?

DHT11 pinout

The DHT11 has only
3 pins:

  • Vcc
  • Ground
  • one data pin
dht11.png

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

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

How does a resistive humidity measurement work?

resistiveHumiMeas.png

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

The NTC Thermistor

ntcThermistor.png

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

Text from the data sheet

humiCalib.png

Measurement Precision

dht11Specs.png

How to connect the device

connections.png

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

Single Wire two way interface

twoWay.png

Overall Communication Process

overall.png

How to initiate a Measurement?

initiate.png

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.

Response from DHT11response.png

A zero bit

zero.png

A one bit

one.png

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.
Let’s save those onto a file and look at it with gnuplot

  %SLIDESHOWEND%
Line: 10 to 143
 

Comments

<--/commentPlugin-->
\ No newline at end of file
Added:
>
>
META FILEATTACHMENT attachment="connections.png" attr="" comment="" date="1508516877" name="connections.png" path="connections.png" size="43672" user="uli" version="1"
META FILEATTACHMENT attachment="dht11Intro.png" attr="" comment="" date="1508516877" name="dht11Intro.png" path="dht11Intro.png" size="27160" user="uli" version="1"
META FILEATTACHMENT attachment="dht11Specs.png" attr="" comment="" date="1508516877" name="dht11Specs.png" path="dht11Specs.png" size="11789" user="uli" version="1"
META FILEATTACHMENT attachment="humiCalib.png" attr="" comment="" date="1508516877" name="humiCalib.png" path="humiCalib.png" size="24930" user="uli" version="1"
META FILEATTACHMENT attachment="initiate.png" attr="" comment="" date="1508516877" name="initiate.png" path="initiate.png" size="157520" user="uli" version="1"
META FILEATTACHMENT attachment="ntcThermistor.png" attr="" comment="" date="1508516877" name="ntcThermistor.png" path="ntcThermistor.png" size="82544" user="uli" version="1"
META FILEATTACHMENT attachment="one.png" attr="" comment="" date="1508516878" name="one.png" path="one.png" size="153216" user="uli" version="1"
META FILEATTACHMENT attachment="overall.png" attr="" comment="" date="1508516878" name="overall.png" path="overall.png" size="184640" user="uli" version="1"
META FILEATTACHMENT attachment="power.png" attr="" comment="" date="1508516878" name="power.png" path="power.png" size="14804" user="uli" version="1"
META FILEATTACHMENT attachment="resistiveHumiMeas.png" attr="" comment="" date="1508516879" name="resistiveHumiMeas.png" path="resistiveHumiMeas.png" size="56214" user="uli" version="1"
META FILEATTACHMENT attachment="response.png" attr="" comment="" date="1508516903" name="response.png" path="response.png" size="29499" user="uli" version="1"
META FILEATTACHMENT attachment="twoWay.png" attr="" comment="" date="1508516903" name="twoWay.png" path="twoWay.png" size="32717" user="uli" version="1"
META FILEATTACHMENT attachment="zero.png" attr="" comment="" date="1508516904" name="zero.png" path="zero.png" size="185952" user="uli" version="1"
META FILEATTACHMENT attachment="dht11.png" attr="" comment="" date="1508517246" name="dht11.png" path="dht11.png" size="612270" user="uli" version="1"

Revision 12017-10-20 - uli

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: DHT11 Temperature and Humidity Sensor

-- Uli Raich - 2017-10-20

Comments

<--/commentPlugin-->
 
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