How to Use ADC on TorizonCore
This article will demonstrate how to access the Analog-to-Digital Converter (ADC) through the
sysfs interface in a container application in TorizonCore.
Feel free to read about how ADC is handled in the Linux environment at ADC on Linux article, and about the Linux Industrial I/O Subsystem (IIO), you can find more information at Linux Industrial I/O Subsystem page from Analog Devices.
This article complies to the Typographic Conventions for Torizon Documentation.
- Have completed the Torizon Quickstart Guide lessons for Torizon.
- (optional) Have installed and configured the Visual Studio Code Extension for Torizon.
Toradex has provided a sample code written in C at our GitHub repository for the ADC Sample with TorizonCore.
It demonstrates the use of iio_utils.c and iio_utils.h from the Linux Kernel tool for the iio interface.
To evaluate the sample application, you can clone the Torizon Samples repository on your PC:
$ git clone https://github.com/toradex/torizon-samples.git
The sample code will get data from input channel-1. If required, the input channel number can be changed in
samples/adc/adc/adc.c modifying the variable
Once you have cloned the Torizon Samples repository, go to the
torizon-samples/adc directory inside samples and execute the following command to build an image. Choose your module from the tab below:
$ docker build . --build-arg CROSS_TC_IMAGE_ARCH=arm64 --build-arg ARCH_ARG=linux/arm64 --build-arg GCC_PREFIX=aarch64-linux-gnu -t adc-sample
$ docker build . -t adc-sample
Both commands build the Dockerfile present in the sample project.
The build of our sample container for ADC demonstration is a two-stage build process, in which the first image is based on debian-cross-toolchain-$CROSS_TC_IMAGE_ARCH to cross-compile the sample application on the host machine, i.e. Desktop PC. In the second stage, the resulting binary is copied from the first stage image to the
/usr/local/bin of the final image (second stage). This will produce the final application container in a small and deployable image with the tag adc-sample. Now this container image can be deployed on the target machine.
You can save and deploy the container image in a portable tar archive file executing the Docker save and load commands, as exemplified below:
$ docker save -o adc-image.tar adc-sample
Now you can copy it to the target machine:
$ scp adc-image.tar torizon@X.X.X.X:/home/torizon/
There are other ways to deploy container images to the target TorizonCore device. Please check more information at Deploying Container Images to TorizonCore.
Running the Sample
In the target device we can use docker to load the image directly from its tar file, by using the following command:
# docker load -i adc-image.tar
After this, the image can be executed by using the following docker command:
# docker run -it --rm adc-sample
In case where settings of ADC is needed to be set, like for continuous conversions mode,
/sys can be mounted for docker:
# docker run -it --rm -v /sys:/sys adc-sample
As a result, it shows raw input channel value and also converts it to a voltage by using the formula:
VIN = VREAD * Scale
Access Using Command-Line
All the Analog-to-Digital Converter (ADC) outputs (raw data) can be listed in the
/dev directory as shown in the example below. For Verdin and apalis modules,
you can list the ADC outputs using
/dev/<family>-adc*. For Colibri family, you can list them by using
# ls -la /dev/apalis-adc*
lrwxrwxrwx 1 root root 75 Jul 18 14:40 /dev/apalis-adc0 -> /sys/devices/platform/bus@5a000000/5a880000.adc/iio:device0/in_voltage0_raw
lrwxrwxrwx 1 root root 75 Jul 18 14:40 /dev/apalis-adc1 -> /sys/devices/platform/bus@5a000000/5a880000.adc/iio:device0/in_voltage1_raw
lrwxrwxrwx 1 root root 75 Jul 18 14:40 /dev/apalis-adc2 -> /sys/devices/platform/bus@5a000000/5a880000.adc/iio:device0/in_voltage2_raw
lrwxrwxrwx 1 root root 75 Jul 18 14:40 /dev/apalis-adc3 -> /sys/devices/platform/bus@5a000000/5a880000.adc/iio:device0/in_voltage3_raw
As you can see, it displays the available module ADCs and the corresponding names used by the BSP. Those corresponding names are important because the Linux kernel logs will print the names used by BSP.
For more general information check the ADC (Linux) article.
As you could observe before, all the standard names lead to a path into
/sys directory, which means that all the Analog-to-Digital Converter (ADC) information can also be read and set using the
iio interface exposed through
Check the sequence of commands below, showing a listing, a reading of an analog voltage value, and the voltage scale.
# cd /sys/bus/iio/devices/iio\:device1
# ls -la in*
-rw-r--r-- 1 root root 4096 Dec 4 02:14 in_voltage-voltage_scale
-rw-r--r-- 1 root root 4096 Dec 4 02:14 in_voltage0-voltage1_raw
-rw-r--r-- 1 root root 4096 Dec 3 22:23 in_voltage0_raw
-rw-r--r-- 1 root root 4096 Dec 4 02:14 in_voltage1-voltage0_raw
-rw-r--r-- 1 root root 4096 Dec 4 02:14 in_voltage1_raw
-rw-r--r-- 1 root root 4096 Dec 4 02:14 in_voltage2-voltage3_raw
-rw-r--r-- 1 root root 4096 Dec 4 02:14 in_voltage2_raw
-rw-r--r-- 1 root root 4096 Dec 4 02:14 in_voltage3-voltage2_raw
-rw-r--r-- 1 root root 4096 Dec 4 02:14 in_voltage3_raw
-rw-r--r-- 1 root root 4096 Dec 3 22:23 in_voltage_scale
# cat in_voltage3_raw
# cat in_voltage_scale
You can also perform a reading of an one shot analog voltage value by using the symlinks exposed through /dev.
# cat /dev/apalis-adc0
The new default ADC interface names exposed through
/dev are limiting, allowing access to raw data only. Therefore, for more specific cases and configurations, use the
iio interface exposed through
The sample application we provided is using C language, but you can easily create a Python or .Net application to do the same.
ADC channels availability on Toradex modules and its usage are described in ADC-Linux article.
Developing a C Application for ADC reading in Visual Studio Code
You can easily develop the same ADC application we did in the sample using Visual Studio Code with Torizon Extension.
For that, please make sure to have followed the Torizon Quickstart Guide lessons and to have Configured the Build Environment for Torizon Containers.
In the Visual Studio Code with the Torizon Extension, please follow the steps below:
- Press F1, and choose option Torizon/C-C++: Create C/C++ application
- Write the application name (in our case, we are going to call it
- Choose the Project Directory
- Select a template for the project, in this example, Makefile-based Project
- Select the target platform for it, which can be debian arm32v7 bullseye or debian arm64v8 bullseye, depending on your target's architecture.
- Write torizon as the username for the application
- Select debug as configuration.
And that's it for the creation of our project. The Torizon Extension may take a while after the setup of the project, so be patient.
You can check these steps in detail in our article about programming and debugging a sample C++ application with Visual Studio Code and Torizon Extension.
When the environment is ready, the next step is to "transplant" the sources from the ADC sample. Please, make the following:
- Copy iio_utils.c file to the project folder.
- Copy iio_utils.h file to the project folder.
- Make the content of adc.c file to the original main C application the project has created.
- Adjust the Makefile so it is as the Makefile in the sample project from Torizon Samples.
See how our project will look like after these changes:
After that, press F5 to start the Debug session of the Torizon Extension. It will build and deploy a debug container of the application in the target, and once started the debugger tools, you'll be able to see and monitor the application execution in the Visual Studio Code at your machine. It's also possible to add breakpoints, watch variables content, among other things.
See below a screenshot of the debug session of our sample application: