Softivation

Raspberry Pi won't connect to Pixhawk, the cable is right

If the Raspberry Pi won't connect to the Pixhawk and no heartbeat arrives, the problem is often not the cable. The process holding the UART, the wrong device node, and a baud mismatch.

Furkan IŞIKAY 9 min read

You connected the Raspberry Pi to the Pixhawk's TELEM2 port, checked the cable three times, and ran your code. There is a single line on the screen and it just sits there: waiting for a connection. The cable is in place, the board is on, the lights are lit.

At this point most people unplug the cable and plug it back in. It rarely helps, because the problem is usually not in the wire itself but in who is listening to that wire and whether the two sides are talking at the same speed. Below we'll eliminate the link step by step.

Even if the cable is right, the three wires can be wrong

The Pixhawk's TELEM2 has six pins, but only three go to the Raspberry Pi: transmit, receive and ground. Transmit and receive are crossed. If you line up the pins with the same name facing each other, both sides switch to listening and nobody talks.

The ground wire is the one most often skipped. If you power the Pi from a separate source, there is no common reference between the two boards. The link either never comes up or produces random corrupted packets.

In the figure, note that the red and blue wires cross in the middle: the Pi's transmit pin goes to the Pixhawk's receive pin, and vice versa.

The three wires between the Raspberry Pi and the Pixhawk TELEM2

Raspberry Pi pin 8 TXD connects to TELEM2 pin 3 RX, pin 10 RXD to TELEM2 pin 2 TX, and pin 6 GND to TELEM2 pin 6 GND. TELEM2 pin 1 (+5 V) is not used
What to look at: where the red and blue wires cross. TELEM2's pin 1 (+5 V) is not used to power the Pi. The Pi is powered from a separate source, and only GND is shared.

Someone else may have opened the port before you

On a Raspberry Pi the serial port is not free by default. Linux's own console uses that line for logins. On the Pi 3 and 4, the Bluetooth hardware takes the good UART for itself. When ModemManager sees a new serial device, it probes it for seconds.

All of these open the port. Your code opens it too, but whoever gets the byte first gets the MAVLink stream, and nothing is left for you.

  • Linux serial console: in raspi-config, turn off the login shell and leave the hardware enabled.
  • Bluetooth: add the disable-bt line to the config file and reboot the board.
  • ModemManager: if it's installed, stop the service.
  • A stale process that never exited: the code left over from your previous attempt may still be holding the port.

Example: Say the SSH session where you tried the link with MAVProxy yesterday was left open. When you start your own code today, the port opens, waits 15 seconds, and exits without a heartbeat. The cable is yesterday's cable, the board is the same board. Before touching the cable, check who is holding the port, close the old process, then restart your code.

On the Pi 5 the node name changed

The instructions that have circulated for years point to /dev/serial0, and that is correct on the Pi 3 and Pi 4. On the Pi 5, the same name is tied to the debug socket on the board. The node that goes to the GPIO pins is /dev/ttyAMA0. With the same cable and the same settings, nothing arrives just because of the node name.

First check which Pi you have and choose the node accordingly. Applying an old forum answer to a Pi 5 as is, is the mistake that wastes the most time on this topic.

Are both sides talking at the same speed

On the autopilot side, the speed is set by a parameter, and the number you write there is not the baud directly. The firmware converts it from a table. If you write 57 it becomes 57600, if you write 921 it becomes 921600. If you write zero or a negative number, the port doesn't close; it silently falls back to 57600.

The speed in your code must be the same too. If it differs, the link comes up but garbage characters spill onto the screen. This is different from no data arriving at all, and it needs you to look somewhere else.

There is also the opposite case: the speed matches, the link comes up, then it keeps dropping. The cause is usually that the telemetry stream you asked for doesn't fit in that speed.

In ArduCopter 4.7.0, if you request nothing, there is almost no traffic on the link. Your code opens the stream. Look at the red bar in the figure: if you ask for a rate like 50 Hz and stay at 57600, the data the autopilot produces rises to several times what the link can carry, and packets start dropping.

How much bandwidth the MAVLink stream wants

With no requests MAVLink traffic is 96 B/s, with a typical ground station request 3,152 B/s, with a 50 Hz request 49,841 B/s. The 57600 baud ceiling is about 5,760 B/s, and the 921600 baud ceiling is about 92,160 B/s
What to look at: where the red bar passes the 57600 line. A 50 Hz request exceeds this ceiling by 8.7 times, and the same request fits at 921600.

Eliminate the link one step at a time with a single program

The code below applies the steps above in order. If it can't connect, it tells you which layer it stopped at. If it connects, it prints that the heartbeat arrived, the firmware version and the serial parameters. Run the code on the Raspberry Pi.

Python
import sys, time
from pymavlink import mavutil

