MATLAB Examples

Control L3G IMU Sensor

This example shows how to use the MATLAB® Support Package for Arduino® Hardware to control a three-axis L3G gyroscope that measures the angular rates of rotation about the roll(X), pitch(Y) and yaw(Z) axes.

Contents

Hardware Requirements

  • Arduino board
  • L3G gyro breakout board with I2C interface

Hardware Setup

  • Connect SDA pin on L3G board to SDA pin on Arduino board.
  • Connect SCL pin on L3G board to SCL pin on Arduino board.
  • Connect VIN pin on L3G board to 5V pin on Arduino board.
  • Connect GND pin on L3G board to GND pin on Arduino board.

Create Arduino Object with Pololu/L3G Add-On

a = arduino('COM38', 'Uno', 'Libraries', 'Pololu/L3G')
a = 

  arduino with properties:

                    Port: 'COM38'
                   Board: 'Uno'
           AvailablePins: {'D2-D13', 'A0-A5'}
               Libraries: {'I2C', 'Pololu/L3G'}

Read Data from L3G Device

Create L3G device object for L3GD20H gyro.

gyro = addon(a, 'Pololu/L3G')
gyro = 

  L3G with properties:

               Pins: 'A4'(SDA), 'A5'(SCL)
                Bus: 0
         I2CAddress: 107 (0x6B)
         DeviceType: 'D20H'
        SA0PinState: 'auto'

Read angular velocity.

vc = gyro.readAngularVelocity;
fprintf('Angular Velocity: %+.3f(X) %+.3f(Y) %+.3f(Z) (rad/s)\n', vc(1), vc(2), vc(3));
Angular Velocity: +0.868(X) -0.533(Y) +0.708(Z) (rad/s)

Clean Up

clear gyro
clear a