meta data for this page
lamaPLC: 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:
| Pin | Function | Description |
|---|---|---|
| VIN | Power Supply | 3.3V to 5V input. |
| 3V | Output | 3.3V regulated output (up to 100mA). |
| GND | Ground | System ground. |
| SCL | I²C Clock | Clock signal for serial communication. |
| SDA | I²C Data | Data signal for serial communication. |
| INT1/2 | Interrupts | Programmable for motion or free-fall detection. |
| DRDY | Data Ready | Indicator 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 Pin | Arduino Uno / Nano | Arduino Mega |
|---|---|---|
| VIN | 5V | 5V |
| GND | GND | GND |
| SCL | A5 | Pin 21 |
| SDA | A4 | Pin 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
This page has been accessed for: Today: 1, Until now: 24