BAGLANTI = sys.argv[1] if len(sys.argv) > 1 else "/dev/serial0"
BAUD     = int(sys.argv[2]) if len(sys.argv) > 2 else 57600

def teshis(sebep):
    print("LINK NOT ESTABLISHED ->", sebep)
    print("  1. port bos mu   : sudo fuser -v", BAGLANTI)
    print(" 2. right device node?: ls -l /dev/serial0 (on a Pi 5, /dev/ttyAMA0)")
    print(" 3. permission granted : id -nG | grep dialout")
    print(" 4. speed matches : SERIALx_BAUD =", BAUD // 1000, "should be")
    sys.exit(1)

print("connecting:", BAGLANTI, BAUD)
try:
    efendi = mavutil.mavlink_connection(BAGLANTI, baud=BAUD, source_system=250)
except Exception as hata:
    teshis("could not open port (%s)" % hata.__class__.__name__)

if efendi.wait_heartbeat(timeout=15) is None:
    teshis("port opened but no HEARTBEAT within 15 s")

print("HEARTBEAT OK sysid=%d compid=%d" % (efendi.target_system, efendi.target_component))

efendi.mav.command_long_send(efendi.target_system, efendi.target_component,
                             mavutil.mavlink.MAV_CMD_REQUEST_MESSAGE, 0, 148, 0, 0, 0, 0, 0, 0)
bitis = time.time() + 5
while time.time() < bitis:
    s = efendi.recv_match(type='AUTOPILOT_VERSION', blocking=True, timeout=1)
    if s:
        v = s.flight_sw_version
        print("firmware %d.%d.%d" % ((v >> 24) & 255, (v >> 16) & 255, (v >> 8) & 255))
        break

for ad in ("SERIAL2_BAUD", "SERIAL2_PROTOCOL", "SERIAL2_OPTIONS"):
    efendi.mav.param_request_read_send(efendi.target_system, efendi.target_component, ad.encode(), -1)
    p = efendi.recv_match(type='PARAM_VALUE', blocking=True, timeout=3)
    print("  %-17s %s" % (ad, "could not be read" if p is None else "%g" % p.param_value))

When running it, you can pass the connection address and speed as arguments. If you're using a Pi 5, make the first argument /dev/ttyAMA0.

What you see when the link is up

If the link is healthy, you'll see this when you run the code. The version line and parameter values may differ on your board:

Output
HEARTBEAT OK sysid=1 compid=0
firmware 4.7.0
  SERIAL2_BAUD      57
  SERIAL2_PROTOCOL  2
  SERIAL2_OPTIONS   0

When the link doesn't come up, the code leaves you four commands and exits. Apply them from top to bottom without changing the order.

Error Messages and Their Meanings

The texts below are lines that MAVProxy, DroneKit and pyserial actually print. Which one you see tells you which layer you're stuck at.

Waiting for heartbeat from /dev/serial0

MAVProxy was able to open the port, but no MAVLink was decoded from the other end. Since the port opened, the node name and permission problems are behind you. The next candidates: the transmit and receive lines not being crossed, no common ground, the two sides' speeds differing, or that port not being set to MAVLink on the autopilot.

Packets arrived for a while, then stopped. If the characters are readable, the speed is right but the link can't carry the load or the physical connection is unstable. If you see unreadable characters on the screen, the picture is different: that means the two sides' speeds differ.

DroneKit opened the port, waited five seconds, and no heartbeat arrived. If MAVProxy works with the same address, the problem is not in DroneKit but in the timeout value or in another process holding the port at that moment.

could not open port /dev/serial0: [Errno 2] No such file or directory

The port doesn't exist at all. Either the serial hardware isn't enabled or on this Pi that name maps to something else. Look at the device list and confirm the node actually exists.

could not open port /dev/serial0: [Errno 13] Permission denied

The node exists, but the user has no access rights. Add the user to the serial port group, then log out and log back in. Running the code as a privileged user seems to fix this, but it changes the Python environment and leads to other errors.

What changes in the field

Most of the steps here you can only check at the board: who is holding the port, which device the node name points to, whether the cable is crossed.

You can test the code's logic and the stream's bandwidth needs on a computer as well. But the cable, the port and the permissions you'll check on your own board.

The most common mistake: changing a setting and checking the result without rebooting the board. The serial speed and flow control settings are read at startup. A change you make without rebooting the board doesn't affect the link, and you may accidentally rule out the right fix.

For readers of this article

The sequence here is enough to bring the link up. If you want to see which line in the firmware the decision comes from, the real defaults of the parameters in the source code and field cases, we open them one by one in the course.

See the course: Drone Communication Technologies

Was this article helpful?

Free resourcePixhawk and ArduPilot error message glossaryThe 5 messages in this article and similar ones are in the glossary: what they mean, why they appear, what to check first.