Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Last updated: Friday 13 November 2026 @ 17:29:40
Programming for Engineers · BeagleBone

Continuous ADC Measurements

Explore continuous adc measurements through practical BeagleBone exercises.

ADCIIOBufferingC
Practical Checklist
Chapter checklist 0 / 17 completed

1. Important information

So far we are reading analog voltages in one-shot mode. Most applications need continuous reading of input values. For continuous mode, we need a buffer to capture the input values while the BBB is doing something else.

Important folders in the iio:deviceX directory are:

  • buffer\
    • enable: get and set the state of the buffer
    • length: get and set the length of the buffer.
  1. The buffer can be enabled by doing the following:

    echo 1 > /sys/bus/iio/devices/iio\:device0/scan_elements/in_voltage0_en
    echo 100 > /sys/bus/iio/devices/iio\:device0/buffer/length
    echo 1 > /sys/bus/iio/devices/iio\:device0/buffer/enable
  1. scan_elements directory contains interfaces for elements that will be captured for a single sample set in the buffer.

    ls -al /sys/bus/iio/devices/iio\:device0/scan_elements/
    drwxr-xr-x    2 root     root            0 Jan  1 00:00 .
    drwxr-xr-x    5 root     root            0 Jan  1 00:00 ..
    -rw-r--r--    1 root     root         4096 Jan  1 00:02 in_voltage0_en
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage0_index
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage0_type
    -rw-r--r--    1 root     root         4096 Jan  1 00:02 in_voltage1_en
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage1_index
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage1_type
    -rw-r--r--    1 root     root         4096 Jan  1 00:02 in_voltage2_en
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage2_index
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage2_type
    -rw-r--r--    1 root     root         4096 Jan  1 00:02 in_voltage3_en
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage3_index
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage3_type
    -rw-r--r--    1 root     root         4096 Jan  1 00:02 in_voltage4_en
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage4_index
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage4_type
    -rw-r--r--    1 root     root         4096 Jan  1 00:02 in_voltage5_en
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage5_index
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage5_type
    -rw-r--r--    1 root     root         4096 Jan  1 00:02 in_voltage6_en
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage6_index
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage6_type
    -rw-r--r--    1 root     root         4096 Jan  1 00:02 in_voltage7_en
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage7_index
    -r--r--r--    1 root     root         4096 Jan  1 00:02 in_voltage7_type
  1. Let's see the size of data type in in_voltage1_type:

    cat /sys/bus/iio/devices/iio\:device0/scan_elements/in_voltage1_type
    le:u12/16>>0

    When the ADC sequencer finishes cycling through all the enabled channels, the user can decide if the sequencer should stop (one-shot mode), or loop back and schedule again (continuous mode). If one-shot mode is enabled, then the sequencer will only be scheduled one time (the sequencer HW will automatically disable the StepEnable bit after it is scheduled which will guarantee only one sample is taken per channel). When the user wants to continuously take samples, continuous mode needs to be enabled. One cannot read ADC data from one channel operating in One-shot mode and and other in continuous mode at the same time.

2. Updating bbb_adc.h

You need to update the bbb_adc.h header file located in our ~/bbb_adc/ to take advantage of the continous mode offered by the ADC chip:

  1. Add the following highlighted macros and prototypes:

    #ifndef BBB_ADC_H_
    #define BBB_ADC_H_
    
    #define SYSFS_ADC_DIR "/sys/bus/iio/devices/iio:device0"
    #define SYSFS_ADC_ENABLE_PATH "/sys/bus/iio/devices/iio:device0/buffer/enable"
    #define SYSFS_ADC_LENGTH_PATH "/sys/bus/iio/devices/iio:device0/buffer/length"
    #define SYSFS_ADC_SCAN_DIR "/sys/bus/iio/devices/iio:device0/scan_elements"
    
    #define POLL_TIMEOUT (3 * 1000) /*3 seconds*/
    
    #define MAX_BUF 256
    #define MAX_LEN_BUF 100
    #define ADC_BUF 6
    #define REFVOLTAGE 1.8
    #define MAXADC 4096
    #define MAX_SAMPLE_RATE 2000000 // 200kSPS
    
    int adc_get_value(unsigned int adcPin, unsigned int *value);
    int adc_fd_open(unsigned int gpio);
    int adc_fd_close(unsigned int fd);
    int adc_to_voltage(unsigned int adcValue, float *voltage);
    int adc_get_buffer(unsigned int adcPin, unsigned int *value);
    int enable_channel(unsigned int adcPin);
    int disable_channel(unsigned int adcPin);
    int enable_buffer(void);
    int disable_buffer(void);
    int set_buffer_length(unsigned int len);
    
    #endif /* BBB_ADC_H_*/

