Swarm robotics — coordinating multiple Arduino-based robots to work together as a collective — is one of the most exciting frontiers in DIY robotics. Inspired by the collective behaviour of ants, bees, and fish schools, swarm robots can achieve tasks that a single robot cannot: area coverage, object manipulation, and fault-tolerant operation where the failure of one unit doesn’t stop the mission. This guide covers the communication protocols, hardware requirements, and Arduino code for building your first multi-robot swarm system.
Table of Contents
- Swarm Robotics Concepts
- Communication Options for Swarm Robots
- Hardware Design for Each Robot Unit
- ESP-NOW Protocol for Swarm Communication
- Swarm Algorithms: Flocking and Coverage
- Complete Arduino/ESP32 Swarm Code
- Recommended Products
- Frequently Asked Questions
Swarm Robotics Concepts
Swarm robotics is characterised by three fundamental principles:
- Decentralisation: No single robot is the “master”. All robots are equal peers that react to local information — proximity to neighbours, obstacle detection, and received messages.
- Local interaction: Each robot only communicates with its nearest neighbours, not with all robots in the swarm. Global coordination emerges from these local interactions.
- Scalability: Adding more robots to the swarm should improve performance without requiring changes to the existing robots’ firmware.
Classic swarm behaviours include:
- Aggregation: Robots move toward each other until clustered
- Dispersion: Robots spread out to maximise coverage area
- Flocking: Robots move in coordinated direction while maintaining separation (Boids algorithm)
- Foraging: Robots search an area and return “food” (objects) to a nest location
- Task allocation: Robots self-assign to different tasks based on need and capability
Communication Options for Swarm Robots
| Protocol | Range | Latency | Cost | Best For |
|---|---|---|---|---|
| ESP-NOW (ESP32) | 100–200m | <5ms | ₹250/unit | Indoor swarms, fast coordination |
| nRF24L01 (2.4 GHz) | 100m | <10ms | ₹80/unit | Arduino-based swarms |
| IR (Infrared) | 0.5–2m | <1ms | ₹10/unit | Local neighbour detection |
| Bluetooth (BLE) | 10–30m | 10–100ms | ₹200/unit | Centralised monitoring |
| LoRa | 1–5 km | 50–500ms | ₹400/unit | Outdoor search/coverage |
ESP-NOW is the recommended protocol for swarm robotics projects because it provides low-latency peer-to-peer WiFi communication without requiring a router or access point. Each ESP32 can broadcast to all peers simultaneously or send targeted messages to specific MAC addresses — ideal for swarm coordination where broadcast is the dominant communication pattern.
Hardware Design for Each Robot Unit
A minimal swarm robot unit for a 3-bot educational swarm:
- ESP32 DevKit V1 (WiFi + dual-core = communication + control)
- L298N motor driver or TB6612FNG dual H-bridge
- 2× DC gear motors with wheels
- HC-SR04 ultrasonic sensor (front obstacle detection)
- 2× IR proximity sensors (left/right neighbour detection)
- LiPo 7.4V 1000mAh battery + LM2596 5V step-down for ESP32
- Chassis: 3D-printed or acrylic cut frame, ~15×10 cm
Estimated cost per robot: ₹800–₹1,200. A 3-robot swarm can be built for ₹2,400–₹3,600 — a very accessible entry point for swarm robotics experimentation.
ESP-NOW Protocol for Swarm Communication
ESP-NOW allows ESP32s to communicate directly without WiFi infrastructure at up to 250 bytes per packet and under 5 ms latency. Setting up ESP-NOW for a swarm:
#include <esp_now.h>
#include <WiFi.h>
// Swarm message structure
typedef struct {
uint8_t robotID; // Sender's robot ID (0-7)
int16_t posX; // Estimated X position (cm)
int16_t posY; // Estimated Y position (cm)
uint8_t state; // 0=idle, 1=moving, 2=obstacle, 3=task
uint8_t heading; // 0-359 degrees
uint8_t taskID; // Current task assignment
} SwarmMsg;
// Broadcast address: sends to ALL ESP32 peers simultaneously
uint8_t broadcastAddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
SwarmMsg myState;
SwarmMsg neighbourStates[8]; // Track up to 8 neighbours
void OnDataSent(const uint8_t* mac, esp_now_send_status_t status) {
// Message sent callback — use for reliability tracking
}
void OnDataRecv(const uint8_t* mac, const uint8_t* data, int len) {
SwarmMsg received;
memcpy(&received, data, sizeof(received));
// Store neighbour state
if (received.robotID < 8) {
neighbourStates[received.robotID] = received;
}
}
void initESPNow() {
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW init failed!");
return;
}
esp_now_register_send_cb(OnDataSent);
esp_now_register_recv_cb(OnDataRecv);
// Register broadcast peer
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, broadcastAddr, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
esp_now_add_peer(&peerInfo);
}
void broadcastState() {
esp_now_send(broadcastAddr, (uint8_t*)&myState, sizeof(myState));
}
Swarm Algorithms: Flocking and Coverage
Reynolds Boids Algorithm (Flocking)
The Boids algorithm creates natural-looking flock movement from three simple rules applied to each robot:
- Separation: Steer away from nearby robots to avoid crowding
- Alignment: Steer toward the average heading of nearby robots
- Cohesion: Steer toward the average position of nearby robots
Each force vector is calculated from the neighbour states received via ESP-NOW, weighted, and summed to produce a target heading for the robot’s motors.
Random Walk Coverage
For area coverage (e.g., floor sweeping, mine detection), each robot follows a modified random walk that ensures no region is revisited: move straight until an obstacle is detected, then turn by a random angle. Coordination via ESP-NOW ensures robots spread out by repelling from known robot positions.
Complete Arduino/ESP32 Swarm Code
#include <esp_now.h>
#include <WiFi.h>
// Motor control pins
#define MOTOR_L_FWD 25
#define MOTOR_L_BWD 26
#define MOTOR_R_FWD 27
#define MOTOR_R_BWD 14
#define MOTOR_SPEED 150 // 0-255 PWM
// Ultrasonic sensor
#define TRIG_PIN 32
#define ECHO_PIN 33
// Configuration
#define ROBOT_ID 0 // Change for each robot: 0, 1, 2
#define OBSTACLE_CM 20 // Stop if obstacle within 20 cm
#define BROADCAST_MS 200 // Broadcast state every 200ms
typedef struct {
uint8_t robotID;
int16_t posX, posY;
uint8_t state;
uint8_t heading;
} SwarmMsg;
uint8_t broadcastAddr[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
SwarmMsg myState = {ROBOT_ID, 0, 0, 0, 0};
SwarmMsg peers[8];
void setMotors(int leftSpeed, int rightSpeed) {
// Positive = forward, negative = backward
analogWrite(MOTOR_L_FWD, max(0, leftSpeed));
analogWrite(MOTOR_L_BWD, max(0, -leftSpeed));
analogWrite(MOTOR_R_FWD, max(0, rightSpeed));
analogWrite(MOTOR_R_BWD, max(0, -rightSpeed));
}
float getDistance() {
digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
return pulseIn(ECHO_PIN, HIGH) * 0.0343 / 2.0;
}
void OnDataRecv(const uint8_t* mac, const uint8_t* data, int len) {
SwarmMsg msg;
memcpy(&msg, data, sizeof(msg));
if (msg.robotID < 8) peers[msg.robotID] = msg;
}
void setup() {
Serial.begin(115200);
// Motor pins
pinMode(MOTOR_L_FWD, OUTPUT); pinMode(MOTOR_L_BWD, OUTPUT);
pinMode(MOTOR_R_FWD, OUTPUT); pinMode(MOTOR_R_BWD, OUTPUT);
// Ultrasonic
pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT);
// ESP-NOW
WiFi.mode(WIFI_STA);
esp_now_init();
esp_now_register_recv_cb(OnDataRecv);
esp_now_peer_info_t peer = {};
memcpy(peer.peer_addr, broadcastAddr, 6);
esp_now_add_peer(&peer);
}
void loop() {
static unsigned long lastBroadcast = 0;
static int turnDir = 1;
static int moveCount = 0;
float dist = getDistance();
if (dist > 0 && dist < OBSTACLE_CM) {
// Obstacle detected — stop and turn
setMotors(0, 0);
delay(200);
// Turn away from obstacle
setMotors(-MOTOR_SPEED * turnDir, MOTOR_SPEED * turnDir);
delay(random(300, 700));
turnDir *= -1; // Alternate turn direction
myState.state = 2;
} else {
// No obstacle — move forward
setMotors(MOTOR_SPEED, MOTOR_SPEED);
myState.state = 1;
moveCount++;
// Random direction change every 50-100 moves (avoid straight-line bias)
if (moveCount > random(50, 100)) {
setMotors(-MOTOR_SPEED, MOTOR_SPEED);
delay(random(200, 500));
moveCount = 0;
}
}
// Broadcast state to swarm
if (millis() - lastBroadcast > BROADCAST_MS) {
lastBroadcast = millis();
esp_now_send(broadcastAddr, (uint8_t*)&myState, sizeof(myState));
}
delay(20);
}
Recommended Products
Waveshare General Driver Board for Robots (ESP32-based)
Waveshare’s robot driver board integrates an ESP32, dual H-bridge motor drivers, servo headers, and sensor ports on a single PCB — the perfect foundation for a swarm robot unit. The onboard ESP32 handles both motor control and ESP-NOW swarm communication without any additional hardware.
ACEBOTT ESP8266 Quadruped Bionic Spider Robot Kit
This pre-built spider robot kit is an excellent starting point for swarm robotics — add ESP-NOW communication to multiple units and programme them for coordinated walking behaviours. The quadruped form factor avoids wheel slippage issues on uneven surfaces common in swarm deployment environments.
Waveshare AlphaBot2 Robot Building Kit for Raspberry Pi
The AlphaBot2 chassis includes a motor driver, IR sensors, and a camera interface — a complete mobile robot platform for swarm experiments. Add a Raspberry Pi and use ROS2 with ESP-NOW gateways for a hybrid swarm where Pi units handle high-level coordination and ESP32 units handle reactive behaviours.
Waveshare ESP32 Servo Driver Expansion Board
For servo-based swarm robots (hexapods, wheeled platforms with servo steering), this ESP32-based 16-channel servo driver provides enough outputs for complex motion while maintaining WiFi connectivity for ESP-NOW swarm communication. Built-in WiFi eliminates the need for external communication modules.
Frequently Asked Questions
How many robots does a swarm need to be useful?
Swarm behaviours start to emerge with as few as 3 robots. For research purposes, meaningful statistical results require 10+ units. For educational demonstrations, 3–5 robots clearly show collective behaviour. The power of swarm robotics — redundancy, parallelism, and emergent coordination — becomes obvious with 5+ units.
Can Arduino Uno robots form a swarm without ESP32?
Yes — use nRF24L01 2.4 GHz radio modules (₹80 each) with Arduino Uno. The nRF24L01 supports multicast (broadcast to all units) and point-to-point messaging. Latency is higher than ESP-NOW (~10 ms vs ~5 ms) but adequate for swarm coordination where decisions are made at 100 ms timescales.
How do swarm robots localise themselves without GPS?
Indoor localisation options: (1) Wheel odometry — count motor encoder pulses to estimate position from last known location. (2) IR landmark beacons — fixed beacons at known positions emit IR codes that robots detect and triangulate. (3) UWB (Ultra-Wideband) ranging — modules like the DWM1000 provide 10–30 cm ranging accuracy. For outdoor swarms, GPS modules (NEO-6M, ~₹350) provide 2–5 metre accuracy.
Can swarm robots avoid deadlock when multiple bots block each other?
Deadlock avoidance in robot swarms uses randomness as a resolution strategy: if a robot detects a peer blocking its path and waits longer than a timeout (e.g., 2 seconds), it turns by a random angle and tries a new direction. Pure deterministic avoidance causes symmetric deadlocks; randomness breaks symmetry reliably.
What is the difference between multi-robot systems and swarm robotics?
Multi-robot systems typically have a central coordinator that assigns tasks and monitors all robots. Swarm robotics is fully decentralised — each robot makes decisions based only on local sensor data and neighbour communication, with no global coordinator. Swarms are more fault-tolerant (no single point of failure) but harder to programme for complex tasks.
2 comments
Forrest
Hi.
I am very interested in swarm robot cars. Do you have any videos of them running or other demonstrations? Any information would be greatly appreciated!
Thank you in advance!
Shubham S
Hi Forrest,
Thank you for your interest in swarm robot cars!
You can check out this demonstration video here: https://www.youtube.com/watch?v=llM_cTODA28 (Copy Link)
It showcases how the swarm robots operate in real-time. Let us know if you’d like more details or have any questions-we’d be happy to help!