Pi IoT In C Using Linux Drivers -The DHT22
Written by Harry Fairhead   
Monday, 15 February 2021
Article Index
Pi IoT In C Using Linux Drivers -The DHT22
Using The Driver
The Program

A function to check for the dht11 overlay and only load it if it isn’t already loaded is easy enough with just some string handling and the use of the popen function:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
FILE *doCommand(char *cmd)
    FILE *fp = popen(cmd, "r");
    if (fp == NULL)
        printf("Failed to run command %s \n\r", cmd);
    return fp;
void checkDht11()
    FILE *fd = doCommand("sudo  dtparam -l");
    char output[1024];
    int txfound = 0;
    char indicator[] = "dht11  gpiopin=4";
    char command[] = "sudo dtoverlay dht11 gpiopin=4";
    while (fgets(output, sizeof(output), fd) != NULL)
        printf("%s\n\r", output);
        if (strstr(output, indicator) != NULL)
            txfound = 1;
    if (txfound == 0)
        fd = doCommand(command);
int main(int argc, char **argv)
    char buffer[25];
    int fd = open(
      "/sys/bus/iio/devices/iio:device0/name", O_RDONLY);
    read(fd, buffer, 25);
    printf("%s", buffer);
    fd = open(
O_RDONLY); read(fd, buffer, 25); close(fd); int temp; sscanf(buffer, "%d", &temp); printf("%f\n\r", temp / 1000.0); fd = open("/sys/bus/iio/devices/iio:device0/ in_humidityrelative_input", O_RDONLY); read(fd, buffer, 25); close(fd); int hum; sscanf(buffer, "%d", &hum); printf("%f\n\r", hum / 1000.0); }

The indicator string is used to specify the string that is in the overlay listing when the driver is loaded and this isn’t necessarily the same as the string in the command string, which is the command to install the driver with any parameters that are needed.

You can run this program without having to change the config.txt file. The driver remains loaded until the next reboot and is reinstalled when you next run the program. Notice that the program has to have root permissions to be able to run sudo, even if the program isn’t being run as root.


  • The Device Tree is the modern way to specify the hardware configuration of a machine and to load and configure drivers.

  • The boot disk contains device trees for each of the different versions of the Pi and the loader selects the appropriate device tree for the Pi model that is booting.

  • The loader merges the device tree with the overlays listed in the config.txt file to produce a customized final device tree used to boot the system.

  • Overlays are fragments of the device tree which can be used to modify and add to the basic device tree.

  • Overlays can themselves be customized by specifying parameters.

  • When the loader parses the DT and applies all the overlays you have specified, the final DT is represented by the folders in /proc/overlay.

  • The DHT22 Humidity and Temperature Sensor provides an example of loading and customizing a driver.

  • A fairly recent innovation is the ability to load and customize drivers after the system has booted using the dtoverlay and dtparam commands.

  • You can easily write a program to load and configure missing drivers at runtime.

  • Dynamic loading of overlays doesn’t always work and unloading overlays, while possible, isn’t a good idea.


Raspberry Pi IoT In C Using Linux Drivers

By Harry Fairhead


Buy from Amazon.


  1.  Choosing A Pi For IoT

  2. C and Visual Studio Code

  3.  Drivers: A First Program

  4.  The GPIO Character Driver
         Extract: GPIO Character Driver

  5. GPIO Using I/O Control

  6.  GPIO Events

  7.  The Device Tree
        Extract: The DHT22

  8.  Some Electronics

  9.  Pulse Width Modulation
    Extract:  The PWM Driver 

  10. SPI Devices
    Extract: The SPI Driver 

  11. I2C Basics

  12. The I2C Linux Driver ***NEW!


  13. Advanced I2C

  14. Sensor Drivers – Linux IIO & Hwmon
      Extract: Hwmon  

  15. 1-Wire Bus
      Extract: 1-Wire And The DS18B20 

  16. Going Further With Drivers

  17. Appendix I



To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.


Is Rust Safe?

Rust is our great hope for the future. Even if you are not using it you probably have heard of it and believe it is a safer language than C or C++. But is it really?

htmx 2 Released - The Next Big Thing?

htmx seems to have crept up on us programmers - perhaps because we are programmers. Is this the next big thing and is it a JavaScript killer?

More News

C book



or email your comment to: comments@i-programmer.info





Last Updated ( Tuesday, 16 February 2021 )