Introduction: 🤖 Arduino Wall Following Robot Tutorial: Build a Maze-Solving Robot

About: I do stuff sometimes.

📚 Complete DIY Guide - Step by Step

So you want to build an autonomous robot that can solve mazes by itself? Cool. This complete guide shows you how to build a DIY wall following and maze-solving robot using Arduino and parts you can get anywhere.

This Arduino robot project uses three ultrasonic sensors to "see" walls and makes autonomous decisions about where to go next. It's not fancy AI or anything .. just some smart if-statements that work surprisingly well. The wall following algorithm detects obstacles ahead, checks which direction has more space, and navigates accordingly.

No robotics experience? No problem. If you can follow steps, you can build this.

Supplies

Shopping List for Your DIY Arduino Robot

Get these 8 items - that's it!

  1. Arduino UNO: The brain that makes all the decisions
  2. L298N Motor Driver: Red board that controls motor power
  3. 3× HC-SR04 Ultrasonic Sensors: Blue boards with "eyes" that detect walls
  4. 2× DC Motors + Wheels: Makes your robot move
  5. 12V Battery Pack: Powers everything
  6. Jumper Wires: Colorful pack that connects all components
  7. Robot Chassis: Pick one DIY method (see chassis options below)
  8. Zip Ties: Wraps everything for mounting

Bonus - LEDs: Optional - light up your robot for extra fun and style!

Tools You'll Need

  1. Battery Charger: Keeps your 12V battery pack charged
  2. Acrylic Sheet: For building the robot chassis (if using Method 1)
  3. Soldering Iron: (Optional) For securing wire connections
  4. Wire Cutter: (Optional) Makes cutting wires cleaner

Step 1: Create the DIY Robot Chassis

Pick the chassis method that works best for you, both options work great!

⭐ Option A: Acrylic Sheet (Recommended)

  1. What you need: 3mm acrylic sheet, approximately 25cm × 15cm
  2. Where to get it: Local makerspace, laser cutting service, or hardware store
  3. Best for: Custom design, precise holes, professional finish
  4. Requires: laser cutting access

📦 Option B: Pre-Made Robot Chassis Kit

  1. What you need: 2WD robot chassis kit (search "2WD robot car chassis" online)
  2. Where to get it: AliExpress, local electronics stores
  3. Best for: Beginners who want to start building immediately
  4. Includes: Pre-drilled mounting holes, motor brackets, and wheels (usually)


Step 2: Body Design

The main Idea was to stack the parts above the body and use the Nylon Zip Wraps to wrap them all together thanks to their lightweight.

I used CorelDRAW to design the body, check the designs above and I made extra holes in case of any future changes.

I went to a local workshop to use their laser cutter to cut the acrylic sheet, then I stacked the parts on it.
Later, I made some changes cuz the Motors were longer than I expected.

your robot doesn't have to be built the same way as mine. also the design is very flexible you can get creative.

Design PDF file and the CorelDRAW File are attached.

If you weren't able to laser cut the design, just buy the body from any store if its available.

Step 3: Implementation (building)

The design was made to fix the sensors on the body.

Step 4: Wiring

Here is a schematic diagram of the connections. connections are tied to the code and you can change the connections in the code if you want to use other pins.

Example: if you're connecting the motor driver to pin 2 but want to change it to pin 3, change the wire pin and change the code to pin 3 as well.

Sensors

Lets talk about "The Ultrasonic sensor"

An Ultrasonic sensor is simple radar that measures the distance of an object by using sound waves. It does that by sending out a sound wave and wait for it to bounce back, it records the time it took for the round trip and divides it by 2 to get the distance.

Ultrasonic Sensor connections:

  1. GND: connect this to the Ground.
  2. VCC: connect to the power source 5 voltages.(Alert! if you connect it to more than 5v it will be damaged)
  3. Echo: connect this to any pin on the Arduino. (match it to the code)
  4. TRIG: connect this to any pin on the Arduino. (match it to the code)

Remember: All grounds (sensors, Arduino, Driver) should be connected together to the battery, so make a common Ground that leads to the battery and connect all GND pins to it.

For sensors power source (Vcc), also group the 3 Sensors to a one 5v Pin, you can use the Arduino 5v pin or the Driver 5v pin, I recommend the Driver.

--------------------------------------------------------------------------------------------------

Motor Driver

The L298N H-bridge: it controls the speed and direction of two DC motors.
it can be used with motors that have a voltage of between 5 and 35V DC.

