How to Build a Robot
A robot is a loop: read a sensor, decide, move a motor. Build that loop in three stages and you can add behaviour forever without rewiring anything.
The robot loop
Strip away the metal and a robot is one loop repeated thousands of times a second: read the sensors, compare against what you want, adjust the motors. There is no magic in it and no part of it that needs a physics degree.
What makes a robot feel clever is not the hardware, it is the logic you put in the loop. A line follower is a loop with a threshold. An obstacle avoider is a loop with a priority rule. A maze solver is a loop with memory.
Motors need a driver, not a pin
The single most common beginner mistake is wiring a motor straight to a GPIO pin. An Arduino pin can source about 20 mA. A small DC motor wants 100 to 200 mA and will pull the pin's voltage down hard enough to reset the board mid-movement.
The fix is an H-bridge, a chip that takes a low-current logic signal and switches a high-current supply in the direction you ask for. Once that is in place, speed control is just how fast you pulse the enable pin, which is exactly PWM.
Step by step
-
Make one motor turn
Wire a DC motor to an H-bridge, drive it from the Arduino, and add a potentiometer to set speed. Confirm forward and reverse both work before anything else is connected.
-
Mount and drive two wheels
Two motors on a chassis with a caster wheel, each with its own H-bridge channel. Drive both at the same PWM value and check the bot travels straight.
-
Add the first sensor
An infrared reflectance sensor reads a line as a number. Print it to the serial monitor and watch the value change as you move the bot over dark tape.
-
Turn the reading into a decision
Compare the sensor to a threshold and steer: too far left, speed up the right motor. This single if-statement is the entire line-follower.
-
Add obstacle avoidance
Mount an ultrasonic sensor on the front. When it reports something close, override the line-following rule and turn, which is a priority rule layered on the same loop.
What you'll need
| Item | Qty | Why |
|---|---|---|
| Arduino Uno R3 | 1 | More pin headroom than a Nano once you add a second motor channel and sensors. |
| L298N dual H-bridge module | 1 | Two channels, so one module drives both wheels. |
| DC geared motor with wheels | 2 | Geared motors give usable torque at low speed without extra gearing. |
| IR reflectance sensor for line following | 2 | One for each side, so the bot knows which way it is off the line. |
| HC-SR04 ultrasonic sensor | 1 | Front obstacle detection. Mount it where the chassis cannot hide it. |
| 9V battery and barrel jack | 1 | A separate motor supply so motor noise does not brown out the board. |
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.
A servo motor holds whatever angle you tell it. Inside the servo is a small DC motor, a gearbox, and a potentiometer that tells the servo's brain where it currently is. The Arduino sends a pulse 50 times per second; the pulse width (1ms to 2ms) tells the servo the target angle (0° to 180°). The Servo.h library handles all of this for you.
This is the classic "knob turns motor" pattern — used in RC controllers, robotic arms, animatronics, and steering systems. Read a potentiometer, map the 0-1023 range to the servo's 0-180° range, and write it. The user moves the knob and the servo follows. This is the simplest "control loop" you can build.
A parking sensor beeps faster as you get closer to an obstacle, and stays silent past a safe distance. We use 100 cm as the cutoff: anything closer triggers a beep (faster the nearer you are), anything farther is silent. The trick is to change the `delay()` between beeps based on distance: very close = short delay, close = medium delay, safe = no beep. The green LED above the buzzer lights up at safe distances so the driver has a visual cue that the car can keep moving — sound alone is easy to miss in a noisy parking garage. This is the same "map input range to output range" pattern you used for the potentiometer projects, but applied to time.
A real factory counts every box that passes on a conveyor belt using a light sensor or ultrasonic. We are going to build the same logic: an ultrasonic sensor "looks" across a doorway. Whenever something comes within 15 cm (the "very close" 10 cm preset), we increment a counter, flash the green LED, and light up one more tally LED on the row of 5 yellow LEDs (D2, D3, D4, D7, D8) so the kid can SEE how many objects have been counted. The trick is "edge detection" — we only count when the state CHANGES from "no object" to "object", not on every loop while the object is still there.
An Arduino pin is not a power supply. Each GPIO pin is rated to source at most about 20 mA — that is the limit for one pin, and the whole chip together is limited to roughly 40 mA. A 5 V relay coil needs about 50 mA to pull its contacts closed, which is more than twice what a single pin will give. Wire the pin straight to the coil and you do not get a working relay; you get a pin running hot, a supply sagging under USB, and contacts that only just close. The fix is an NPN transistor. The pin drives the transistor's base through a resistor — only a few milliamps — and the transistor switches the full coil current. The relay then does the one thing it is genuinely better at than a transistor: its coil, carrying 50 mA, pulls a set of metal contacts that carry a completely separate circuit. Nothing in that second circuit ever touches the Arduino. That is galvanic isolation, and it is the reason the relay has survived fifty years of transistors — you can switch a high-voltage load from a 5 V logic signal without the two ever meeting.
A real robot arm has at least 4 servos: base rotation, shoulder, elbow, and gripper. The Mega can drive all 4 from independent PWM pins (D9, D10, D11, D12) using the Servo.h library. Each servo is told an angle (0-180°) and the library generates the right PWM signal automatically. The four potentiometers (A0–A3) act as the RC controller — twist them and each servo follows in real time, just like a real robot arm teaching pendant.
Common questions
How do I build a robot for beginners with no experience?
Start with a single motor and a switch, then add a second motor, then add one sensor. Do not begin with a chassis, a sensor array and a line-following algorithm at once, because that is three debugging problems that look like one. Every stage in this guide is a separate project in the simulator, and each one works before you add the next.
How much does a robot cost to build?
A functional line-following robot from parts runs about $45 to $90: $18 to $30 for a 2WD chassis with geared motors, $6 to $12 for the H-bridge, $4 to $8 for the IR sensors, and $10 to $20 for the board and battery. Add an ultrasonic sensor and you are around $100.
Which language should I use to program a robot?
C++ on the Arduino, because that is what the hardware runs. The good news is that the robot logic itself is simple: variables, if-statements and a loop. The simulator in ThinkEdge Learn runs the exact same C++ you would flash to the board, so you can learn the logic with no hardware in front of you.
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.