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
HWMON
Working!

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

Hwmon5

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)
{
   checkLM75();
    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

Summary

  • 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.

 

 

Hwmon3

 

 

Raspberry Pi IoT In C Using Linux Drivers

By Harry Fairhead

Cdrivers360

Buy from Amazon.

Contents

  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

 <ASIN:1871962641>

<ASIN:B08W9V7TP9>

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.

Banner


SnapCode: A Java IDE for the Web
27/02/2024

Thanks to CheerpJ and WebAssembly you can now run a Java IDE inside your browser and local first.This is SnapCode, and while lightweight and in-browser, is to be not underestimated.



Visual Studio 17.9 Now Generally Available
18/03/2024

Visual Studio 17.9 is now fully available with AI assistance and better extensibility. The first preview of 17.10 has also been made available in preview.


More News

raspberry pi books

 

Comments




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

<ASIN:187196265X>

<ASIN:1871962692>

<ASIN:1871962609>



Last Updated ( Monday, 21 June 2021 )