lamaPLC: GY-511 6DOF sensor module

GY-511 6DOF sensor module The GY-511 is a 6-degree-of-freedom (6DOF) sensor module that combines a 3-axis digital accelerometer and a 3-axis digital magnetometer into a single breakout board. It is primarily based on the STMicroelectronics LSM303DLHC chip and is widely used for creating electronic compasses and motion-tracking systems.

Key Specifications

  • Sensor Chip: STMicroelectronics LSM303DLHC.
  • Operating Voltage: 3V to 5V DC (includes an integrated low-drop voltage regulator).
  • Communication Interface: standard I²C protocol.
  • Accelerometer Range: user-selectable at ±2g, ±4g, ±8g, and ±16g.
  • Magnetometer Range: seven selectable ranges from ±1.3 to ±8.1 gauss.
  • Resolution: built-in 12-bit ADC providing a 16-bit digital data output.

Pinout Configuration

The module typically features 8 pins, though only 4 are essential for basic operation with controllers like Arduino Uno:

PinFunctionDescription
VINPower Supply3.3V to 5V input.
3VOutput3.3V regulated output (up to 100mA).
GNDGroundSystem ground.
SCLI²C ClockClock signal for serial communication.
SDAI²C DataData signal for serial communication.
INT1/2InterruptsProgrammable for motion or free-fall detection.
DRDYData ReadyIndicator for new measured values.

Common Applications

  • Tilt-Compensated Compasses: Determining heading even when the device isn't perfectly level.
  • Motion Tracking: Used in wearables and fitness trackers to monitor activity levels.
  • Free-Fall Detection: Identifying sudden drops to trigger safety mechanisms.
  • Drones & Robots: Improving flight stability or navigational accuracy.

Arduino example code

To connect the GY-511 (LSM303DLHC) to an Arduino, the most reliable method is to use the Pololu LSM303 Library.

GY-511 PinArduino Uno / NanoArduino Mega
VIN5V5V
GNDGNDGND
SCLA5Pin 21
SDAA4Pin 20

First, install the LSM303 library from the Arduino Library Manager. This basic code reads raw accelerometer and magnetometer data and prints it to the Serial Monitor.

#include <Wire.h>
#include <LSM303.h>
 
LSM303 compass;
 
void setup() {
  Serial.begin(9600);
  Wire.begin();
 
  // Initialize the sensor
  if (!compass.init()) {
    Serial.println("Failed to detect GY-511!");
    while (1);
  }
 
  compass.enableDefault();
  Serial.println("GY-511 Initialized.");
}
 
void loop() {
  // Read all sensor data
  compass.read();
 
  // Accelerometer data (X, Y, Z)
  Serial.print("Accel X: "); Serial.print(compass.a.x);
  Serial.print(" Y: ");      Serial.print(compass.a.y);
  Serial.print(" Z: ");      Serial.print(compass.a.z);
 
  // Magnetometer data (X, Y, Z)
  Serial.print(" | Mag X: "); Serial.print(compass.m.x);
  Serial.print(" Y: ");       Serial.print(compass.m.y);
  Serial.print(" Z: ");       Serial.println(compass.m.z);
 
  delay(500); // Wait half a second
}

Key Functions in the Library

  • compass.init(): Detects if the sensor is connected via I²C.
  • compass.enableDefault(): Configures basic settings like 100Hz update rate and ±2g scale.
  • compass.read(): Fetches the latest 6-axis data from the sensor's registers.
  • compass.heading(): Calculates the compass heading in degrees (0-360) after calibration.

Calibration Note

For accurate compass readings, you must calibrate the magnetometer. Use the Calibrate example included with the library to find your sensor's specific m_min and m_max values and update them in your setup().

I2C topics on lamaPLC

PageDateTags
2025/05/31 21:56, , , , , , ,
2025/09/23 19:25, , , , , ,
2026/03/21 19:20, , , , , , ,
2026/02/15 20:33, , , , , , , , ,
2026/02/14 22:24, , , , , , , , , , , , ,
2026/02/15 20:40, , , , , , , , , , , , , ,
2026/02/14 23:37, , , , , , , , , , ,
2026/02/14 22:40, , , , , , , , , ,
2026/03/21 22:25, , , , , , , , , , ,
2026/02/14 18:19, , , , , , , , , , ,
2026/02/14 18:11, , , , , , , ,
2025/05/31 21:32, , , , , , , ,
2026/02/14 19:29, , , , , , , , , , , , , ,
2025/11/21 23:07, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
2023/07/01 15:29, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
2026/03/22 00:26, , , , , , , , ,
2026/02/14 22:45, , , , , , , , ,
2026/02/14 22:09, , , , , , , , , , , , , , , ,
2026/02/14 17:26, , , ,
2026/02/14 21:54, , , , , , , , , , , , , , , , , , , , , , , , ,
2026/02/14 23:58, , , , , , , , , , ,
2026/02/14 17:27, , , , , , , , , ,
2026/02/14 23:38, , , , , , ,
2026/02/14 22:52, , , , , , , ,
2026/02/14 22:23, , , , , , , ,
2026/02/14 22:53, , , , , , , , , , , , ,
2026/02/15 20:27, , , , , , , , , , , , , , , ,
2026/02/15 20:29, , , , , , , , , , , , , ,
2026/02/14 22:47, , , ,
2026/02/14 22:51, , , , , ,
2026/02/14 17:26, , , ,
2026/02/14 22:22, , , , , , , , , , , , ,
2026/02/14 22:21, , , , , , , , , , ,
2026/02/14 22:22, , , , , , , ,
2026/03/05 20:19, , , , , , , , , , , , , , , , ,
2026/02/14 17:27, , , , , , ,

This page has been accessed for: Today: 1, Until now: 24