File Exchange

image thumbnail

Legacy LSM303 Add-On Library for Arduino

version 1.3.1 (96.5 KB) by MathWorks MATLAB Hardware Team
Communicate with LSM303 devices connected to Arduino hardware in MATLAB.


Updated 22 May 2019

View License

This add-on is supported for MATLAB R2016a-R2018b.

This add-on library extends MATLAB Support Package for Arduino Hardware to read from LSM303 accelerometers and compasses. With the add-on, you can read measured acceleration in meter per second squared and magnetic field strength in microtesla. Supported boards are LSM303D, LSM303DLHC, LSM303DLM, and LSM303DLH.

Sample usage:
% Create arduino object with the add-on library
a = arduino('COM38', 'Uno', 'Libraries', 'Pololu/LSM303');
% Create LSM303 object
compass = addon(a, 'Pololu/LSM303');
% Obtain acceleration
acc = readAcceleration(compass);
% Obtain acceleration
mag = readMagneticField(compass);

It includes documentation and an example that demonstrates the use with an LSM303 to get acceleration and magnetic field strength.

Important: Before using this add-on library in MATLAB, you need to install the LSM303 Arduino library. Here are the instructions:
1. Download the zip file from
2. Unpackage the zip into local directory.
3. Move the ‘LSM303’ folder inside unzipped folder into the "libraries" folder inside your Arduino sketchbook folder. You may need to create the folder if it does not already exist:
On Windows, the default path is "Documents\Arduino\libraries\"
On Mac, the default path is "~/Documents/Arduino/libraries/"
On Linux, the default path is "/home/<username>/Arduino/libraries"

Troubleshooting tips:
1. After installing this add-on library and the required Arduino library, type "listArduinoLibraries" in MATLAB to see if the add-on is properly installed.
2. Wire up the Arduino board and the add-on device properly before creating the object to avoid a connection error in MATLAB.
3. Refer to the documentation for details on usage and syntax. It can be found under Supplemental Software in the product documentation.

Feel free to contact the MATLAB Hardware Team if you have questions about this add-on library:

Cite As

MathWorks MATLAB Hardware Team (2020). Legacy LSM303 Add-On Library for Arduino (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)


This file does not work on Matlab 2019b.
The following message appears:

>> a = arduino('COM5', 'Leonardo', 'Libraries', 'Pololu/LSM303')
Updating server code on board Leonardo (COM5). This may take a few minutes.
Cannot program board Leonardo (COM5). Please make sure the board is supported and the port and board type are correct. For more information, see Arduino
Hardware Troubleshooting.

Pololu/LSM303 is not supported in this file on R2019b.
If developed, please update it.

Thank you in advance.


Junel Fronda

Can this be used on a different sensor? Like ADXL345 accelerometer and HMC5883L magnetometer? Thanks.

DongHyun Youn



Added Legacy to title


Updated description and release compatibility to specify that the library is only supported for releases through R2018b.


Added license addendum and updated MathWorks http references to https

Update Arduino library install instructions on both webpage description and the shipping documentation.

MATLAB Release Compatibility
Created with R2016a
Compatible with R2016a to R2018b
Platform Compatibility
Windows macOS Linux