It also has an on-board 5v regulator (5v Supply), so if your battery voltage is 12v you can use the on-board 5v to supply the sensors or any other parts.

Pins map (Check the drivers picture):

  1. DC motor 1 “+” > connect this the motor #1
  2. DC motor 1 “-” > connect this the motor #1
  3. 12v jumper > keep this connected to enable the 5v regulator.
  4. Power Source > Connect your battery positive here
  5. GND > connect this the battery negative
  6. 5v output (if 12v jumper in place) > connect the sensors here
  7. DC motor 1 enable jumper > Remove the jumper and connect it to the Arduino this is used to control the speed of motor 1 (match it to the code).
  8. IN1 Direction Control > connect it to the Arduino this is used to control the direction of motor 1 (match it to the code).
  9. IN2 Direction Control > connect it to the Arduino this is used to control the direction of motor 1 (match it to the code).
  10. IN3 Direction Control > connect it to the Arduino this is used to control the direction of motor 2 (match it to the code).
  11. IN4 Direction Control > connect it to the Arduino this is used to control the direction of motor 2 (match it to the code).
  12. DC motor 2 enable jumper > Remove the jumper and connect it to the Arduino this is used to control the speed of motor 2 (match it to the code).
  13. DC motor 2 “+” > connect this the motor #2
  14. DC motor 2 “-” > connect this the motor #2

Note: This Driver allows only 1A per channel, draining more current will damage the IC. this simply means that each motor should only drain 1A Max.

------------------------------------------------------------------------------------------------------

Battery

I used 12v Battery with 1000 mAh.

The table Above shows the voltage drop when the battery discharges. just keep it in mind and recharge the battery when it drops.

To calculate how much time you have:

Discharge time is the battery capacity (Ah or mAh) divided by the current drained.

Example: for a 1000mAh battery with a load that draws 300mA you have:

1000/300 = 3.3 hours

logically if you drain more current you get less time. just make sure that you don't exceed the Battery Discharge Current Limit or it will be damaged.

Again remember to connect all grounds to a common Ground to the battery negative.

Step 5: Coding

I had fun coding this robot. so make sure to enjoy that as well.

The main goal is to get out the maze without hitting the walls.

I had 2 simple mazes so I build it to use 2 algorithms:

- The blue maze uses right wall following algorithm.

- The red maze uses left wall following algorithm.

The photo above shows the way out in both mazes.

if you want to force only one algorithm please check the following

I made a code that can solve both mazes, so to be able to know which wall to follow I made those 3 variables:

1- first_turn = false ;

2- rightWallFollow = false ;

3- leftWallFollow = false ;

so at first they are all set to false and the robot will only go forward until it finds the first turn, if the turn is right, then the robot will follow the the right wall, and it will set first_turn and rightWallFollow to true and when it can't detect any walls it will stop (assuming that it is out of the maze). If the first turn was left then it will set first_turn and leftWallFollow to true and follow the left wall.

if you want to overwrite that you can always set them true as you want, for example:

to make the robot follow the right wall:

first_turn = true ;

rightWallFollow = true ;

leftWallFollow = false ;

if you want it to follow the left wall:

first_turn = true ;

rightWallFollow = false ;

leftWallFollow = true ;

- for readsensors I found 2 methods to get sensor readings, one kinda slow but accurate and the other is fast with less accuracy, So I put both in that section but I commented out the slow ones.

That means it's there but it's not used. to use it just uncomment it and comment the other one. If you want to use it just remove the "//" and comment out the other one as shown in the photo.

I separated each function to make the code more simple (also you can find some lines that were commented out that were used for experiments). so the main flow is the code runs the main function and calls out the other functions as needed.

Code flow:

  1. defining the pins
  2. defining output and input pins
  3. check sensors' readings
  4. use sensors' reading to define walls
  5. check first route (if it was left then follow the left wall, if it's right follow the right wall)
  6. Use PID to avoid hitting the walls and to control motors' speed

You can use this code but change the pins and the constant numbers to get the best results.


Follow This Link for the library and the Arduino Code File.

https://github.com/isaac-adas/Maze-Solving-Robot

Step 6: Have Fun

Make sure to have fun :D


This is all for fun don't panic if it's not working or if there is any thing wrong. track the error and don't give up.


Thanks for reading and I hope it helped.



Follow me on LinkedIn:

Isaac H. Adas