Skip to content

Instantly share code, notes, and snippets.

@fxprime
Created June 23, 2023 09:38
Show Gist options
  • Save fxprime/1a40362bd37c6be94490f5d54e922337 to your computer and use it in GitHub Desktop.
Save fxprime/1a40362bd37c6be94490f5d54e922337 to your computer and use it in GitHub Desktop.
Example for serial mode of GY-25
/**
www.modulemore.com
Example for serial mode of GY-25
Product link : https://www.modulemore.com/product/1888/เซนเซอร์วัดการเอียง-gy-25-tilt-sensor-module-mpu-6050
Based on : https://forum.arduino.cc/t/run-gy-25-in-arduino-ide-with-kalman-filter/565016
PDF : http://mkpochtoi.ru/GY25_MANUAL_EN.pdf
*/
#include <SoftwareSerial.h>
static const int RXPin = 2, TXPin = 3; // announce your Rx and Tx pins
SoftwareSerial gy25Serial(RXPin, TXPin);
float Roll, Pitch, Yaw;
unsigned char buffer[8], counter = 0;
bool imu_updated = false;
// Output format :
// each frame contains 8 bytes (hex):
// 1.Byte0: 0xAA Preamble Flags
// 2.Byte1: 0x00-0xFF HIGH heading high
// 3.Byte2: 0x00-0xFF LOW heading lower
// 4.Byte3: 0x00-0xFF HIGH pitch angle
// 5.Byte4: 0x00-0xFF LOW pitch angle
// 6.Byte5: 0x00-0xFF HIGH roll angle
// 7.Byte6: 0x00-0xFF LOW roll angle
// 8.Byte7: 0x55 Frame end flag
// Calculation method:
// Angle = ( (HIGH << 8) | LOW ) / 100;
// COMMANDS:
// 1. 0xA5 + 0x51: query mode, return directly to the angle value, to be sent each read
// 2. 0xA5 + 0x52: Automatic mode, send a direct return angle, only initialization
// 3. 0xA5 + 0x53: Automatic mode, ASCII code output, serial port for direct computer
// assistant View
// 4. 0xA5 + 0x54: correction mode, the pitch correction roll angle of 0 degrees, need
// to stay level when sending
// 5. 0xA5 + 0x55: correction mode, 0 degree course correction, heading cleared at
// any angle
// (1), due to self-correction at power up the module must be held in a stationary
// position for at least 3000ms, hand-held of this module is not recommended
// (2), module heading will drift after a while without magnetometer.
// (3), due to the angle of the Euler angles universal lock problem, roll, pitch, have an
// impact on each other at 90 degrees.
// (4), The module IOs are 5.0V tolerant, the module can be used with 5.0 and 3.3V
// system and serial adapters without any risk.
void setup() {
Serial.begin(115200);
gy25Serial.begin(9600); // SoftwareSerial can only support 9600 baud rate for GY 25 but Serial3 can support 115200 and 9600 both
delay(1000);
Serial.println("--- Start setting GY-25 ---");
//correction mode, the pitch correction roll angle of 0 degrees, need to stay level when sending
gy25Serial.write(0XA5);
gy25Serial.write(0X54);
Serial.println("--- Calibrating GY-25, need to stay level ---");
delay(4000);
// gy25Serial.write(0XA5);
// gy25Serial.write(0X51); //0X51:query mode, return directly to the angle value, to be sent each read, 0X52:Automatic mode,send a direct return angle, only initialization
gy25Serial.write(0XA5);
gy25Serial.write(0X52); //Automatic mode, send a direct return angle, only initialization
Serial.println("--- End setting GY-25 ---");
}
void loop() {
serialEvent();
if (imu_updated) {
imu_updated = false;
Serial.print("roll= ");
Serial.print(Roll);
Serial.print(" pitch= ");
Serial.print(Pitch);
Serial.print(" yaw= ");
Serial.println(Yaw);
}
// delay(100);
} // End of void loop
void serialEvent() {
// gy25Serial.write(0XA5);
// gy25Serial.write(0X51); //send it for each read if you use mode 1
while (gy25Serial.available()) {
buffer[counter] = (unsigned char)gy25Serial.read();
if (counter == 0 && buffer[0] != 0xAA) return;
counter++;
if (counter == 8) // package is complete
{
counter = 0;
if (buffer[0] == 0xAA && buffer[7] == 0x55) // data package is correct
{
Yaw = (int16_t)(buffer[1] << 8 | buffer[2]) / 100.00;
Pitch = (int16_t)(buffer[3] << 8 | buffer[4]) / 100.00;
Roll = (int16_t)(buffer[5] << 8 | buffer[6]) / 100.00;
imu_updated = true;
}
}
} // End of while
} // End of serialEvent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment