Servers by jDrones

MAVLink and Arduino: step by step


(davidbitton) #41

@jplopezll have you thought about throwing the code up on Github?

(Leiro Juan) #42

Hi again.

Everything is working great for me. I receive data from Pixhawk on my arduino, but I dont know hot to do to send data from arduino to pixhawk. Someone try to use the debug messages or someone know exactly how to create a custom message?

The goal is to send current measurements from arduino to pixhawk. Someone can help?


(Juan Pedro) #43

Hi, what for Github? I have an account there, but I do not think this project is that big.

(davidbitton) #44

It just makes it easier to share the code, and if someone has a suggested change, that’s easy to assimilate too.

(Juan Pedro) #45

OK, I will think about it. Maybe whenever I finish the review of the article I have pending…

(Max Hobson) #46

Good evening. I’ve read through this thread and everything is very well explained and very clear, however, I’m using a BlueROV2, which uses Ardusub, so I’ve been wondering wether there are any important differences in the configuration because of it. For example, I checked the sysid variable in QGroundControl, and it is 1, as in your code, so no issues there. however, the int type = MAV_TYPE_QUADROTOR line probably needs to have something else in it, and I can’t seem to find anywhere what should go there.

(Juan Pedro) #47

Hi, @mjhl96:

I would suggest type 12 MAV_TYPE_SUBMARINE Submarine. You have all the types folllowing this link, under the MAV_TYPE enumeration.

Honestly, I have no experience on using other kind but quadcopters. I have no idea on how the type can impact on response.

Any feedback will be very much appreciated.

(Adam van Muschenbroek) #48

Hey @jplopezll , great tutorial! I do have a question though. If I were to send a command to the Pixhawk using a Mavlink message over UART, will the pixhawk already understand what to do? For instance, do I just need to load the firmware onto it and when I flash the code it will start doing whatever command I sent?

(Juan Pedro) #49

Hi, @Canadaexplorers. I do not fully understand your question. All comm ports of the Pixhawk are UART ports. You first decide what por to be used for MAVLink according to your setup, then you set the port to “speak” MAVLink using your ground station software. Check this link and revert in case of doubts.

(Xenopoulos) #50

Dear Juan Pedro
I read with great interest your really very explanatory thread.
I need your help in the following simple(?) issue: I have connected through I2C two ARDUINOs (a master MEGA and a slave NANO). Using the following code they communicate correctly:
For the master:
#include <Wire.h>
String msg = String("");
void setup()
Wire.begin(); // join i2c bus
Serial.begin(9600); // start serial for output
void loop()
Wire.requestFrom(10, 27); // request n=27 bytes from slave device #10
while (Wire.available())
char c =; // receive a byte as character
Serial.print©; // print the character
And for the slave:
#include <Wire.h>
void setup()
Wire.begin(10); // join i2c bus with address #10
Wire.onRequest(requestEvent); // register event
void loop()
// function that executes whenever data is requested by master
void requestEvent()
Wire.write("41.139234,24.912345,67.25; "); // respond with message of n bytes

I must replace the master ARDUINO MEGA with the PIXHAWK flight controller of a hexa.
The purpose is to transfer the above sentence "41.139234,24.912345,67.25;” to the python code through which I’m controlling the hexa.

  1. my first question is: is it possible following your suggestion using “mavlink.h” to “read” this sentence by Pixhawk through a serial port (e.g. Telem 2) and to have it available inside my python scripting in MP?
  2. Is it possible to implement a simpler (?) solution: what should be the code in python scripting, that will replace the above cpp code for master, in order to communicate through I2C with the slave ARDUINO NANO and read the above sentence "41.139234,24.912345,67.25;”? I use python scripting through Mission Planner to control the hexa.
    Thanking you in advance.

(Xenopoulos) #51

Hi Johnathan
I 'm trying exactly the same thing! I have implemented already the wireless data collection from a remote sensor (GPS) to ARDUINO NANO and the pre-processing. Now I’m trying to connect it (ARDUINO) to Pixhawk either through Telem2, either through I2C but I didn’t make it. I’m controlling a hexacopter using Python scripting through Mission Planner and the purpose is to access these data through the Python scripting in MP. Did you make any progress??

(Juan Pedro) #52

Hi, @Ioannis. If you can load mavlink in the nano, you can send messages directly to the MP using its address. If you do not need any processing in the Pixhawk, the message will just pass through and reach MP.

Unfortunately, I have no experience with python scripting.

(Xenopoulos) #53

Thank you Juan for your prompt response! If you mean, to include the library: #include <mavlink.h> in the Arduino NANO code, Yes, I can. Can you be more specific in “you can send messages directly to the MP using its address” ? Which is its address? Have you an example code to do this. As regard the python scripting to access the information from Pixhawk I don’t think that there will be a problem!
Thank you again

(Juan Pedro) #54

Hi @Ioannis. What I mean is that when you compose a MAVLink message you specify the destination address. If you re-read step 4 of the article, you will see a comment indicating that the Pixhawk has a reserved address (1), same as the ground station (255).

This means that you can send a message from the nano to address 255. The Pixhawk will just ignore it and will reroute it to the mission planner.

The code is easy, just change the destination address from 1 to 255 when composing the message.

(Xenopoulos) #55

Dear Juan
thank you so much for your response. I will try it and let you know
Best regards

(Leiro Juan) #56


Anyone knows how to send a MAV_CMD_USER_1 command? When I try it an error message apears, “Command not supported”.
But this command its allready defined in Mavlink 2.0

Thanks for your time

(Juan Pedro) #57

Hi @LeiroJuan:

What library have you loaded? If you are using a nano, probably you are using v1.0 of the protocol. I have tried loading v2 of MAVLink to the nano, but there is not enough flash memory as v2 has longer messages and needs more space.

To use v2 of MAVLink you will need a Mega…

(ppoirier) #58

Or a Teensy that offer more memory and power on a smaller footprint

(Leiro Juan) #59

Now Im thinking that this is not the best thread probably for make this cuestion.

Anyway Im trying to send this commando from a computer to an arduino Uno. Im using QGC on the labtop and it tells me that MAV_CMD_USER_1 its not supported, its strange because QGC its working with the mavlink 2.0, so i dont know how to fix it. Im studying the code of QGC now.

Thanks for your time

(Xenopoulos) #60

Hi again dear Juan
after hard trying for two days I don’t think that I’m now even one step ahead! The reason is obvious: I’m new here … Are you sure that it is so simple to compose a MAVLink message by just changing the destination address in the step 4? If it is not so difficult for you could you please indicate me the basic commands to send the message including the characters: "41.139234,24.912345,67.25;” to a buffer of Pixhawk and those to access the content of the buffer in MP?
Thanks again for your time
Also please explain the “int compid = 158;” to which component is referred?
The controlled system is a QUADROTOR or is an airplane / fixed wing?
Thanking you again