Angular velocity with Arduino

In this tutorial we will use the L3G4200D sensor from STMicroelectronics. This gyroscope measures angular rates about three axes. They are referenced as x, y and z. The angular velocity about the x, y and z axis are also called respectevely roll pitch, and yaw rate.

What you will need

  • L3G4200D 3 axes gyroscope
  • Arduino Board. It is possible to interface the sensor to a Raspberry Pi as well.
  • Breadboard
  • Male-male jumpers
  • Software: Arduino IDE. Processing


This sensor is capable of providing the measured angular rate through a digital interface. It supports both SPI and I2C and has some other features such as programmable interrupts. We will use the I2C interface, which requires the following set up:

  • GND -> GND
  • VCC -> 3.3V
  • SDA -> A4
  • SCL -> A5
  • SD0 -> 3.3V


Getting angular velocities

In order to read data we need to request them from the sensor using the I2C library. As we can see from page 35 of the datasheet, the angular rate data are expressed as two’s complement and stored in registers. The following snippet retrieves these values from the registers and saves them in x,y and z variables.

So with the unique device address 105 (in hexadecimal) and the number of bytes of data to accept from the device we first begin the transmission and request data from the register. Then we read and save the bytes we are looking for. The read and write functions are reported below. Thanks to for these.

Finally, we will print the angular rates to the serial interface in the format “A,wx,wy,wz,Z”.

Initiate the Wire library for I2C communication, the Serial interface and the gyroscope with the resolution of 250°/s. Place what follows in your setup().

Full code

This code will print the angular rate values in a CSV friendly format. Uncomment the DEBUG constant to find out what the readings are.

These readings are not steady, it would be impossible to use them in a application. Check the second part to remove bias.



Measuring how fast something is spinning about an axis could be useful for the following domains:

  • Gaming and virtual reality input devices
  • Angular Motion detection
  • Attitude Estimation
  • GPS navigation systems
  • Appliances and robotics