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

HiPi::Interface::HobbyTronicsADC

This module provides a high level interface for the HobbyTronics I2C Analog To Digital Converter.

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

The HobbyTronics ADC uses clock stretching which doesn't work correctly with the Raspberry Pi. You can try running the entire I2C bus at a lower speed by setting i2c_baudrate in /boot/config.txt ( a value of 32000 seems to work ) or you might find using the bit banging gpio driver activated by the i2c-gpio overlay works for this ic.

Methods

Create a new instance of the class. Optional key value pairs and their defaults are:

my $adc = HiPi::Interface::HobbyTronicsADC->new(
    address     => 0x28,
    devicename  => '/dev/i2c-1',
    res         => 1,
    fil1        => 0,
    fil0        => 0
);

address

I2C address of the ADC.

devicename

I2C device

res, fil0, fil1

when :  res   == 1 - device has  8 bit resolution
        res   == 0 - device has 10 bit resolution
        fil0  == 1 - use complimentary feature
        
        fil1 currently unused according to datasheet

A convenience method that allows retrieving the value for a single channel

my $val = $adc->read_channel( $channel );

A convenience method that allows retrieving the values for a number of specified channels. The return @vals will contain a number of values equal to the number of @channels requested in the order requested.

my @vals = $adc->read_channels(0,5,7);
print $vals[0] . ' is value for channel 0' .qq(\n);
print $vals[1] . ' is value for channel 5' .qq(\n);
print $vals[2] . ' is value for channel 7' .qq(\n);

Reset all the option flags used in the constructor to the values passed

$adc->set_option_flags( $res, $fil0, $fil1 );