Connecting the Pixhawk flight controller to Arduino Uno, to read MAVLink heartbeat data in serial monitor

5,308 views
Skip to first unread message

Carl Michael

unread,
Mar 10, 2016, 3:55:34 AM3/10/16
to MAVLink
Hi, I was wondering if anyone has ever successfully connected the Arduino Uno to the pixhawk flight controller via telemetry port 2 or port serial 4/5 and is able to read the MAVlink heartbeat data packets in the arduino serial monitor. 

I am extremely new to this and i dont have much programming experience, i have been searching high and low on the internet but i can't find any working open source codes, i have tried the codes in this link: https://github.com/alaney/arduino-mavlink/blob/master/ArduinoMAVLink/ArduinoMAVLink.ino  But i cant get it to compile successfully. 

Would someone be so kind as to share their experience with me, step by step, on how to connect the wiring to the arduino and also coding the pixhawk and arduino in order to read and receive heartbeat data on the arduino serial monitor? I would really appreciate any help and tips, thank you in advanced!

Demetric Henry

unread,
Mar 10, 2016, 12:38:46 PM3/10/16
to MAVLink
Hello Carl Michael,

I started just as you did about 1 or 2 months ago. This is the information I have gathered to use telemetry port 2:

1. Connect the Pixhawk to the Arduino similar to the first picture but do not connect the power(red line). The arduino start acting strange when I did. http://dev.ardupilot.com/wiki/raspberry-pi-via-mavlink/.
2. The Pixhawk Tx(green) is connected to the Arduino pin that you set as Rx. Pixhawk Rx(orange) is connected to the Arduino pin that you set as Tx.
3. On the Pixhawk, connect it to either Mission Planner or QGroundControl applications. Find the Parameters list(Under Config/Tuning in Mission Planner and Parameters in QGroundControl) and set Serial2_BAUD to 57. Note: Edit  sub-parameters under SR2 to get other messages forward to the Telemetry 2 port.
4. On the Arduino, I went here(https://github.com/alaney/arduino-mavlink) to get the arduino code and mavlink libraries. I had to get all the files in common folder and place same folder as ArduinoMAVLink.ino  and then all files in pixhawk folder and place the into the same folder as my ArduinoMAVLink.ino file(I overwritten any duplicates).
5. When you open the ArduinoMAVLink.ino file, you would see FastSerial. If you can get it to work please let me know how but I have given up. Here is what I changed:

#include <FastSerial.h>
#include "../mavlink/include/mavlink.h"        // Mavlink interface

FastSerialPort0(Serial);



with

#include <SoftwareSerial.h>
#include "mavlink.h"

#define RX_PIN 2 //used for Mavlink, reads input from Pixhawk
#define TX_PIN 3 //used for Mavlink, transmits to Pixhawk

SoftwareSerial MavSerial = SoftwareSerial(RX_PIN, TX_PIN);

Note 1: Serial port can either be pins 0 and 1 or the USB port. I wanted to use the Serial Monitor feature in Arduino application with the USB port so I did not use those pins but you can if you like.
Note 2: I use pins 2 and 3 for my Rx and Tx on the Arduino Uno but you can select other pins.

6. This is some simple code to verify if you are getting something from the telemetry port. I would comment out the other code in the "void loop" until you know you are getting data. As mentioned in the previous note, I get data from MavSerial and display the info on Serial which is displayed in Serial Monitor. Here is the code:

void setup()
{
  Serial.begin(57600);
  while (!Serial)
  {
    ; // wait for port to connect.
  }
  MavSerial.begin(57600);
  while (!MavSerial)
  {
    ; // wait for port to connect.
  }
 
  Serial.println("Ready");
 
}
void loop()
{
  while (MavSerial.available() > 0 )
  {
    Serial.print("Num Bytes: ");
    Serial.println(MavSerial.available());
  }
}
If you get nothing, check your connections (try reversing the Rx and TX on the arduino) or double check that Serial Baud is set to 57.

7. If you are getting data the remove the previous code in the "void loop" and uncomment the original code. After this, it is just playing with the Mavlink code(I spent about 2 weeks on this). I wasn't getting certain messages (example 42 - Mission Current) and couldn't figure out why. I eventually wrote my own decode code but I still use their packing/checksum code.

Regards,

Demetric

Habibur Rahman

unread,
Feb 13, 2019, 3:26:42 AM2/13/19
to MAVLink
Dear Henry,
it's been you posted it but it really helpful post. Thank you. I can print the Num Bytes, but could you explain what does it means? Thank you again.
Reply all
Reply to author
Forward
0 new messages