I’m quite new to this, and it was a struggle for me to do a simple thing like read a channel of the A/D subsystem on my Beaglebone. Many of the guides on the web seem not to work under Debian Image 2014-05-14. So in the hope of helping someone else, here is my hard-won cookbook for Debian users.
Following is a step-by-step guide to reading the A/D channels on my Beaglebone (white or black) running Debian Image 2014-05-14.
STEP ONE
Change uEnv.txt to look like this:
##Example
#cape_disable=capemgr.disable_partno=
cape_enable=capemgr.enable_partno=BB-ADC
(the first two lines above will already be in the file. You add the last line “cape_enable=capemgr.enable_partno=BB-ADC” )
This will cause BB-ADC device tree to be installed each time you boot your board.
STEP TWO
With this done, reboot the board. I boot to “root@beaglebone:~#” so it will appear below.
Verify that you can now read A/D channel 0 by executing the following from the command line on your Beaglebone:
root@beaglebone:~# cat /sys/bus/iio/devices/iio:device0/in_voltage0_raw
You should see a number something like 3927. If so, you have access to the A/D subsystem and you can try things like
root@beaglebone:~# cat /sys/bus/iio/devices/iio:device0/in_voltage7_raw
to read channel 7, and so on.
STEP THREE
If you’re interested in C/C++, the following short program should read all eight A/D channels and display their value:
#include
#include
#include <fcntl.h> //define O_WRONLY and O_RDONLY
using namespace std;
int main()
{
int fd ; //for file handle
char ch[5]; //for the A/D value when read
//The next three strings are used to construct a path to the A/D channels
char bufbase[64] = “/sys/bus/iio/devices/iio:device0/in_voltage”; //path prefix
char chnlnumber[2]; //channel to be read, 0,1,…,7
char bufend[] = “_raw”; //path suffix
char buf[64] = “”; //string to hold full path to A/D channel
int i; //loop index
chnlnumber[1] = 0; //end of string
for (i=0;i<8;i++)
{ chnlnumber[0] = 48+i; //string containing channel number as a character
strcat(strcat(strcat(buf,bufbase),chnlnumber),bufend); //construct the full path
cout << “Cnl” <<chnlnumber<< " " ; //display the channel number being read
fd = open(buf, O_RDONLY); //access the A/D channel as a file
read(fd,ch,4); // "
cout << ch << " " << endl ; //display the current raw value from the channel
close(fd);
strcpy(buf,""); //get ready to do next channel access
usleep(10000); //wait for 10ms – not really needed
}
}
(I’m also new to C++, being more of a assembler guy myself, so this code may have many shortcomings. Advice on improvements
welcome!)