9 Replies Latest reply on Jan 30, 2015 5:47 AM by Martech9

    DHT22 Interface and source


      Anyone who knows me, knows that I sometimes bang on about how Galileo is different from other arduino boards due to the fact that it's a full computing system, not a micro-processor hosted board. That give rise to the fact that the GPIO on Galileo is has non-deterministic timing, for reasons that I have covered before, several times.


      So, what happens when you need to interface to a device which requires specific timing on the IO? Well, just as you would if you were interfacing that device to a PC or Mac, then you need to use an appropriate interface to make the GPIO interface of the device compatible with one of the many standard interfaces available on Galileo.


      In this example, I have interfaced a DHT22 (Also known as AM2303) sensor which requires fixed rate GPIO, to the UART (CMOS Level RS232) of the Galileo.


      My interface uses a very cheap PIC12F629 to provide the interface conversion. Here is the schematic :-


      You connect +5V and GND to the PWR Header, and the DHT22/AM2303 Sensor to the 4-Pin header.

      JP2 provides an RS232 Transmit signal which transmits the data back to the host, and GND along with an AUX signal which is not yet implemented, but you could use is as RS232 RX, or as a trigger or anything else really.


      The ICSP header simply allows you to program the PIC with a PicKIT 2/3 or ICD3 programmer.

      As you can see, the interface uses just a PIC, some headers and a single 1K resistor. The whole circuit could easily be knocked up on some veroboard, and would cost about $2 in total.


      Here is the circuit formed on some breadboard:

      Screen Shot 2014-10-15 at 13.50.49.png

      (I added an LED and pullup resistor on the TX line, but that is optional).


      The source code is attached.

      The source is a single file of C code which includes everything you need.

      It sets up the hardware, and uses a software UART to transmit the data from the sensor back to the host at 14.4 Kbps. It reads from the sensor about once per second, and you can tweak this in the source, or you could use that AUX input to only read the sensor on receipt of a signal on that pin. The long and short pulses from the sensor are decoded to create the 5 Byte response.

      The data sent from the PIC consists of the byte 0xAA, followed by the 5 Bytes from the sensor, followed by a <CR> character. See the DHT22 or AM2303 data sheet for details of the 5 Bytes received from the sensor.

      The 0xAA is useful as a SYNC byte and for auto-detecting the baudrate, where possible, since in binary a start bit, the byte 0xAA and a stop bit will form the pattern:


      The <CR> character is useful for detecting the end of the input. The output repeats every 1 second.

        • 1. Re: DHT22 Interface and source

          it appears that the code and schematic do not agree. the use of GP1 and GP2 seem to be 'flipped'

          • 2. Re: DHT22 Interface and source

            You are right - well spotted!

            In fact in the code GP0 is attached to the DHT22, and GP1 is software UART output. GP2 isn't used at all.


            I will re-draw the schematic and re-post it here.

            • 3. Re: DHT22 Interface and source

              have had a chance to correct the information presented here and repost it?

              • 4. Re: DHT22 Interface and source



                I would like to add a bluetooth module instead of a connection to a  PC ( Wireless Serial 4 Pin Bluetooth RF Transceiver Module HC-06 RS232 With backplane ).

                Have you try that  on this kind of  PIC?




                • 5. Re: DHT22 Interface and source

                  I have used similar modules in the past, actually have a product that still uses them.
                  Be careful these are not the standard RS232!
                  They work on 5v serial FTDI I believe.
                  The 9600 baud default rate is way slow.
                  You will need to initialize the blue tooth module
                  Also numerous of these do not meet blue tooth specs.
                  I can’t draw the symbol but they only can use it if the unit passes ALL the blue tooth specifications.

                  The one we still use is from Parallax  rn42  I believe.

                  • 6. Re: DHT22 Interface and source



                    have you fixed the schematic and reposted?

                    • 7. Re: DHT22 Interface and source

                      Hi Jeff,


                      For the moment price is not a factor, but it will come

                      Do you have any code that you're willing to give ?




                      • 8. Re: DHT22 Interface and source

                        there really isn't any special code to use these. we just reset the unit and let it run in its default state.

                        there are timing considerations for reset pulse width and the like but those should be well documented.

                        after that just give it serial data and away it goes.

                        the real work is on the receiver side.. scanning for the device, pairing, etc.

                        here is our start up code. after reset we change the name of the device to  make it easier to find after scanning.

                           pio_wr(5,0);    /* put blue tooth in to reset */
                           pio_wr(5,1);    /* take blue tooth out of reset */

                           putser1(0x24,tport);  // send $$$ to blue tooth
                           putser1(0x24,tport);  // gets into command mode
                        //   putser1(cr,tport);
                        //   putser1(lf,tport);

                          pio_init(11, 1); /* Set P11 input pulled high enables EE ADC  */
                          x = ee_rd(30);        // cust number
                          x =(x << 8);
                          x = (x | ee_rd(31));
                          sprintf(z, "%4d", x);

                          y = ee_rd(32);
                          y =(y << 8);
                          y = (y | ee_rd(33));    // high int of unit number
                          x =(x << 8);
                          x = (x | ee_rd(34));
                          x =(x << 8);
                          x = (x | ee_rd(35));  // low int of unit number
                          b = (long)(y);
                          a = ((b<<16) + x);
                          sprintf(z, "%07ld", a);
                        //  putser1(lf,tport);
                          putser1('R',tport);  //  reboot device
                        //  putser1(lf,tport);
                          putser1(0x2d,tport);  // send --- to blue tooth
                          putser1(0x2d,tport);  // gets out of command mode
                        //  putser1(lf,tport);
                          clean_ser1(tport);   /* flush out all the responses  */


                        really extra simple stuff. just be sure to allow the unit enough time to respond to commands.

                        I have seen prices for serial units range from about $4.00 to $60.00.

                        I would suggest just getting a unit and working with it. actually get 2 units just in case!

                        • 9. Re: DHT22 Interface and source

                          Hi Jeff,


                          Thank you for your code and explaination !

                          Lets give it a try and see.