3. Updating bbb_adc.c

Now we need to update bbb_adc.c to create the functionality of our new prototypes:

  1. Starting with int enable_channel(...);:

    Add to the bottom of the bbb_adc.c file:

    /***************************************************************
    * enable_channel
    ****************************************************************/
    
    int enable_channel(unsigned int adcPin){
    
        int fd;
        char buf[MAX_BUF];
    
        snprintf(buf, sizeof(buf),SYSFS_ADC_SCAN_DIR "/in_voltage%d_en", adcPin);
    
        fd = open(buf,O_WRONLY);
        if (fd < 0) {
            perror("adc/scan_element/");
            return fd;
        }
    
        // to enable continous mode
        write(fd, "1", 2);
        close(fd);
    
        return 0;
    
    }
  1. Likewise of int disable_channel(...), you only need to modify the write()... as write(fd, "0",2);

    /***************************************************************
    * disable_channel
    ****************************************************************/
    
    int disable_channel(unsigned int adcPin){
    
        int fd;
        char buf[MAX_BUF];
    
        snprintf(buf, sizeof(buf),SYSFS_ADC_SCAN_DIR "/in_voltage%d_en", adcPin);
    
        fd = open(buf,O_WRONLY);
        if (fd < 0) {
            perror("adc/scan_element/");
            return fd;
        }
    
        // to disable continous mode
        write(fd, "0", 2);
        close(fd);
    
        return 0;
    
    }
  1. Next we need to do the same for enable_buffer() and disable_buffer()

    • int enable_buffer(void);

      /***************************************************************
      * enable_buffer
      ****************************************************************/
      
      int enable_buffer(void){
      
          int fd;
          char buf[MAX_BUF];
      
          // to enable continous mode
          snprintf(buf, sizeof(buf),SYSFS_ADC_ENABLE_PATH);
      
          fd = open(buf,O_WRONLY);
          if (fd < 0) {
              perror("adc/buffer/enable");
              return fd;
          }
      
          write(fd, "1", 2);
          close(fd);
      
          return 0;
      }
    • Likewise of int disable_buffer(...), you only need to modify the write()... as write(fd, "0",2);:

      /***************************************************************
      * disable_buffer
      ****************************************************************/
      
      int disable_buffer(void){
      
          int fd;
          char buf[MAX_BUF];
      
          // to enable continous mode
          snprintf(buf, sizeof(buf),SYSFS_ADC_ENABLE_PATH);
      
          fd = open(buf,O_WRONLY);
          if (fd < 0) {
              perror("adc/buffer/enable");
              return fd;
          }
      
          write(fd, "0", 2);
          close(fd);
      
          return 0;
      
      }
  1. Next we need to set the buffer length as to store n values:

    /***************************************************************
    * set_buffer_length
    ****************************************************************/
    
    int set_buffer_length(unsigned int len){
    
        int fd;
        char buf[MAX_BUF];
        char lenBuf[MAX_LEN_BUF];
    
        snprintf(buf, sizeof(buf),SYSFS_ADC_LENGTH_PATH);
    
        fd = open(buf,O_WRONLY);
        if (fd < 0) {
            perror("adc/buffer/length");
            return fd;
        }
    
        snprintf(lenBuf, sizeof(lenBuf),"%d",len);
    
        write(fd, lenBuf, sizeof(lenBuf));
        close(fd);
    
        return 0;
    
    }
  1. Lastly, for the adc continous mode, we need to create the adc_get_buffer(...):

    /****************************************************************
    * adc_get_buffer
    ****************************************************************/
    int adc_get_buffer(unsigned int adcPin, unsigned int *value) {
        int fd;
        char buf[MAX_BUF];
        char retChars[ADC_BUF];
    
        // Path to buffered ADC values
        snprintf(buf, sizeof(buf), SYSFS_ADC_DIR "/in_voltage%d_raw", adcPin);
    
        // Open the buffer file
        fd = open(buf, O_RDONLY | O_NONBLOCK);
        if (fd < 0) {
            perror("adc/get-buffer");
            return fd;
        }
    
        // Read buffer data
        ssize_t bytesRead = read(fd, retChars, sizeof(retChars));
        if (bytesRead < 0) {
            perror("adc/get-buffer/read");
            close(fd);
            return -1;
        }
    
        // Convert the read data to an integer value
        *value = strtol(retChars, NULL, 0);
    
        close(fd);
        return 0;
    }
  1. We also made a prototype to do the voltage conversion for adc, make the int adc_to_voltage(unsigned adcValue, float *voltage){}:

    • Try make this yourself first, implement this equation and make reference to the macros, REFVOLTAGE and MAXADC:

      \[ V_{in} = \frac{adcValue\ \cdot\ V_{ref}}{2^n - 1} \]

      int adc_to_voltage(unsigned int adcValue, float *voltage){
      
          *voltage = ( ( (float)adcValue * REFVOLTAGE ) / (float)MAXADC );
      
          return 0;
      }
  1. Remember to use make to update the libraries:

    make uninstall
    sudo rm -f /usr/include/bbb_adc.h
    sudo rm -f /usr/lib/libbbb_adc.a
    sudo rm -f /usr/lib/libbbb_adc.so
    sudo ldconfig
    make clean
    rm -f bbb_adc.o libbbb_adc.a libbbb_adc.so
    make
    gcc -Wall -Werror -fPIC   -c bbb_adc.c -o bbb_adc.o
    ar rcs libbbb_adc.a bbb_adc.o
    gcc -shared -o libbbb_adc.so bbb_adc.o
    make install
    sudo cp bbb_adc.h /usr/include/
    sudo cp libbbb_adc.a /usr/lib/
    sudo cp libbbb_adc.so /usr/lib/
    sudo ldconfig

