Cos'è ElectroYou | Login Iscriviti

ElectroYou - la comunità dei professionisti del mondo elettrico

Ricerca personalizzata
8
voti

HAVE YOU EVER MADE IT WITH A PICMicro? Double thermometer/hygrometer with DHT22 sensor

Vuoi leggere questo articolo in italiano? Fai click qui!

I was just a high school boy, while my father a friend of him designed and built a small wheather station, making an anemometer and anemoscope. In the following years the weather station improved, hosting a thermometer and a hygrometer. Time has passed and meantime, due to various reasons, the weather station moved from its original location and for some years has been switched-off and coverd, until 2015 when my father and his friend decided to reset it. Wind-meter started to work good; hygrometer was abandoned and thermometer measured false temperatures. I thougt about it and decided to redesign the missing partes, using DHT22 digital sensor.

Indice

DHT22 sensor (aka AM2302)

It is a sensor born in the Arduinos-age (Arduinos, intending different models, original and clones) and I beleive that for that device exist suitable software interfaces/library to manage the sensor. It is brother of DHT11 and DHT21, even if I don't know and never used them.Its pinout is very simple: four pins, one is not oused (NC), tow of them are the power (VCC and GND) and the las one is used for the data-link (DATA).


Picture 1 - DHT22, humidity and thermometer sensor (1)

Picture 1 - DHT22, humidity and thermometer sensor (1)

Since it is a digital sensor, inside of it it is possible to find not only temperature (NTC termistor) and humidity sensors but probably a microcontroller, to interface DHT22 with an external device. This one could be something else than an Arduino!

Picture 2 - DHT22 inside(2)

Picture 2 - DHT22 inside(2)


Schematic

Requirements were clear: get two temperatures and enviromental humidity, inside and outside home, using two DHT22 (yes, I bought two) insted of LM35Z. Moreover, temperature and humidity values had to be shown on a 16x2 LCD display, using a technique able to show all the values. I decided to use a PICmicro to drive everything, but which PIC? Design didn't need so much pins, but looking in my drawers I found a PIC18F2620 which could be used for that project. A too much big PICMicro, you could say. I agree, but I had it and I used it. Other constraints were about PCB dimension, so I used PICMicro and 7805 as PTH devices, the others are SMD.

Picture 3 - Schematic (part 1)

Picture 3 - Schematic (part 1)

Picture 4 - Schematic (part 2)

Picture 4 - Schematic (part 2)

Display has a backlight, so I put a biploar transistor, PWM-driven by PIC, to control backlight, from a maximum value decreasing to a mininum value each 10 seconds. To reset the backlight at the maximum value, user has to press a push button.

Picture 5 - Schematic (part 3)

Picture 5 - Schematic (part 3)

Hardware

From schematich to PCB it is easy but I had some troubles (after 3 PCBs I asked a friend to print it for me), but then I could mount all devices. I was sure to have heard PIC18 to say something as "I am ready!", waiting for its firmware.

Picture 6 - PCB and one sensor used

Picture 6 - PCB and one sensor used

Picture 7 - Panel installation (rear view)

Picture 7 - Panel installation (rear view)

Communication protocol

Before analyzing the C code of the firmware, it is important to show how the communication protocol between DHT22 and microcontroller works. DHT22 has four pins but only one is used to communicate, so it is important to understand how to ask temperature and humidity value to the sensor. Even if the communication uses only 1 pin, it is not one-wire protocol, has weel said in the datasheet. Communication starts with a startup-sequence, where microcontroller acts on DATA wire with well define timing table. After that starts the payload transfer from sensor to micro; payload is made of four data bytes and an addiotional fifth byte containing checksum. Some detail follow.

Startup sequence

Each time micro talks to sensor, it has to start communication with these operation: since DATA line is kept high by a pull-up resistor, the start-of-transmission signal is given from micro pulling low DATA line for at least 1ms. After that micro sets it DATA pin to input mode and the line goes high (pull-up works) and maintened high at least 20-40 us; then a up-and-down dance of DATA line, driven by sensor, as described in the following picture.

Picture 8 - Startup sequence

Data trasmission

Once startup-sequence has finished, sensor trasmints data to micro. As said, sensor sends 5 bytes, total of 40 bits. Each byte is coded as follow:

  • a single bit is 0 if the low-high sequence has timing of 50us and 70us
  • a single bit is 1 if the low-high sequence has timing of 50us e 26-28us

Picture 9 - Bit 0 and bit 1

Relative humidity, temperature and checksum

First two bytes received are about relative humidity, expressed as parts of thousands, not percent. So, if HR is 37.5%, PICmicro receives from sensor a value of 375. Temperature follows, and it is ecpressed as tenth of Celsius degrees, sign included. DHT22 measures temperature form -40°C to +80°C, so it is important to evaluate the sign and then convert the temperature value in Celsius. The last byte is the checksum byte, calculated as 8-bit sum of the previuos four bytes.

Picture 10 - Data payload

Firmware code

