Select the version of your OS from the tabs below. If you don't know the version you are using, run the command
cat /etc/os-release or
cat /etc/issue on the board.
Remember that you can always refer to the Torizon Documentation, there you can find a lot of relevant articles that might help you in the application development.
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.
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 $ cd adc/
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 adc directory inside samples and execute the following command to build an image for
$ docker build . -t adc-sample
To build an image for
arm64v8 you can use the following command:
$ 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
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.
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
All the Analog-to-Digital Converter (ADC) information can 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 821 # cat in_voltage_scale 2.000000000
As you could observe, knowing the path of the device and its channel you can simply use file manipulation to read (or write) the data you want.
The sample application we provided is using C language, but you can easily create a Python or .Net application to do the same.
Warning: ADC channels availability on Toradex modules and its usage are described in ADC-Linux article.
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:
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:
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: