How to make an Arduino-based robot
What you need
This guide builds the classic first robot: a two wheeled car that drives around and steers away from obstacles. You need:
| Part | Why |
|---|---|
| An Arduino board, such as the UNO R3 | The brain: runs your program and reads the sensor |
| A motor driver board or motor shield | Lets the Arduino control motors that need far more current than its pins can supply |
| Two DC gear motors with wheels | Drive and steer: different speeds on each side turn the robot |
| A caster wheel or skid | Holds up the front or back |
| A chassis | A base plate to mount everything on; a kit, or stiff card or plywood |
| A battery pack for the motors | Motors need more power than the Arduino's USB or 5 V pin can give |
| An ultrasonic distance sensor | Measures how far away obstacles are |
| Jumper wires and a small breadboard | To make connections without soldering |
Robot car kits bundle most of these together and are a cheap way to start. The UNO R3 is a common choice: Arduino's documentation lists its ATmega328P chip, 16 MHz clock, 14 digital pins (6 of which can do PWM) and 6 analogue inputs, which is plenty for this project. Not sure which board to pick? Try this question:
Pick the Uno, ESP32, Pico or Raspberry Pi for each project, with a spec table to read (ChoiceQuiz).
Press Mark to have your answer marked in the Micro Controllers course.
Why the robot needs a motor driver
An Arduino's pins are for signals, not power. A small DC motor can draw hundreds of milliamps, far more than a pin can safely supply, and as it spins down it can send voltage spikes back into the circuit. Connecting a motor straight to a pin can damage the board. A motor driver sits in between: the Arduino sends it small control signals, and it switches the big current from the battery to the motors. Arduino's own Motor Shield Rev3, for example, uses an L298 chip, which Arduino describes as a dual full bridge driver designed for inductive loads such as motors, and it drives two DC motors independently.
Inside, a driver uses transistors as switches, arranged in an H-bridge so the current can flow either way through the motor. That is how one board makes a motor go forwards or backwards.
Step 1: build the chassis
- Fix the two motors on either side at the back of the chassis and push the wheels on.
- Fit the caster at the front so the robot sits level.
- Mount the Arduino, the motor driver and the battery pack. Keep the battery low and central so the robot doesn't tip.
- Mount the ultrasonic sensor at the front, facing forwards.
Step 2: wire it up
- Motors to the driver: each motor's two wires go to one output channel of the driver.
- Battery to the driver: the motor battery connects to the driver's power input, not to the Arduino's 5 V pin.
- Common ground: join the driver's ground (GND) to the Arduino's GND. Without a shared ground, the control signals have no reference and nothing works.
- Control pins: each motor needs two direction pins and one speed pin. The speed pins must be PWM pins (marked ~ on the board).
- Sensor: the ultrasonic sensor needs 5 V, GND, a trigger pin and an echo pin.
The sketch below uses pins 5 and 6 for speed, 2, 4, 12 and 13 for direction, and 7 and 8 for the sensor. Check your driver's own pin labels; many boards call the inputs IN1 to IN4 and the speed pins ENA and ENB.
Step 3: control speed with PWM
An Arduino pin can only be fully on or fully off. To run a motor at part speed, the speed pin switches on and off very quickly, called pulse width modulation (PWM). analogWrite(pin, value) sets how much of each cycle the pin is on, from 0 (always off) to 255 (always on). The motor responds to the average.
So analogWrite(5, 191) keeps the pin on 191 ÷ 255 ≈ 75% of the time. With a 6 V motor supply through the driver, the motor gets about 0.75 × 6 = 4.5 V on average and runs at roughly three quarters speed. Try one: this question asks you to work out a PWM value or its average voltage.
Type an analogWrite value for a duty, or the average voltage for a value (TypedAnswer), random rounds.
Press Mark to have your answer marked in the Micro Controllers course.
Step 4: measure distance with the ultrasonic sensor
The sensor sends out a burst of ultrasound and times the echo. Sound travels at about 343 m/s in air at 20 °C (OpenStax), which is 0.0343 cm every microsecond. The sound goes to the obstacle and back, so halve the total:
An echo that takes 1,166 µs means the obstacle is 1,166 × 0.0343 ÷ 2 ≈ 20 cm away. Our sound waves guide explains why this works.
Step 5: the code
Upload this sketch with the Arduino IDE. It drives forwards, and when something comes within 20 cm, it backs up and spins right before carrying on.
const int TRIG = 7, ECHO = 8;
const int ENA = 5, IN1 = 4, IN2 = 2; // left motor
const int ENB = 6, IN3 = 12, IN4 = 13; // right motor
void setup() {
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
int pins[] = {ENA, IN1, IN2, ENB, IN3, IN4};
for (int p : pins) pinMode(p, OUTPUT);
}
// speed from -255 (full reverse) to 255 (full forwards)
void drive(int left, int right) {
digitalWrite(IN1, left > 0);
digitalWrite(IN2, left < 0);
digitalWrite(IN3, right > 0);
digitalWrite(IN4, right < 0);
analogWrite(ENA, abs(left));
analogWrite(ENB, abs(right));
}
float distanceCm() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH); // a 10 microsecond trigger pulse
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long us = pulseIn(ECHO, HIGH, 30000); // 0 if no echo within 30 ms
return us * 0.0343 / 2;
}
void loop() {
float d = distanceCm();
if (d > 0 && d < 20) { // something within 20 cm
drive(-150, -150); // back up
delay(300);
drive(150, -150); // spin right
delay(350);
} else {
drive(200, 200); // straight ahead
}
}To turn, the robot runs one wheel forwards and the other backwards, called skid steering. The 30 ms timeout stops the program waiting forever if no echo comes back; at 0.0343 cm per µs it covers obstacles up to about 5 m. Test with the wheels off the ground first, and swap a motor's two wires if it spins the wrong way. You can practise writing and checking code like this in our Code Editor before uploading it.
Ideas to take it further
- Line follower: add infrared sensors underneath and steer to keep a black line between them.
- Look around: mount the distance sensor on a servo and check left and right before choosing a way to turn.
- Remote control: add a Bluetooth module, or use an ESP32 board with built in Wi-Fi and drive it from your phone.
- Smoother driving: ramp the speed up and down instead of jumping straight to full power.
- Add AI: pair it with a Raspberry Pi and a camera to recognise objects, as in our guide to Raspberry Pi machine learning.
The Microcontrollers course covers each building block, from blinking an LED and reading buttons to PWM, sensors and Wi-Fi on the ESP32.
Common mistakes
- Powering motors from the Arduino. Use a motor driver and a separate battery for the motors.
- Forgetting the common ground. The Arduino and the driver must share GND.
- Using a non-PWM pin for speed. On an UNO, only pins 3, 5, 6, 9, 10 and 11 support analogWrite.
- Flat batteries. Weak batteries make motors stall and can reset the Arduino. Charge or replace them first when things act strangely.
- Forgetting to halve the echo time. The sound travels there and back.
- Motors fighting each other. If the robot spins when it should go straight, one motor is wired backwards; swap its two wires.
Common questions
What do I need to make an Arduino robot?
Can I connect a motor directly to an Arduino?
Which Arduino is best for a robot?
How does an Arduino control motor speed?
What programming language does Arduino use?
How long does it take to build an Arduino robot?
Revision cards
Read the front, say your answer, then flip to check.
Where to go next
- Solve it
Code Editor
Write and run code right in your browser
Open the tool - Learn it
Micro Controllers
Arduino, ESP32 and programming hardware at the lowest level
Start the course