Pi IoT In C Using Linux Drivers - Hwmon
Written by Harry Fairhead   
Monday, 21 June 2021
Article Index
Pi IoT In C Using Linux Drivers - Hwmon

Once the drivers are loaded you will discover that there is a new directory in hwmon:


You can see that temp1_input is going to be the file to read to get the current temperature:

int main(int argc, char **argv)
    int fd = open("/sys/class/hwmon/hwmon2/temp1_input",
O_RDONLY); char buf[100] = {0}; read(fd, buf, 100); printf("%s\n\r", buf); float temp; sscanf(buf, "%f", &temp); temp = temp / 1000; printf("%f\n\r", temp); close(fd); }

The temperature is returned as a string in millidegree Celsius.

If you want to set the critical temperature and hysteresis you can use:

    fd = open("/sys/class/hwmon/hwmon2/temp1_max",
O_RDWR); char max[] = "20000"; write(fd, max, 5); close(fd); fd = open("/sys/class/hwmon/hwmon2/temp1_max_hyst",
O_RDWR); char min[] = "190000"; write(fd, max, 5); close(fd);

This sets the critical temperature to 20°C and the hysteresis to 19°C. After this, if the temperature goes above 20°C the LED on the board will come on and stay on until the temperature drops below 19°C. Notice that you need to run this program with root permissions as it is writing to files in /sys.

In chapter but not in this extract:

  • Industrial I/O
  • An Example - The HTU21
  • The IIO Utilities
  • The Libiio Library


  • The IIO and Hwmon systems are attempt to create drivers that present a standard interface, irrespective of the way that the devices are actually interfaced to the machine.

  • Hwmon is the older system and was originally intended only for devices that are built into the system rather than discrete devices connected via external buses.

  • The LM75 Temperature Sensor is often found built in to monitor hardware operating conditions, but it can also be connected via I2C and interfaced using the hwmon driver.

  • Industrial I/O (IIO) was invented as an extension of Hwmon to create something that could support a wide range of sensors.

  • IIO has many sophisticated features including triggers that can be used to make regular measurements in kernel space – most drivers don’t support this.

  • The HTU21 introduced in Chapter 12 has an IIO driver.

  • There are a set of IIO utilities that you can install but again due to limited driver support many features don’t work.

  • There is also a more sophisticated library, Libiio, which makes IIO devices easier to work with, but lack of driver support makes it less attractive than working directly with devices.






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.


IBM Launches The Granite Code LLM Series

IBM is introducing decoder-only code models for code generative tasks as part of its Granite collection. The models have been trained with code written in 116 programming languages and range in size f [ ... ]

Microsoft Reveals Preview C#13 Features

Microsoft has announced details of what will be included in C# 13. The news was announced at Microsoft's recent Build conference. The new version will have enhanced parameters, extension types, and se [ ... ]

More News

C book



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




Last Updated ( Monday, 21 June 2021 )