MyPi ADC Card Setup
The ADC features double ended inputs with an input scale of 0-2.048V, this card has been primarily designed for the reading of 4-20mA sensors via the on-board 100R 0.1% current shunts.
The ADC in use is the common MCP3424 device for which many example C and Python code samples exist, see link below to an example Git repository with demos and libraries for C and Python.
If you are running the kernel version below or later you can also make use of the precompiled kernel module giving command line access to the ADC
# uname -a Linux raspberrypi 4.1.19+ #853 Wed Mar 9 18:04:48 GMT 2016 armv6l GNU/Linux
To update your kernel/firmware version you should run rpi-update (see link at bottom for more info) following the onscreen prompts
Take note of the "Booting problems" blog entry under "Installing Linux" that details the addition of the mmc overlay that prevents issues discovered when using later kernels with the Compute Module.
To load and configure the kernel module run the following commands as root user, this installs the driver and then tells the system the MCP3424 ADC device is present on the I2C bus at address 0x6E
# modprobe mcp3422 # echo "mcp3424 0x6e" >/sys/bus/i2c/devices/i2c-1/new_device
This command sets up a shortcut in /dev
# ln -s /sys/bus/i2c/devices/1-006e/iio:device0/ /dev/i2cadc
Note : If you have updated the OS via rasp-update (then rebooted) and find the modprobe command returns an error, run the below commands in sequence to ensure all the module dependences have been correctly configured after the update.
# depmod # reboot
This should now appear when the below is run
# ls -l /dev/i2cadc/
total 0 -r--r--r-- 1 root root 4096 Mar 10 12:01 dev -rw-r--r-- 1 root root 4096 Mar 10 12:01 in_voltage0_raw -rw-r--r-- 1 root root 4096 Mar 10 12:01 in_voltage0_scale -rw-r--r-- 1 root root 4096 Mar 10 12:01 in_voltage1_raw -rw-r--r-- 1 root root 4096 Mar 10 12:01 in_voltage1_scale -rw-r--r-- 1 root root 4096 Mar 10 12:01 in_voltage2_raw -rw-r--r-- 1 root root 4096 Mar 10 12:01 in_voltage2_scale -rw-r--r-- 1 root root 4096 Mar 10 12:01 in_voltage3_raw -rw-r--r-- 1 root root 4096 Mar 10 12:01 in_voltage3_scale -rw-r--r-- 1 root root 4096 Mar 10 12:01 in_voltage_sampling_frequency -r--r--r-- 1 root root 4096 Mar 10 12:01 in_voltage_scale_available -r--r--r-- 1 root root 4096 Mar 10 12:01 name -r--r--r-- 1 root root 4096 Mar 10 12:01 sampling_frequency_available lrwxrwxrwx 1 root root 0 Mar 10 12:00 subsystem -> ../../../../../../../bus/iio -rw-r--r-- 1 root root 4096 Mar 10 12:00 uevent
As an example a +1.8V supply has been attached to ADC Channel 0 to then read the ADC channel run the following commands :
$ cat /dev/i2cadc/in_voltage0_raw 1790
$ cat /dev/i2cadc/in_voltage0_scale 0.001000000
So 1790 x 0.001000000 = 1.790V
Changing the sample frequency affects the accuracy of the ADC reading taken, as taking fewer samples leads to a higher resolution conversion, which is especially important when working with low voltage or slowly varying signals. Changing the sample frequency will also alter the available scaling factors.
The first step is checking the available sample frequencies and then echo the chosen one into the input sample frequency controller
# cat /dev/i2cadc/sampling_frequency_available 240 60 15 3
# cat /dev/i2cadc/in_voltage_sampling_frequency 240
# echo 15 >/dev/i2cadc/in_voltage_sampling_frequency
# cat /dev/i2cadc/in_voltage_sampling_frequency 15
# cat /dev/i2cadc/in_voltage0_raw 28635
# cat /dev/i2cadc/in_voltage0_scale 0.000062500
# cat /dev/i2cadc/in_voltage_scale_available 0.000062500 0.000031250 0.000015625 0.000007812
So here the lower sample rate has given way to a more accurate reading, and using the same input voltage as before :
28635 x 0.000062500 = 1.7896875V
Likewise the input scaling multiplier can be altered in a similar fashion
# cat /dev/i2cadc/in_voltage_scale_available 0.001000000 0.000500000 0.000250000 0.000125000
# echo 0.000125000 >/dev/i2cadc/in_voltage0_scale
# cat /dev/i2cadc/in_voltage0_raw 2047
If the scaling is set too high, the amplifier stage is before the converter stage within the device, then the voltage presented to the ADC stage for conversion will be greater than the 2.048V internal reference and as a result you'll just see the max value (FSD) of the ADC i.e. 2047 (2.047V)
Note that you do not need to be root user to read these values, but only the root user can alter the sample or scaling factors