Monday, 1 December 2014

Reading analog (ADC) values on BeagleBone black

ADC value on BBB
There are 7 analog inputs on the BeagleBone.

ADC properties:
 - 12 bits (Output values in the range 0-4095)
 - 125ns sample time
 - 0-1.8V range (do not exceed!)
 - 2 uA current flows into the ADC pin in this range
 - If using a voltage divider, the lower leg (the one connected to ground) should be <= 1k Ohm
 - Since we are measuring millivolts, resistors with 0.1% error tolerance should be used in a voltage divider.
 - There is a 1.8V reference voltage VDD_ADC at Port 9 Pin 32.
 - There is a GNDA_ADC that should be grounded on Port9 Pin 34.
NamePin # Pin # Name
32 VDD_ADC
AIN433 34GNDA_ADC
AIN635 36 AIN5 
AIN2 37 38 AIN3 
AIN0 39 40 AIN1 
 The ADC values can be read very easily:

Original BeagleBone, later builds:
    beaglebone:~# cat /sys/devices/platform/omap/tsc/ain1
    1807
Original BeagleBone, older builds: 
    beaglebone:~# cat /sys/devices/platform/tsc/ain1
    1807
(Note that the path "ain1" maps to the pin "AIN0", the path "ain2" maps to the pin "AIN1", etc.)


BeagleBone black:
FIrst enable the ADC ports, then find out where they are and finall read them:

    sh-4.2# echo cape-bone-iio > /sys/devices/bone_capemgr.*/slots
    sh-4.2# find /sys/ -name '*AIN*'
    /sys/devices/ocp.2/helper.14/AIN0
    /sys/devices/ocp.2/helper.14/AIN1
    /sys/devices/ocp.2/helper.14/AIN2
    /sys/devices/ocp.2/helper.14/AIN3
    /sys/devices/ocp.2/helper.14/AIN4
    /sys/devices/ocp.2/helper.14/AIN5
    /sys/devices/ocp.2/helper.14/AIN6
    /sys/devices/ocp.2/helper.14/AIN7
    sh-4.2# cat /sys/devices/ocp.2/helper.14/AIN1                         
    1588







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.
The basics 
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:
cat /sys/devices/ocp.2/helper.11/AIN1
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:
adc_failing
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 : )
Troubleshooting
If your analog inputs do not show up at the above spot, make sure the helper module is loaded:
lsmod
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

No comments:

Post a Comment