Table of Contents

Maze Solver Robot

Author Berescu Silvia-Maria
Series and Group 332CD
GitHub Maze Solver Robot

Introduction

This project involves building a small autonomous robot capable of navigating and escaping a maze with opaque walls, using ultrasonic sensors and a wall-follower algorithm.

Hypothesis: I believe that using the Left-Hand Wall Follower algorithm with HC-SR04 ultrasonic sensors and real-time visual feedback through an LCD will allow the robot to solve a simple maze in under 60 seconds, as the algorithm guarantees finding the exit in any simply connected maze.

General Description

The block diagram below shows all project modules and how they interact:

Block Diagram

Main modules and their roles:

Hardware Design

Bill of Materials

No. Component Qty Role
1 2WD chassis kit (line follower) 1 Mechanical platform
2 ATmega328P-XMINI 1 Main microcontroller
3 L298N motor driver module 1 DC motor control via PWM
4 HC-SR04 ultrasonic sensor 2 Obstacle detection
5 LCD 16×2 with I2C module (PCF8574) 1 Robot status display
6 18650 Li-ion battery 2500mAh 2 Autonomous 7.4V power
7 Dual 18650 charger 1 Battery recharging
8 2×18650 battery holder + switch 1 Battery mount + ON/OFF
9 Male-female dupont wires 40 Component connections
10 400-point breadboard 1 Connection prototyping

Pin Mapping — Arduino Labels vs ATmega328P Physical Pins

The table below clarifies the mapping for all pins used in this project:

ATmega Port/Pin Used For
PD2 HC-SR04 Front — TRIG
PD3 (INT1) HC-SR04 Front — ECHO
PD4 HC-SR04 Left — TRIG
PD5 (OC0B) L298N ENB — PWM Motor Right
PD6 (OC0A) L298N ENA — PWM Motor Left
PB0 L298N IN1
PB1 L298N IN2
PB2 L298N IN3
PB3 L298N IN4
PB4 HC-SR04 Left — ECHO
PC4 (SDA) LCD I2C — SDA
PC5 (SCL) LCD I2C — SCL
VCC 5V power input
GND Ground

PD3/INT1: connected to the INT1 external interrupt line, used for precise ECHO pulse measurement on the front sensor. Interrupt-driven measurement is more accurate than polling because the ATmega reacts instantly to the signal edge without busy-waiting.

PD5/OC0B and PD6/OC0A: both are hardware PWM outputs of Timer0 (Channel B and Channel A respectively). Using both channels allows independent speed control of each motor, which is essential for straight-line driving.

PB4: plain GPIO pin used for HC-SR04 Left ECHO via polling. It has no special hardware function requirements since the left sensor uses software polling.

Electrical Schematic

Electrical Schematic

The schematic uses net labels instead of long wires for clarity. Two pins sharing the same net label name are electrically connected.

Schematic Explanation

Power Supply

  1. The 2×18650 batteries (7.4V in series) connect through the ON/OFF switch on the battery holder to the +12V pin of the L298N (labelled VMS on the physical module)
  2. The L298N drives the two DC motors directly at 7.4V through OUT1/OUT2 (Motor A) and OUT3/OUT4 (Motor B)
  3. The L298N has an onboard 5V regulator. Its +5V output is connected to the VCC rail, powering the ATmega328P, both HC-SR04 sensors and the LCD
  4. All GND pins across all components share a common ground

Motor Control

The L298N receives direction signals on IN1–IN4 from ATmega pins D8–D11:

IN1 IN2 Motor Left behavior
LOW HIGH Forward
HIGH LOW Reverse
LOW LOW Stop

The same logic applies to IN3/IN4 for Motor Right.

Speed is controlled via PWM on both ENA (D6/OC0A) and ENB (D5/OC0B) using Timer0 Fast PWM mode.

HC-SR04 Front Sensor

HC-SR04 Left Sensor

LCD 16x2 I2C

Power Supply Summary

  1. 2×18650 batteries (7.4V) → ON/OFF switch → L298N +12V (VMS)
  2. L298N → motors at 7.4V
  3. L298N 5V OUT → ATmega328P + HC-SR04 x2 + LCD I2C

Hardware Progress

Assembled Robot

The image shows the fully wired robot with all components mounted and powered via the 2×18650 battery pack (7.4V).

Component Identification

What the Indicators Confirm

Software Design

Development Environment

Project Structure

File Role
main.cpp Motor functions, navigation algorithm, setup/loop
sensors.cpp/.h HC-SR04 front (INT1 interrupt) and left (polling)
lcd.cpp/.h TWI/I2C driver + HD44780 LCD functions

Lab Concepts Used and Justification

Lab concept Justification
GPIO TRIG/ECHO sensor pins and motor direction pins controlled directly via DDRx/PORTx registers
PWM Timer0 Fast PWM on OC0A and OC0B for independent speed control of each motor; allows straight-line correction by setting different OCR values
Interrupts INT1 on PD3 for front HC-SR04 ECHO, interrupt-driven measurement is more accurate than polling because the MCU reacts instantly to signal edges without busy-waiting
I2C LCD 16×2 via PCF8574 on PC4/PC5; TWI implemented on registers following the same pattern as the lab
Timers Timer0 for PWM generation; Timer1 as free-running counter inside INT1 ISR to timestamp ECHO pulse edges and compute distance

The robot implements the Left-Hand Wall Follower algorithm. The maze is constructed so the robot always has a left wall present. The decision logic runs continuously in the main loop:

  1. If left is free (distance > 20cm): turn left 90° then move forward — this is the core left-hand rule, taking every available left turn
  2. Else if front is free (distance > 20cm): move forward, follow the left wall
  3. Else: turn right 90°, blocked on both sides, only option is right

This guarantees finding the exit in any simply connected maze.

Sensor Implementation

The front sensor uses INT1 hardware interrupt for precise ECHO pulse measurement. Timer1 is started on the rising edge of ECHO and stopped on the falling edge. Distance is computed from the elapsed ticks. This approach is more accurate than polling and frees the CPU between measurements.

The left sensor uses GPIO polling. PB4 (D12) was chosen instead of D5 because D5 (OC0B) was needed for ENB PWM. Polling is acceptable here since the left wall changes slowly, precision requirements are lower than for the front sensor.

Motor Control

Timer0 Fast PWM at 7.8kHz drives both motors independently via OC0A (left) and OC0B (right).

Important finding: The motor direction logic is inverted, determined experimentally during hardware testing.

Motor B runs at a slightly higher PWM value than Motor A to compensate for mechanical differences between the two motors, ensuring straight-line driving.

Calibration

Validation

Each component was validated independently before integration: motors tested individually for direction and speed, each sensor tested by placing hand at known distances and verifying LCD readings, turn angles verified physically on grid paper. Full algorithm validated in test maze sections observing behavior for each decision case.

Demo Video

To be added after final maze test

Results

Conclusions

Download

Journal

Week Activity Status
Week 1 Component selection, ordering parts Done
Week 2 Chassis assembly, mounting motors and wheels Done
Week 3 L298N wiring + electrical schematic Done
Week 4 HC-SR04 integration + LCD I2C integration Done
Week 5 Navigation algorithm implementation + maze test Planned
Week 6 Code optimization, final demo Planned

Bibliography / Resources

Hardware Resources

Export to PDF