I used Microchip XC8 compiler and MPLABX IDE, and the project is very simple. After PIC inits (oscillator, ports, etc) and display inits, we go to infinite loop containing temperature and humidity acquiring and visualizations. At each cycle startup-sequence is called, and it is made as follow:

   /* Waiting while DATA line sets to 0 */
   
   if (validEdge(INTERNAL, FALLING_EDGE) == false)
   {
       return false;
   }        
   
   /* Waiting while DATA line sets to 1 */
   
   if (validEdge(INTERNAL, RISING_EDGE) == false)
   {
       return false;
   }             
   
   /* Waiting while DATA line sets to 0 */
   
   if (validEdge(INTERNAL, FALLING_EDGE) == false)
   {
       return false;
   }             
   
   return true;


The waiting of the DATA line status change in made with a while() cicle; to avoid a firmware stuck, I used a timer as a guardian, to avoid an infinite loop in case of sensor (or DATA line) damages. It was possible to do it also witha state-machine, but the method I used is faster. If TIMER 0 sets its overflow bit (TMR0IF), it means that something has gone wrong; TMR0 period is longer than the timing used to identify bit 0 and bit 1 of payload.

The implementation of the function that check the status changing of pin port is the following:


   boolean validEdge(uint8_t intOrExt, boolean edgeType)
   {
       TMR0 = 0;
       T0CONbits.TMR0ON = 1;   
       INTCONbits.TMR0IF = 0;
       
       if (intOrExt == INTERNAL)
       {
           while ((DHT_INT_READ == edgeType) && (INTCONbits.TMR0IF ==0));    
       } else {
           while ((DHT_EXT_READ == edgeType) && (INTCONbits.TMR0IF ==0));
       }
       
       T0CONbits.TMR0ON = 0;
       if (INTCONbits.TMR0IF == 1)
       {
           return false;
       }  else {
           return true;
       }     
   }


bit coding is simply mad as follow: each time DATA line is low, I read the TMR0 value to determine if bit is 0 or 1

   /* DATA line is low: stop to TIMER 0 and read the value */
   T0CONbits.TMR0ON = 0;
   
   if ((TMR0>=50) && (TMR0<=80)) /* This bit is set to 1 */
   {
       DHT22payLoad[nByte]|=bitPosition;
   } else if ((TMR0>=20) && (TMR0<=35)) /* This bit is set to 0 */
   {
       /* do nothing */
   }

ISR checks the time-that-goes-by, to do two things:

  • counts two-seconds between a data acquirement and the other (alternating between indoor and outdoor sensor each 1 second of time);
  • "counts to ten" and after 10 seconds reduces displayu backlight a little bit; by presisng the push buttom user can reset backlight at the macimum value.


How it works

To install all devices we needed to do some mechanichal modification, i.e. closing the window where the previuos HR-meter was placed.

Picture 11 - Weather station with wind-meter, thermometer and hygrometer

Picture 11 - Weather station with wind-meter, thermometer and hygrometer

Under LCD display there is the push button thet resets the backlight to the maximum value, taking the place of the switch used in the past to switch between indoor and outdoor readings. Noiw this switching is made by firmware each second.

Picture 12 - Temperature and humidity outdoor

Picture 12 - Temperature and humidity outdoor

Picture 13 - Temperature and humidity indoor

Picture 13 - Temperature and humidity indoor

Some recommendations

Do not use long cabling between sensor and microcontroller or you could have wrong readings. Datasheet shows a very important note (paragrap 7, page 5): between two consecutive readings, consider a delay of at least 2 seconds. (collecting period).

Improvments

As I said, probably PIC18F2620 is too much, to interface sensors; a PIC16F could be enough. Considering the possibility ti change micro, I left the space for external oscillator (in fneeded). Serial line hasn't a proper un line-driver (not necessary in case of low bad-rates). Debouncing is raw but it works; don't use this raw method in case of multiple push-buttons!

Firmware

You can download firmware at this link

Insight and resources

I'd like to highlight the article shown on SettoreZero.com (Italian language) that show the use of a PICMicro connected to a DHT22 and to a PC (serial link). Author describes the timing of DHT22, determined with instruments.


Licence

These article, pictures and diagrams follow the licence CREATIVE COMMONS BY-NC-ND 3.0, as described in the legal notice reported.

Licenza Creative Commons

Licenza Creative Commons

Sintesi delle note legali (italiano)

Note legali (italiano)

Legal code (international)

Commons deed (international)

6

Commenti e note

Inserisci un commento

di ,

Way to go!

Rispondi

di ,

Thank you, mir!

Rispondi

di ,

Sorry, the right thing was this ..

Rispondi

di ,

Excellent work ... Great Paolino

Rispondi

di ,

Grazie Riccardo.

Rispondi

di ,

Bravo Paolo, davvero un eccellente lavoro. Apprezzabile anche la scelta della lingua inglese, così da dare maggiore visibilità.

Rispondi

Inserisci un commento

Per inserire commenti è necessario iscriversi ad ElectroYou. Se sei già iscritto, effettua il login.