PINE64::MCP3208 - Perl interface to the MCP3208 12-bit SPI analog to digital converters.


        use PINE64::MCP3208;

        my $adc = PINE64::MCP3208->new(10,12,11,13); 
        #5 bits because the first is the start bit
        my @ch0 = (1,1,0,0,0);

        for(my $s=0;$s<200;$s++){
                my ($reading, $binval, $voltage ) = $adc->read3208(\@ch0, 50, 5.01);
                $voltage = sprintf("%.3f", $voltage);
                print "binval: $binval\tvoltage: $voltage vdc\n";
        }#end for


This module allows you to control an MCP3004 or MCP3008 10-bit analog to digital controller via bit-banged SPI using Perl on the PINE64A+ single board computer. Works in single channel or differential mode.



Takes clock pin number, SPI data in pin number, SPI data out pin number, SPI chip select pin number, and returns a PINE64::MAX3208 object. Pin numbers are valid PINE64::GPIO objects on the Pi-2 bus.


This is the main function of the package. It takes an array reference to select the channel number and mode (single/differential) that you want to sample

The following are valid single channel array values: @ch0 = (1,1,0,0,0); @ch1 = (1,1,0,0,1); @ch2 = (1,1,0,1,0); @ch3 = (1,1,0,1,1); @ch4 = (1,1,1,0,0); @ch5 = (1,1,1,0,1); @ch6 = (1,1,1,1,0); @ch7 = (1,1,1,1,1);

The following are valid differential channel array values @ch0_diff = (1,0,0,0,0); @ch1_diff = (1,0,0,0,1); @ch2_diff = (1,0,0,1,0); @ch3_diff = (1,0,0,1,1);

So, when calling read3208, you would pass a reference of the channel you want to sample

$adc->read3208(\@ch0, 50, $vref);

The second argument is the delay between clock pulses in milliseconds. This is a crude way to control the sample speed. I almost always use 50msec, and have had good results.

The third argument is a reference voltage and is optional. This method will calculate the returned voltage value based on the output code of the converter in proportion of the reference voltage. So if the reference voltage is 50V, and the output code is 2048, the method will return 25V.

The reference voltage can be useful if you are using a voltage divider circuit to step-down a voltage higher that the maximum input reference voltage of the MCP3208.

read3208() returns an array that contains an array reference of the 10-bit binary result i.e. 101101001101, the output code in decimal i.e. 2024, 567, 311, etc., and the calculated voltage based on the supplied reference voltage.

Below is an example call to the read3208() method: my ($reading, $binval, $voltage ) = $adc->read3208(\@ch0, 50, 5.01);