Introduction
Building a drone swarm is no longer a concept reserved for large military or research institutions. With affordable hardware like the Raspberry Pi Pico W, hobbyists and developers can experiment with coordinated multi-agent systems using MicroPython.
In this article, we'll explore:
The hardware needed for a swarm of drones.
How to set up communication using Wi-Fi (UDP).
Leader-follower swarm logic.
Example code for both leader and follower drones.
Hardware Requirements
Each drone in the swarm should have:
Raspberry Pi Pico W
Motor controller (ESC) for brushless motors
EDF or propeller with BLDC motor
IMU (e.g., MPU6050)
Optional: GPS module (e.g., M10Q), barometer (e.g., BMP280), or airspeed sensor
LiPo Battery (3.7V or higher)
Frame (custom 3D-printed or carbon fiber)
Note: This article assumes that basic drone flight control (ESC control, stabilization) is implemented or handled by another module. Here, we focus purely on swarm coordination logic using MicroPython.
Communication via UDP Broadcast (Pico W)
We'll use UDP broadcast messages over Wi-Fi for communication between drones.
Network Setup
Each Pico W connects to the same Wi-Fi network (or one acts as access point). They communicate by broadcasting position data (X, Y, Z) and commands.
MicroPython Firmware
Install MicroPython firmware on each Raspberry Pi Pico W.
Flash it from https://micropython.org/download/rp2-pico-w/
Then, use Thonny or rshell to upload the scripts below.
1️⃣ Leader Drone Code
This drone sends its current position and a command every 0.5 seconds.
# leader.py
import network
import socket
import time
SSID = 'YourWiFi'
PASSWORD = 'YourPassword'
PORT = 5005
BROADCAST_IP = '255.255.255.255'
def connect():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
print("Connecting...")
time.sleep(1)
print("Connected to", SSID)
print("IP:", wlan.ifconfig())
def send_command():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
while True:
# Simulated coordinates (you can read from sensors)
x, y, z = 10, 20, 5
msg = f"{x},{y},{z},FOLLOW"
s.sendto(msg.encode(), (BROADCAST_IP, PORT))
print("Sent:", msg)
time.sleep(0.5)
connect()
send_command()
2️⃣ Follower Drone Code
Each follower receives the command and adjusts its own course to match or follow the leader.
# follower.py
import network
import socket
import time
SSID = 'YourWiFi'
PASSWORD = 'YourPassword'
PORT = 5005
def connect():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
print("Connecting...")
time.sleep(1)
print("Connected to", SSID)
print("IP:", wlan.ifconfig())
def listen():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('0.0.0.0', PORT))
while True:
data, addr = s.recvfrom(1024)
msg = data.decode()
print("Received:", msg)
try:
x, y, z, cmd = msg.split(',')
# Simulated response: just print for now
print(f"Following leader to X={x}, Y={y}, Z={z}")
# Here you would command motors or PID loop
except Exception as e:
print("Error parsing message:", e)
connect()
listen()
Behavior Logic – Follow-the-Leader
To expand this swarm logic, implement simple behaviors like:
Maintain fixed offset from leader (dx
, dy
)
Collision avoidance (future)
Priority rules (e.g., time delay between followers)
# Within follower.py
offset_x = -5 # stay 5 meters behind
target_x = float(x) + offset_x
# PID control towards (target_x, y, z)
Visualization (Optional)
Send drone telemetry to a PC or mobile dashboard using MQTT or WebSocket for real-time visualization.
Security Consideration
Future Expansions
Use ESP-NOW (ESP32) for mesh network
Add collision avoidance using ultrasonic sensors or LiDAR
Use AI vision system (YOLOv8 on Jetson Nano or Pi 5) for target tracking
Switch from Pico W to ESP32-C3 for faster real-time messaging with BLE + Wi-Fi
Conclusion
With just a few Raspberry Pi Pico W boards and MicroPython, you can create a basic drone swarm that shares data, coordinates movement, and follows a central leader.
This is just the foundation — from here, you can add AI, object detection, GPS-based positioning, and more advanced coordination algorithms.
