Servers by jDrones

MAVLink and Arduino: step by step

Is there ONE source that has all the code? Putting together all the snippets is a pain. I am looking for code that doesn’t run LEDs - it simply outputs requested parameters and allows the user to send Mavlink messages to change parameters inside a PixHawk.

I would greatly appreciate if someone posted a .ZIP file or some other source that was directly compilable. I need to start from this code and add my own functionality.

I figured out how to get things working to a basic degree. I’m using a TEENSY 3.2, and the PixHawk is connected to Serial1. My computer is connected to the USB port (which is Serial0). This thread is so long maybe I missed it but I would like to be able to read the current MODE (STABILIZE, AUTO etc) of the controller, and I would like to be able to change the MODE of the controller, as well as change the direction of movement.

Can anyone give me a few pointers on how to accomplish this?

Hi @jplopezll,

I having some doubts and need help from everyone. I am building an obstacle avoidance system for F450 quadcopter by using hc-sr04 ultrasonic sensor. The arduino UNO TX is connect to RX telem2 pixhawk 2.4.8. It seem like there is no function to the pitch and roll. I was wondering there are some problem in my coding. Below are the code to connect arduino with MAVlink. Thanks.

void FHeartBeat() {
mavlink_message_t msg;
uint16_t len;
// System ID = 255 = GCS
mavlink_msg_heartbeat_pack(1, 0, &msg, MAV_TYPE_QUADROTOR, MAV_AUTOPILOT_GENERIC, 0, 1, 0);

// Copy the message to send buffer
len = mavlink_msg_to_send_buffer(buf, &msg);

// Send the message (.write sends as bytes)
Serial.write(buf, len);


void RCOverride(mavlink_message_t *msg, uint16_t len, uint8_t *buf, uint16_t PitchOut, uint16_t RollOut) {
//Pack and send the calculated Pitch and Roll data. Only send if the data is new
mavlink_msg_rc_channels_override_pack(1, 0 , msg, 1, 0, RollOut, PitchOut, 0, 0, 0, 0, 0, 0);
len = mavlink_msg_to_send_buffer(buf, msg);
Serial.write(buf, len);
Serial.print("\n\rPitch: “);
Serial.print(" Roll: ");

Dear @jplopezll:
Your way of encode MAVlink is so readable and clear .Thank you so much! I got the msgids from PIXhawk,but the msgids I got are imperfect.
In short, the msgid I want to get is 141.
I have been tried to change the MAVStreams,but it no useful.

>     MAV_DATA_STREAM_ALL=0, // Enable all data streams | 
>     MAV_DATA_STREAM_EXTRA1=10, // Dependent on the autopilot | 
>     MAV_DATA_STREAM_EXTRA2=11, // Dependent on the autopilot |
>     MAV_DATA_STREAM_EXTRA3=12, //Dependent on the autopilot | 

I have tried each MAVStreams.But the msgids don’t seem to have changed.
It will be grateful if you could answer my dump questions.I will try my best to answer asap.By the way,there may be jet lag,because I am in China.
best wishes for you!:grinning:

Hi, @Agent_of_Fooyou:

Sorry for the belated reply, I have not much spare time nowadays…

I canno’t find any stream that include 141 as a reply message, I am almost sure it is not implemented. I don’t know what is your final need, at first sight you have two choices:

  1. Implement the function: write the code, compile, upload to the Pixhawk. Complex task and error prone.

  2. Calculate the altitude yourself based on the pressure reported by the barometer, the GPS and the initial offset. Sounds complex but it is more or less trivial if you do not need extreme precission in the calculations. You need to request for MAV_DATA_STREAM_RAW_SENSORS.

If you need further help, better send me a PM.


Hi, @charles_linquist:

Sorry for the belated answer.

As far as I know, you cannot request for the mode via streams. But you can read the value of the RC channel where you send the mode and calculate in which mode you are based on your configuration.

You can also override the value of that channel to manually force a mode on the flight controller. Be careful when doing this, as you may lose control of the aircraft.

For more focused recommendations I would need to know more about your setup and objectives. Send me a PM if you need further assistance.

Kind regards.

Hi, Juan. Your post is the most helpful to understand how to receive data from Mavlink.

I’m having one problem right now, probably you can help.
My code is working but somehow the code -> mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status) always return the value as zero. Do you have any idea why?

Solved, I did twice Mav_Request_Data() that’s why it doens’t work.

1 Like

Dear @Fazlur_Rahman:

Thank you very much for your kind words. Good to see that your problem is already solved!

Do not hesitate to ask if you need further help.

Kind regards.

Dear @jplopezll
I’m working on a connection between Pixhawk and Arduino and I want to control the movement of the drone from the Arduino according to the ultra sound sensor. I can reicive pixhawk packets on the Arduino and see them on the Arduino’s serial monitor, but my objectif is to write an Arduino code that allows me to send commands to the Pixhawk to control it (Land if there is an obstacle). and also see it on QGroundControl.
My problem now is that I do not know what functions I should use and how to use it.
Please I need HELP.
And Thank you soo much.


Yours is not an easy question and has many potential answers depending on your setup and your objectives.

Have a look at the definitions and descriptions of the common part of the MAVLink library and revert with what could be your proposal. Either me or anybody else will be willing to help!


Sorry for being late.
Dear @jplopezll
I want that if the ultrasound sensor detects an obstacle the Arduino sends to pixhawk a commend for the drone to stop at the same position and land for example.
so i need a mavlink function wich can do that.
I looked on the Internet but I did not find a file that explains the functions of mavlink and how to use it.
I am currently in Internship in Madrid and I am working on this project. the ultimate goal is for the drone to fulfill a mission and avoid obstacles with ultrasound senser and Arduino.
if you can help me, I will be very happy.
Thank you in advance for your answer.

Hi all, might be a strange question but has anyone had any luck taking MAVLINK data directly from a telemetry modem into the arduino? Would be used in a ground station to pick out key information (Battery, RSSI, distance from home, altitude etc) and display it on an LCD.

Any thoughts?

I’ve tee’ed off a RFD900x telemetry modem output on the ground (received from the plane side) to capture the GPS position (lat, long, and altitude) from the plane with an antenna tracker). The tracker runs Arduino code on an STM32 Maple Mini.

Since the tracker can’t talk back to the telemetry radio (I did not connect a wire from the STM TX side to the telemetry radio input), the link telemetry link works as normal with a Raspberry PI for a MavLink ground station.

Any telemetry data coming down the link should be available to use. I’m not sure if distance from home is sent over MavLink, but I calculate distance between my tracker GPS and the plane GPS.

I display the plane and tracker altitudes and positions as well as the distance/azimuth/and elevation angles between them. However the display is typically only used for troubleshooting the tracker since it’s not daylight readable…and it is mounted on the pan axis so it slowly rotates most of the time.

If I lose sight of the aircraft, the antenna point direction is helpful in showing where to aim my eyes.



So after a brief scan of this thread, can I infer that this library can be used to get commands from the PixHawk 2.1 and have an Arduino activate small motors through a shield?



This just shows a very basic way to send and receive Mavlink messages. The Mavlink library has all of the methods to package data.

Servers by jDrones