Update July 17, 2013: In the latest kernel (3.8.13-00687 and onward) there is a new way of enabling and using the ADC driver. The bone-iio-helper driver is no longer used, instead the channels are exported to folder /sys/bus/iio/devices/iio:device0/. The bugs should be straightened as well, thanks toZubair: http://beagleboard-gsoc13.blogspot.no/2013/07/continuous-sampling-mode-for-adc-driver.html.
At you own risk: A pre-compiled kernel is available from http://distros.replicape.com. Follow the instructions here: http://wiki.replicape.com/index.php?title=Replicape_rev_A3_setup, but instead of writing just “make” write “make kernel”, and it will install the kernel 3.8.13-00687. This will overwrite your current kernel and kernel modules which may bork it: http://hipstercircuits.com/unbrick-beaglebone-black-without-erasing-emmc/
The Analog to Digital Converter (ADC) on the BeagleBone is both a Touch Screen Controller (TSC) and a general purpose ADC. Depending on how many of the lines are reserved for the TSC (4, 5, or 8), the remaining lines can be used for reading analog values. One of the lines is connected to the power Management Integrated Circuit (PMIC, TPS65217) of the white Bone, in order to sense the power used by the board. This does not appear to be the case for the new BeagleBone Black.
To read the analog inputs on your BeagleBone Black, first enable the driver:
echo cape-bone-iio > /sys/devices/bone_capemgr.*/slots
The analog inputs should now be activated and exported to the filesystem. To read their values, just cat the files:
This will read the analog value of AIN1 (The pin is P9_40). What you see is a voltage between 0 and 1.8V, but in millivolts (0 to 1799). If you tie the pin high by shorting the pin to the analog VDD (P9_32), you will see a 1799. If you tie the pin low to (P9_34, Analog ground), you will see a 0. There is a bug in the driver, so the value you read is actually the previous value you read. For now, just read the value twice. More on that later.
Bug in the ADC driver
Note: This was tested on kernel 3.8.6, the one that came with the early BeagleBone Black.
The driver for the ADC was riddled with errors early on. This manifested itself as getting weird values from the ADC when reading from one or several pins in rapid succession:
Clearly, a negative value is not right. The reason why this occurs is that there is no test to make sure the ADC is finished with sampling the pin. Furthermore, when one pin is read, the reading of this pin triggers the ADC to sample a new value (for all the pins) for the next reading. For further reading, consult the Linux mainline source: ti_am335x_adc.c. This will probably be fixed in later veriosn of the kernel, but if you need a solution now, have a look at these adc-patches and see if you can’t figure it out : )
If your analog inputs do not show up at the above spot, make sure the helper module is loaded:
This shows a list of loaded kernel modules, make sure “bone_iio_helper” shows up in the list.
If the helper modules is loaded, but the fiels are not in the right place, try searching for them:
find /sys -name *AIN*
This should show you a list of the analog input files. If not, go to forum and rant. Make sure you include the kernel version you are using:
root@beaglebone:~# uname -a Linux beaglebone 3.8.6 #1 SMP Sat Apr 13 09:10:52 CEST 2013 armv7l GNU/Linux