AI Swarm: Many Small Robots, Three Rules
Flocking, herding and robot swarms all come from the same three rules applied to each individual separately. On an Arduino, each rule is three lines of C.
The three rules, and nothing else
In 1987 Craig Reynolds wrote down three rules for flocking, and a flock still appeared. Separation, so birds keep a personal space. Alignment, so birds match the heading of their neighbours. Cohesion, so birds steer towards the middle of their neighbours. No bird knows where the flock is going and no bird talks to the whole flock. The pattern comes out of the local interactions.
Every rule is a distance comparison, which means every rule is three lines of C. If the nearest neighbour is closer than 30 cm, move away. If the average heading of my neighbours differs from mine by more than 20 degrees, turn towards it. If I am on the edge of the group, steer inwards. That is a swarm, and an Arduino at 16 MHz is fast enough to run one.
Why local rules beat a central plan
A central controller that plans for every robot has to know where every robot is, every cycle. Over a shared radio link that is one message per robot per cycle, and the traffic grows with the square of the number of robots, so any lost packet corrupts the plan for everybody. Local rules need each robot to know only its own few neighbours, so a dropped message damages one robot's view instead of the whole flock.
The second reason is physical. A swarm of five small robots can cover a space that one big robot cannot, and it keeps working when two members fail. Neither property needs intelligence. That is the useful lesson for a classroom: some of the most impressive-looking behaviours are much simpler than they appear once you draw them out as rules.
Step by step
-
Simulate three boids on a screen first
Twenty lines of Python or a Scratch scene, moving three dots with the three rules. Watching the dots clump and separate teaches the rules faster than any diagram.
-
Build one rule on one robot
Separation alone: mount one ultrasonic sensor forward and one to each side, and turn away from whichever side is closest. That is a swarm member with no neighbours at all.
-
Add the neighbour sense
Use a second robot, or an HC-SR04 on a rotating servo, to measure how far the nearest neighbour is. Alignment becomes: if the neighbour is ahead of me, match its heading.
-
Keep the loop tight
An HC-SR04 needs about 6 ms of sound flight time for a 1 m round trip and up to 25 ms at its 4 m limit. Read the sensors in sequence and keep the whole loop under 50 ms, or the robot reacts to a world that has already moved on.
-
Exchange readings over serial
Two Arduinos on a 9600 baud link can share a distance and a heading each cycle, which is about 15 bytes per robot per cycle. The swarm gets a shared view without a central computer.
-
Scale it and measure
Put three to five robots on a table, record how long they take to regroup after being scattered, and compare that with a single robot attempting the same task.
What you'll need
| Item | Qty | Why |
|---|---|---|
| Arduino Nano | 3 | Small enough to build three of them, and each one runs the same three rules with its own address. |
| HC-SR04 ultrasonic sensor | 3 per robot | Front, left and right, so the robot can tell which side a neighbour is on rather than only how far it is. |
| SG90 micro servo | 1 per robot | Points one sensor around to find neighbours cheaply, at the cost of a slower cycle. |
| L298N dual H-bridge module | 1 per robot | Two motor channels, so a robot can drive and steer at the same time rather than only one or the other. |
| LED bar graph module | 1 per robot | Shows the distance reading, so you can see which rule is firing and why. |
| 9V battery and barrel jack | 1 per robot | A separate motor supply, so motor noise does not reset the microcontroller mid-decision. |
Practise it before you build it
Every step above has a matching circuit in the ThinkEdge Learn simulator. The simulator runs a real
Arduino sketch against a modified-nodal-analysis solver, so digitalWrite actually drives the
LED on screen and the readings change when you change the wiring — no hardware, no burnt components.
Mount an HC-SR04 ultrasonic sensor on top of a servo. Sweep the servo from 0° to 180° and back, and at each step measure the distance. Print the results to the Serial Monitor as "angle,distance" pairs. Open the Serial Plotter in the Arduino IDE to see a live radar-like graph of the room. With Processing on your computer, you can build a full visual radar.
The Mega has 4 hardware serial ports (Serial, Serial1, Serial2, Serial3). That means you can talk to 4 different devices at the SAME time — like a GPS, a Bluetooth module, an XBee radio, and the USB. Use 3 of those ports to read 3 different sensors in parallel and stream their data to the Serial Monitor as a CSV row every second. This is the foundation of every real Arduino data logger.
In factories, conveyor belts carry products past a sensor that counts them. A simple "break-beam" sensor (a light source + photodetector) registers each object as it passes. In this project, you'll simulate the conveyor with your hand: every time you press the button (mimicking a product passing the sensor), the counter increments and the LED blinks. Add a buzzer to beep once for every 10 items. This is the same logic as a people counter at a door, a coin counter at a bank, or a bottle counter on a bottling line.
Real smart fans use TWO sensors: temperature (to know WHEN to turn on) and motion or distance (to know IF someone is present). If it's hot AND someone is within 100 cm, the fan turns on. This avoids the silly situation of a fan blowing in an empty room. We use the DHT11 for temperature, the ultrasonic for distance, and a real DC fan motor for the output.
A 10-segment LED bar graph is the easiest way to visualize an analog input: spin a potentiometer and 1, 2, 3 ... 10 LEDs light up in sequence. This is a "VU meter" — the kind you see on stereo equipment. The Mega is ideal because it has 10 free digital pins without stealing any from the analog or serial pins. The same code can be extended to 20, 30, or 100 LEDs on a Mega.
A real traffic light runs a state machine: GREEN (cars go) → YELLOW (slow down) → RED (cars stop) → PEDESTRIAN WALK (cross the road) → RED → GREEN. The Mega has enough pins to build a full intersection: 3 LEDs for cars in one direction, 3 LEDs for cars in the other, 2 LEDs for the pedestrian signal, and a button to request to cross. Use millis() for non-blocking timing so the button check is responsive.
Common questions
How many robots do you need for a swarm?
Three is the minimum that shows the effect, because with two you cannot tell alignment from coincidence. Four or five makes the pattern obvious on a table and costs about $25 to $35 per robot in parts. Real swarms use hundreds or thousands, but the behaviour appears at small numbers if the rules are right.
Is swarm robotics the same as swarm intelligence?
Yes, the two phrases are used for the same idea, and the second is the broader one. Swarm intelligence describes any system where many simple agents solve a problem together, which includes ants finding short paths through a maze and a set of robots sharing a map. The robotics version is the one you can build with sensors, motors and about $30 per robot.
How quickly must a swarm robot react?
Faster than its own speed. A robot moving at 0.2 m/s covers 2 cm in 100 ms, so a sensor loop slower than that lets it drive into whatever it was avoiding. That is why the ultrasonic reading has to fit inside the loop, and why the servo sweep approach trades cycle time for the number of sensors you can afford.
Related guides
Try the full simulator with your class
103 guided circuit projects, 21 lessons and an in-browser Arduino simulator. The first 15 projects are free, no account needed. NCERT Science mapping on 47 projects, with Arabic and English copy in the regional builds.