4. Make a programme to utilise the continous capibilities

  1. Create a directory whether you are storing your implementations, for me I use ~/examples_C/adc_continuous_read

  1. Once in the directory you can create a file called adc_continuous_read.c

  1. We are going to modify the file to take user input, the pin number and the size of the buffer:

    #include <bbb_adc.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[]) {
        if (argc != 3) {
            fprintf(stderr, "Usage: %s <adcPin> <bufferLength>\n", argv[0]);
            return EXIT_FAILURE;
        }
    
        unsigned int adcPin = atoi(argv[1]);
        unsigned int bufferLength = atoi(argv[2]);
        unsigned int adcValue;
        float voltage;
    
        // Enable buffer and set buffer length
        if (enable_buffer() < 0) {
            fprintf(stderr, "Error enabling buffer.\n");
            return EXIT_FAILURE;
        }
    
        if (set_buffer_length(bufferLength) < 0) {
            fprintf(stderr, "Error setting buffer length.\n");
            disable_buffer();
            return EXIT_FAILURE;
        }
    
        // Enable ADC channel
        if (enable_channel(adcPin) < 0) {
            fprintf(stderr, "Error enabling ADC channel %u.\n", adcPin);
            disable_buffer();
            return EXIT_FAILURE;
        }
    
        printf("Starting continuous ADC read on pin %u...\n", adcPin);
    
        // Continuous read loop
        for (int i = 0; i < bufferLength; i++) {
            if (adc_get_buffer(adcPin, &adcValue) == 0) {
                adc_to_voltage(adcValue, &voltage);
                printf("ADC Value: %u, Voltage: %.3f V\n", adcValue, voltage);
            } else {
                fprintf(stderr, "Error reading buffer for ADC pin %u.\n", adcPin);
                break;
            }
    
            //usleep(500000); // Delay between reads (adjust as needed, e.g., 500 ms)
        }
    
        // Cleanup: disable buffer and channel
        disable_channel(adcPin);
        disable_buffer();
    
        printf("Continuous ADC read completed.\n");
    
        return EXIT_SUCCESS;
    }

    Explanation of code

    Main Function

    • The main function begins by checking the arguments. It requires two command-line arguments:

      • adcPin: The ADC pin number.

      • bufferLength: Number of samples to read in continuous mode.

        int main(int argc, char *argv[]) {
            if (argc != 3) {
                fprintf(stderr, "Usage: %s <adcPin> <bufferLength>\n", argv[0]);
                return EXIT_FAILURE;
            }
      • If the correct arguments aren't provided, it outputs an error and exits.

    • Setting Up the ADC

      • The program enables the buffer with enable_buffer() and sets the buffer length with set_buffer_length(bufferLength). It then enables the specified ADC channel using enable_channel(adcPin).

        if (enable_buffer() < 0) {
            fprintf(stderr, "Error enabling buffer.\n");
            return EXIT_FAILURE;
        }
        
        if (set_buffer_length(bufferLength) < 0) {
            fprintf(stderr, "Error setting buffer length.\n");
            disable_buffer();
            return EXIT_FAILURE;
        }
        
        if (enable_channel(adcPin) < 0) {
            fprintf(stderr, "Error enabling ADC channel %u.\n", adcPin);
            disable_buffer();
            return EXIT_FAILURE;
        }
    • Continuous Read Loop

      • The program enters a loop that iterates bufferLength times, reading ADC values using adc_get_buffer(adcPin, &adcValue) and converting each to voltage with adc_to_voltage(adcValue, &voltage). Each value is printed with both its ADC and voltage readings.

        for (int i = 0; i < bufferLength; i++) {
            if (adc_get_buffer(adcPin, &adcValue) == 0) {
                adc_to_voltage(adcValue, &voltage);
                printf("ADC Value: %u, Voltage: %.3f V\n", adcValue, voltage);
            } else {
                fprintf(stderr, "Error reading buffer for ADC pin %u.\n", adcPin);
                break;
            }
        }

        Note:

        The usleep function is commented out here. In real applications, uncomment and adjust it to control the sample rate, if necessary.

    • Cleanup and Exit

      • After reading, the program disables the ADC channel and buffer, and then exits.

        disable_channel(adcPin);
        disable_buffer();
        
        printf("Continuous ADC read completed.\n");
        
        return EXIT_SUCCESS;
  1. Create a Makefile and replicate the code below:

    # Compiler and flags
    CC = gcc
    CFLAGS = -Wall -Werror
    
    # Target executable name
    TARGET = adc_continuous_read
    
    # Source files
    SRC = adc_continuous_read.c
    
    # Library to link against
    LIBS = -lbbb_adc
    
    # Default target: build the executable
    all: $(TARGET)
    
    # Build the executable
    $(TARGET): $(SRC)
        $(CC) $(CFLAGS) $(SRC) $(LIBS) -o $(TARGET)
    
    # Clean up build artifacts
    clean:
        rm -f $(TARGET)
    
    # Phony targets to avoid conflicts with files of the same name
    .PHONY: all clean
  1. If you are not wired up from ADCLibrary, here is the image for reference:

  1. Now use make and compile the code and run: