T ThinkEdge Learn

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.

ai swarm swarm robotics ai swarm intelligence
AI and Electronics · Flocking rules reduced to sensor comparisons

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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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

ItemQtyWhy
Arduino Nano3Small enough to build three of them, and each one runs the same three rules with its own address.
HC-SR04 ultrasonic sensor3 per robotFront, left and right, so the robot can tell which side a neighbour is on rather than only how far it is.
SG90 micro servo1 per robotPoints one sensor around to find neighbours cheaply, at the cost of a slower cycle.
L298N dual H-bridge module1 per robotTwo motor channels, so a robot can drive and steer at the same time rather than only one or the other.
LED bar graph module1 per robotShows the distance reading, so you can see which rule is firing and why.
9V battery and barrel jack1 per robotA 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.

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

Physical AI: Intelligence That Has to ActPhysical AI is intelligence that drives a motor, not a cursor. Here is how models, controllers and feedback split up, and how to build the loop safely. Edge AI: Decisions Made on the DeviceEdge AI runs the decision on the device instead of in the cloud. Here is what that buys you in latency and privacy, and what a 32 KB microcontroller can really do. What an AI Agent Actually IsAn AI agent is a loop of model calls, tools and memory. Learn how that loop maps onto the sense, decide, act cycle of a robot you can build this term. Robotics and Autonomous Systems: The Four Sub-AreasRobotics and autonomous systems split into perception, planning, control and SLAM. Here is what each needs, and which parts an Arduino can reach. Mesh Networking: How Nodes Relay Data for Each OtherA mesh network lets nodes relay data for each other. How flooding and hop limits work, what mesh costs in latency, and when a simple star is better.

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.