HiPi
Perl Modules for Raspberry Pi
Version 0.92 - released 28 March 2024

HiPi::Interface::MCP3ADC

This module provides an interface to the the MCP 3004, 3008, 3204 and 3208 SPI analog to digital converters

It uses HiPi::Device::SPI as a backend.

Methods

Create a new instance of the module class.

use HiPi qw( :mcp3adc );
use HiPi::Interface::MCP3ADC;
my $adc = HiPi::Interface::MCP3ADC->new(
    devicename   => '/dev/spidev0.0',
    ic           => MCP3008,
);

devicename gives the SPI device and CS number that the IC is connected to.

ic gives the chip family type. It can be MCP3004, MCP3008, MCP3204 or MCP3208

Retrieve the value from the specified channel as a single channel read.

Channel can be 0 to 3 for MCP3004 and MCP3204, 0 to 7 for MCP3008 and MCP3208

This method is a convenience wrapper arround the read() method, where for example:
$adc->single_read( 3 ); is equivalent to $adc->read( MCP3ADC_CHAN_3 );

my $value = $adc->single_read( 3 );

Retrieve the value from the indicated channels as a differential channel read.

Channel can be 0 to 3 for MCP3004 and MCP3204, 0 to 7 for MCP3008 and MCP3208

This method is a convenience wrapper arround the read() method, where for example:

$adc->diff_read( 0 ); is equivalent to $adc->read( MCP3ADC_DIFF_0_1 );
$adc->diff_read( 1 ); is equivalent to $adc->read( MCP3ADC_DIFF_1_0 );
$adc->diff_read( 2 ); is equivalent to $adc->read( MCP3ADC_DIFF_2_3 );
$adc->diff_read( 3 ); is equivalent to $adc->read( MCP3ADC_DIFF_3_2 );
$adc->diff_read( 4 ); is equivalent to $adc->read( MCP3ADC_DIFF_4_5 );
$adc->diff_read( 5 ); is equivalent to $adc->read( MCP3ADC_DIFF_5_4 );
$adc->diff_read( 6 ); is equivalent to $adc->read( MCP3ADC_DIFF_6_7 );
$adc->diff_read( 7 ); is equivalent to $adc->read( MCP3ADC_DIFF_7_6 );

my $value = $adc->diff_read( 3 );

The 3004 and 3008 ics have 10 bit resolution ( return 0 to 1023 ) while the 3204 and 3208 ics have 12 bit resolution ( return 0 to 4095 ).

This method provides a wrapper around the read() method for single channel reading, converting the value returned to a percentage of its possible maximum.

my $percent = $adc->percent_read( 3 );

$mode is the seven bit mask described as the configure bits in the MCP3004/3008/3204/3208 datasheets that determines the channel and read mode for the returned value.

For convenience HiPi exports the following constants that you can pass as a value for $mode

MCP3ADC_CHAN_0    => 0b00001000,  # single-ended CH0
MCP3ADC_CHAN_1    => 0b00001001,  # single-ended CH1
MCP3ADC_CHAN_2    => 0b00001010,  # single-ended CH2
MCP3ADC_CHAN_3    => 0b00001011,  # single-ended CH3
MCP3ADC_CHAN_4    => 0b00001100,  # single-ended CH4
MCP3ADC_CHAN_5    => 0b00001101,  # single-ended CH5
MCP3ADC_CHAN_6    => 0b00001110,  # single-ended CH6
MCP3ADC_CHAN_7    => 0b00001111,  # single-ended CH7
MCP3ADC_DIFF_0_1  => 0b00000000,  # differential +CH0 -CH1
MCP3ADC_DIFF_1_0  => 0b00000001,  # differential -CH0 +CH1
MCP3ADC_DIFF_2_3  => 0b00000010,  # differential +CH2 -CH3
MCP3ADC_DIFF_3_2  => 0b00000011,  # differential -CH2 +CH3
MCP3ADC_DIFF_4_5  => 0b00000100,  # differential +CH4 -CH5
MCP3ADC_DIFF_5_4  => 0b00000101,  # differential -CH4 +CH5
MCP3ADC_DIFF_6_7  => 0b00000110,  # differential +CH6 -CH7
MCP3ADC_DIFF_7_6  => 0b00000111,  # differential -CH6 +CH7
use HiPi qw( :mpc3adc );
$value = $adc->read( MCP3ADC_CHAN_3 )

In most cases it will be simpler to use the single_read, diff_read and percent_read methods.