Temperature and humidity sensor on the Orange Pi

Connect the sensor to the Pi

Time for some physical computing on the Orange Pi Plus 2e! We will connect a temperature and humidity sensor to the Orange Pi and write and compile a C code snippet to display the sensor readings. Additionally, you will learn about the GPIO pins.

For this project you will need:

Here is a picture of the sensor with the jumper cables:


So for this idea we will use the GPIO pins. The Orange Pi Plus 2e has 40 pins, located on the side with the double USB and power inputs. The acronym GPIO stands for general purpose input-output. We can use these pins for a variety of physical computing and Internet of Things applications. For example, one can use the GPIO pins to communicate and send power to leds, sensors, motors, and even other micro-controllers, such as the Arduino. The pin layout for the Orange Pi Plus 2e is of the CON3 type, described in more detail here.

The DHT22 has 4 pins coming out of it, out of which only 3 are usable. One is for power in (VCC), another one is for data coming out (DATA) and one is a ground pin (GND).

First of all power off your Orange Pi when connecting the sensors. Using the female to female jumper wires connect the following pins (sensor to Pi):

  • DATA to PIN 7 (GPCLK)
  • GND to PIN 6 (GND)
  • VCC to PIN 1 (VCC-3.3V)

Here is the diagram:


And an actual picture:


For this project you can use the an identical setup to connect the DHT22 sensor to the Raspberry Pi 2 or Raspberry Pi 3, because the pins used in this example are in the same location as on the Orange Pi Plus 2e. Having set up the hardware we need to configure the software part.

Write the C code for the GPIO pins

To read the data sent by the sensor we can write a C program. Python is also an option and it would probably be quicker to write, but from what I gather the quality of the measurements are better in C. In order to do that we need to install a C library that will help us access the GPIO pins. For the Raspberry Pi’s we can get WiringPi. For the Orange Pi the corresponding library is called WiringOP. Install it by running this command in your home folder:

git clone https://github.com/zhaolei/WiringOP.git -b h3

Then, following the instructions on Github, run:

cd WiringOP
chmod +x ./build
sudo ./build

You can confirm the layout of the pins by running this command, that will output the GPIO table layout:

gpio readall

Now you are ready to write and compile a C script that reads data from the sensor. Change back to the home folder and open create a new C file called dht with nano:

nano dht.c

Then, based on this code from UUgear, write in the file:

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define MAX_TIMINGS 85
#define DHT_PIN 7
int data[5] = { 0, 0, 0, 0, 0 };

void read_dht_data()
    uint8_t laststate    = HIGH;
    uint8_t counter        = 0;
    uint8_t j            = 0, i;

    data[0] = data[1] = data[2] = data[3] = data[4] = 0;

    /* pull pin down for 18 milliseconds */
    pinMode( DHT_PIN, OUTPUT );
    digitalWrite( DHT_PIN, LOW );
    delay( 18 );

    /* prepare to read the pin */
    pinMode( DHT_PIN, INPUT );

    /* detect change and read data */
    for ( i = 0; i < MAX_TIMINGS; i++ )
        counter = 0;
        while ( digitalRead( DHT_PIN ) == laststate )
            delayMicroseconds( 1 );
            if ( counter == 255 )
        laststate = digitalRead( DHT_PIN );

        if ( counter == 255 )

        /* ignore first 3 transitions */
        if ( (i >= 4) && (i % 2 == 0) )
            /* shove each bit into the storage bytes */
            data[j / 8] <<= 1;
            if ( counter > 16 )
                data[j / 8] |= 1;

     * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
     * print it out if data is good

    if ( (j >= 40) &&
         (data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )
        float h = (float)((data[0] << 8) + data[1]) / 10;
        if ( h > 100 )
            h = data[0];    // for DHT11
        float c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
        if ( c > 125 )
            c = data[2];    // for DHT11
        if ( data[2] & 0x80 )
            c = -c;
        float f = c * 1.8f + 32;
        printf( "Humidity = %.1f %% Temperature = %.1f *C (%.1f *F)\n", h, c, f );
    else  {
       printf( "Data not good, skip\n" );

int main( void )
    printf( "DHT22 temperature/humidity test\n" );

    if ( wiringPiSetup() == -1 )
        exit( 1 );

    while ( 1 )
        delay( 2000 ); /* wait 2 seconds before next read */

Next, save the code with Ctrl+O, close with Ctrl+X and compile using this command:

cc -Wall -o dht dht.c -lwiringPi

To execute, simply run:

sudo ./dht

Result should be:


And voilà, the script outputs the temperature and humility from the DHT22!

Remote connect to the Orange Pi
Install MySQL on the Orange Pi


  1. Michael Beckwith
    21st January 2017 - 8:21 pm

    If anyone is having issues with a message saying there are undefined references to ptheread_join, pthread_create or pthread_cancel I was able to fix those. Instead of using:

    sudo cc -Wall -o dht dht.c -lwiringPi

    to complie use:

    sudo gcc -pthread -o dht dht.c -lwiringPi. Hope this helps

  2. Hello,
    Thank your C code, I can read all data 🙂 Can we do the same things with python? Is there any sample code?

  3. For those playing at home, the Orange Pi Plus 2e and Orange Pi One have the same pin layout, which is the same as the Raspberry Pi A+, B+, 2 B and 3 B.

    In \”Pin numbering\” the pins used in this project are 1, 4, and GND.
    In physical numbering the pins used in this project are 1, 7, 9.

  4. Would be awesome if someone who more familliar with python make some libary based on https://github.com/duxingkei33/orangepi_PC_gpio_pyH3

    coz its too difficult for beginers just import this lib and rewrite all C code into python.

    Please help us wiht it…

  5. Hi, thanks for your great example.
    I used your dht.h/cpp files as the basis for a C++ application that can be run from crontab to output to a CSV file at regular intervals:

  6. Shouldn’t we use a 10k or 4.7k Ω resistor, as they say here: https://raspberrypi.stackexchange.com/questions/12161/do-i-have-to-connect-a-resistor-to-my-dht22-humidity-sensor

    Or it works fine like this because the wires are short?

  7. For Orange Pi Plus 2 (without E), the layout of the GPIO 40 pin header is orientated differently, so look for the little triangle on the board marking PIN 1

  8. Hi! Would this code compile and run without problems on a H2 chip (Orange Pi Zero)?

    • Hi, I haven’t tested on a H2, just a H3. The GPIO library I used is for H3, so my guess would be the code won’t run with adjustments.

  9. Has anybody experienced receiving 0.0% humidity and 0.0 C temperature? I constantly receive these results, sometimes “Data not good, skip”. What could be the cause?

  10. you have to change the governor of the cpu.
    Change into : ” /etc/default/cpufrequtils” set the min valude from :




    In this way the error : “Data not good, skip ” has gone away

  11. Thank you! Well, I just redefined the #define MAX_TIMINGS 85 to 185 and its fine.

Leave a Reply

Your email address will not be published / Required fields are marked *