Obstacle-avoiding robot with Arduino | Basic guide
Build your first robot that moves on its own and avoids bumping into things, with Arduino and an ultrasonic sensor. Real hardware, the logic explained, and the AI writes the code.
- Level
- Basic
- Time
- One afternoon (2-3 h)
- Published
| Part | Qty | Approx. price |
|---|---|---|
| Arduino Uno (or clone) | 1 | 8-20 € |
| HC-SR04 ultrasonic sensor | 1 | 2-3 € |
| 2-wheel chassis with DC gear motors | 1 | 8-12 € |
| L298N motor driver | 1 | 3-4 € |
| 4xAA battery holder or 6-7.4V pack | 1 | 3-5 € |
| Dupont wires and zip ties | 1 | 3 € |
How it works
The Arduino can't drive the motors directly: its pins give milliamps and a motor wants hundreds. That's why the L298N sits in between, taking current from the batteries and using the Arduino only as a switch. The HC-SR04 measures distance by sending a sound pulse and timing the echo. Echo comes back fast, something's close: turn. Slow: clear, move forward.
The build
Where do you start in robotics without getting frustrated? With a robot that does ONE thing well: move and not crash. It’s the “hello world” of hardware that moves, and once you watch it dodge your foot on its own, you’re hooked.
The build, no mystery
Think in three blocks: the one that decides (Arduino), the one that pushes (motors + driver) and the one that sees (sensor). The Arduino is the brain, but a brain with no muscle: so you never wire a motor to its pins. That’s mistake number one and it fries the board.
| Signal | Arduino | Goes to |
|---|---|---|
| Trig | D9 | HC-SR04 |
| Echo | D10 | HC-SR04 |
| Motors | D5 D6 D7 D8 | L298N IN1-IN4 |
Safety
Batteries power the motors, not the Arduino. Share only ground (GND). If the robot resets itself when the motors start, you’re stealing current from the brain: separate the supplies.
Sample guide. The real version is built by the guide-builder agent with verified voltages and pins.
The software (the AI writes it)
The firmware does three things: read distance, decide (forward or turn), send the order to the driver. A 20-line loop. Ask the AI with this prompt: 'Arduino code for a robot with HC-SR04 on trig 9 echo 10 and L298N driver on pins 5,6,7,8; if distance is under 20 cm turn, else go forward.' Check the pins match your build.