MyPi Integrator Board Combo I/O Card Setup
The MyPi I/O Card is comprised of three types of I/O all of which are isolated from the main card and so useable in an industrial environment.
These are :
4 x Double ended opto-isolated digital inputs, these can be driven from 5V upwards
4 x Digital outputs in the form of N/O Relay contacts rated for 24VDC/1A or 125VAC/0.5A
4 x Double ended ADC inputs, input scale of 0-2.048V, primarily 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
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
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
We have created a setup file called mypi-ioboard.sh which configures the ADC kernel module, GPIO exports for input/output as well as and setting up several /dev shortcuts for all the I/O channels in one go. You can download this from the link at the bottom of this page.
This can be made permanent and executed during the init startup sequence via the below command sequence :
# chmod +x ./mypi-ioboard.sh # mv ./mypi-ioboard.sh /etc/init.d # systemctl enable mypi-ioboard.sh # reboot
This will create the below shortcuts :
# ls -l /dev/dig* lrwxrwxrwx 1 root root 28 Mar 10 12:00 /dev/diginput0 -> /sys/class/gpio/gpio25/value lrwxrwxrwx 1 root root 28 Mar 10 12:00 /dev/diginput1 -> /sys/class/gpio/gpio26/value lrwxrwxrwx 1 root root 28 Mar 10 12:00 /dev/diginput2 -> /sys/class/gpio/gpio27/value lrwxrwxrwx 1 root root 28 Mar 10 12:00 /dev/diginput3 -> /sys/class/gpio/gpio37/value lrwxrwxrwx 1 root root 28 Mar 10 12:00 /dev/digout0 -> /sys/class/gpio/gpio38/value lrwxrwxrwx 1 root root 28 Mar 10 12:00 /dev/digout1 -> /sys/class/gpio/gpio41/value lrwxrwxrwx 1 root root 28 Mar 10 12:00 /dev/digout2 -> /sys/class/gpio/gpio43/value lrwxrwxrwx 1 root root 28 Mar 10 12:00 /dev/digout3 -> /sys/class/gpio/gpio42/value
# 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 - which in comparison to a DVM reading of 1.793 is pretty good.
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
Read/write of I/O is possible as as a non-root user due to the use of the gpio command within the setup file (see GPIO blog section for more details)
To change a relay state :
$ echo 1 >/dev/digout0
$ echo 0 >/dev/digout0
To read a digital input state :
$ cat /dev/diginput0 0
$ cat /dev/diginput0 1
For convenience we have added a +5V_ISO and 0V_ISO to pins 9 & 10 of the digital input connector (roughly 10-20mA capacity) for driving the digital inputs and so can be used with volt-free contacts (e.g. pulse input) or for powering a led pulse detector sensor.