Home Contents Start Prev 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Next

Phase 6: Interfacing Pi and the Arduino

Roadmap

The following is a plan of where I'm going:

  • Serial UART mechanism
  • Determine baud rate. I suspect it needs to be high
  • Create a communication protocol, primarily from Pi->Arduino but maybe two way
  • Physically wire this up on kupe
  • Pi should send steerage commands to Arduino
  • Compass readings
  • Where to interface the compass (Pi or Arduino)
  • Get it working on kupe

Serial UART mechanism

Moana used the Pi as a smart sensor but overall control stayed with the Arduino. Kupe is different. Because I need to perform more complex mathematical computations (matrix math, sine and cosine etc), the heavy lifting and control will reside in the Pi. The arduino will become a layer over the hardware and will be under the control of the Pi.

The ardiono is ideally suited to sensor interfacing and I like the idea of it passing sensor data to the Pi and responding to commands sent from the Pi. This requires a two way communication channel and associated traffic management. So, in the first implementation, I intend to feed any sensor information directly into the Pi and the Pi will send locomotion commands to the arduino. Ideally we want an acknowledgment of receiving the command back to the Pi, but essentially this is a one-way mechansm Pi->Arduino. I can extend this later if I need to.

Although there are many different mechanism I could use, a simple serial interface is perhaps the easiest and most extensible mechanism. I used such an interface on moana to send commands from my laptop and it was relatively simple to add extra commands as needed. If all the sensor information resides on the Pi, this communication should only need to be locomotion commands so it will be easy to create a simple communication protocol.

Serial Connections

The Arduino Mega has 4 UARTs so we have a good choice of appropriate serial pins we can use. The Pi has one UART which can be accessed via GPIO pins 14 and 15.

NOTE: Due to the voltage differences bewtween the devices, we need to ensure 5v output from arduino steps down to 3v at Pi end. Arduino Tx pin is main offender.

Pi Arduino
TX GPIO14 Rx pin 0
RX GPIO15 Tx pin 1

The UART on the Pi is is normally used for the bluetooth module so this needs disabling.

sudo raspi-config

Interface Options → Serial → No (for shell and kernel messages) → Yes (for serial port hardware enabled).

sudo reboot

Console use of the serial port can be disabled:
sudo nano /boot/firmware/config.txt
enable_uart=1

We need pyserial library:

pip install pyserial

Outline of code that sends text over the Pi serial connection (and expects an acknowledgement)

import serial
import time

# Configure the serial port
ser = serial.Serial(
    port='/dev/ttyS0',  # Default UART port on Raspberry Pi 5
    baudrate=9600,      # Match Arduino's baud rate
    timeout=1
)

try:
    while True:
        # Send data to Arduino
        ser.write(b'Hello Arduino!\n')

        # Read response from Arduino
        response = ser.readline().decode('utf-8').strip()
        if response:
            print(f"Raspberry Pi received: {response}")

        time.sleep(1)  # Delay between messages

except KeyboardInterrupt:
    print("Exiting...")
    ser.close()


Sept 2026


Home Contents Start Prev 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Next