Talking to LSM303DLHC Accelerometer and Compass using perl and Device::LSM303DLHC

I bought a pololu’s AltIMU inertial measurement unit that includes among other things a onboard Accelerometer and Magnetometer in the form of the LSM303DLHC chipset. I decided to write down the steps I take to get it working with my Beaglebone Black and Raspberry Pi boards using perl, to help those who are trying to achieve the same. First follow the instructions to install Device::SMBus on Beaglebone Black if you have a Beaglebone Black, or instructions to install Device::SMBus on a raspberry Pi if you are using a Raspberry Pi board Then install Device::LSM303DLHC from cpan sudo apt-get install Device::LSM303DLHC then put this code in a file and save it as lsm303dlhc.pl : #!/usr/bin/perl use strict; use warnings; use Device::LSM303DLHC;  my $dev = Device::LSM303DLHC->new(I2CBusDevicePath => '/dev/i2c-1'); $dev->Magnetometer->enable(); $dev->Accelerometer->enable(); while(){   my $accelerometer = $dev->Accelerometer->getRawReading();   my $compass = $dev->Magnetometer->getRawReading();   print "\tACCELEROMETER: $accelerometer->{x}\t$accelerometer->{y}\t$accelerometer->{z}\t\tCOMPASS: $compass->{x}\t$compass->{y}\t$compass->{z}\t\n" ; } Run this file now, you will get the following result:     The code and these modules have been tested to work on both Raspberry Pi models and on Beaglebone black, and there is no reason why it wont work in other dev boards as long you have perl and the other dependencies for Device::SMBus on...

Getting started with Device::SMBus on Raspberry Pi

A while back I wrote a perl library Device::SMBus to interface the plethora of I2C devices available with my Raspberry Pi board. I2C is a serial hardware bus invented by Philips that allows you to connect multiple devices to the same hardware pins and talk to them using their unique device addresses. http://en.wikipedia.org/wiki/I2C Device::SMBus builds on top of a standard linux i2c library libi2c-dev to talk to these class of devices. Each device on the bus is addressed using its standard device address, and the systems interacts with these devices by setting/reading appropriate control registers and translating that information. It was recently made aware to me that it is not straightforward using perl’s Device::SMBus library with raspberry PI. It has a chain of dependencies that needs to be properly documented from start to end to make it easy to people who try to use it: I use model B Pi with Adafruit 16 Channel PWM Servo Controller PCA9685 among other devices. You must have four servos or ESC -> Brushless motors connected to channel 4,5,6,7 for testing your I2C code using the PCA9685. I have also interfaced to other I2C devices like L3GD20 Gyroscope, LSM303DLHC accelerometer and magnetometer, LPS331AP Altimeter. which may also be used to test this module if so desired. I have writter the perl modules for these devices as well. All three of these chipsets are available on Pololu’s AltIMU board, however these chipsets may also be available on other boards by themselves. Device::L3GD20 Device::LSM303DLHC Device::LPS331AP I will skip over the hardware setup guide as that is not the purpose of this manual and there are enough...