Run GY 25 in Arduino IDE (with Kalman filter)

I get Euler Angles from GY-25 by Serial protocol (in Arduino IDE). If you use Software serial (Virtual serial port) you can only use baud rate of 9600 to get data from GY-25. But if you use Arduino Mega 2560, you can use Serial1 or Serial2 or Serial3 (Actual Serial ports) and set baud rate of 115200 to get data from GY-25 (fast and exact).

My code for All Arduino boards with Software serial:

#include <SoftwareSerial.h>
static const int RXPin = 68, TXPin = 69; // announce your Rx and Tx pins
SoftwareSerial Portone(RXPin, TXPin);
float Roll,Pitch,Yaw;
unsigned char Re_buf[8],counter=0;
void setup()
{ 
Serial.begin(115200);
Portone.begin(9600); // SoftwareSerial can only support 9600 baud rate for GY 25 but Serial3 can support 115200 and 9600 both
delay(4000);      
Portone.write(0XA5); 
Portone.write(0X54);//correction mode
delay(4000);
Portone.write(0XA5); 
Portone.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 
}

void loop() {
serialEvent();
Serial.print("roll= ");
Serial.print(Roll);
Serial.print(" pitch= ");                  
Serial.print(Pitch);
Serial.print(" yaw= ");                  
Serial.println(Yaw);
} // End of void loop

void serialEvent() {
Portone.write(0XA5); 
Portone.write(0X51);//send it for each read
while (Portone.available()) {   
Re_buf[counter]=(unsigned char)Portone.read();
if(counter==0&&Re_buf[0]!=0xAA) return;        
counter++;       
if(counter==8)  // package is complete              
{    
  counter=0;                  
  if(Re_buf[0]==0xAA && Re_buf[7]==0x55)  // data package is correct      
    {          
     Yaw=(int16_t)(Re_buf[1]<<8|Re_buf[2])/100.00;   
     Pitch=(int16_t)(Re_buf[3]<<8|Re_buf[4])/100.00;
     Roll=(int16_t)(Re_buf[5]<<8|Re_buf[6])/100.00;
    }      
}  
}  // End of while
} // End of serialEvent

My code for Arduino Mega 2560 with Actual Serial ports:

// Connect RX of GY 25 to TX3 Arduino Mega and TX of GY 25 to RX3 Arduino Mega 
float Roll,Pitch,Yaw;
unsigned char Re_buf[8],counter=0;
void setup()
{ 
Serial.begin(115200);
Serial3.begin(115200);// SoftwareSerial can only support 9600 baud rate for GY 25 but Serial3 can support 115200 and 9600 both
delay(4000);      
Serial3.write(0XA5); 
Serial3.write(0X54);//correction mode
delay(4000);
Serial3.write(0XA5); 
Serial3.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 
}

void loop() {
serialEvent();
Serial.print("roll= ");
Serial.print(Roll);
Serial.print(" pitch= ");                  
Serial.print(Pitch);
Serial.print(" yaw= ");                  
Serial.println(Yaw);
} // End of void loop

void serialEvent() {
Serial3.write(0XA5); 
Serial3.write(0X51);//send it for each read
while (Serial3.available()) {   
Re_buf[counter]=(unsigned char)Serial3.read();
if(counter==0&&Re_buf[0]!=0xAA) return;        
counter++;       
if(counter==8) //package is complete               
{    
  counter=0;                  
  if(Re_buf[0]==0xAA && Re_buf[7]==0x55)  // data package is correct      
    {          
     Yaw=(int16_t)(Re_buf[1]<<8|Re_buf[2])/100.00;   
     Pitch=(int16_t)(Re_buf[3]<<8|Re_buf[4])/100.00;
     Roll=(int16_t)(Re_buf[5]<<8|Re_buf[6])/100.00;
    }      
}  
}  // End of while
} // End of serialEvent

Note: You should set Baud rate of GY-25 according to the attached picture.

I used GY-25 with this code for my quadcopter, but the final result of GY-955 (BNO055) through I2C connection, was very better in comparison to GY-25. Here is the code to get Euler angles from GY-955 through I2C connection: https://forum.arduino.cc/index.php?topic=563091.0

And also, I obtained distance vector along the north and the east using Ublox Neo 6M module:

If you have any question, you can ask me in comments or by a pm.

Hello

Thanks about your code.

I have used GY-25 module and implemented your code, but I just got zero numbers in serial monitor.

Is it a hardware problem from GY-25 or sth else?

Hi, which Arduino board did you use?

Hello I have the same problem. The result in serial monitor is just 0,0,0.I am using arduino mega 2560. Where is the problem?

hello again my problem is solved!My mistake was wrong connecting rx of gy25 to rx of arduino :confused: but it must be: rx to tx and tx to rx. Thank you for the code.

Good Luck My friend.

Hello,

I have the problem that my arduino don't transfer the program. I don't have any error messages. I have an arduino Uno. I think i have problems with the RX/TX pin. My connections are 5V -> VCC, GND -> GND, TX -> RX and RX ->TX. Do you have an idea?

hi, What is your arduino board model? Mega 2560?

hi thanks for your helpful tutorial.l need to run GY25 with I2C and l dont find any exampls in google. can you help me?

Hi, if you connect to Gy 25 through I2C connection, you can only get raw data of MPU 6050.

Hello,

Do you have any idea how I could connect a GY-25 sensor to an ESP32 via I2C?
I had already connected the GY-25 to a MEGA via I2C, which worked without any problems.

Unfortunately I could not establish a connection between the GY-25 and an ESP32. The I2C scanner cannot find a device, even though the sensor is plugged in.

best regards

Hi, sorry I have no idea about ESP32.