Robotics Project

Build an Obstacle-Avoiding Robot

An obstacle-avoiding robot is a small autonomous vehicle that detects objects in its path and changes direction automatically. This project combines a distance sensor, microcontroller, motor driver and DC motors to create a robot capable of navigating without continuous human control.

Obstacle-Avoiding Robot

Project Overview

The robot continuously measures the distance to objects in front of it. When an obstacle is detected within a selected distance, the controller stops the motors and selects another direction.

Distance Sensor
       โ”‚
       โ–ผ
Microcontroller
       โ”‚
       โ–ผ
Navigation Logic
       โ”‚
       โ–ผ
Motor Driver
       โ”‚
   โ”Œโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”
   โ–ผ       โ–ผ
Left     Right
Motor    Motor

This simple feedback system allows the robot to move autonomously around obstacles.

Robot Platform

A basic two-wheel robot can be constructed using two geared DC motors, two wheels and a free-moving caster or skid.

        FRONT
          โ–ฒ
          โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚   SENSOR    โ”‚
    โ”‚             โ”‚
    โ”‚             โ”‚
    โ”‚             โ”‚
    โ”‚             โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
      O         O
    LEFT      RIGHT
    MOTOR     MOTOR

The two motors can be controlled independently, allowing the robot to move forward, reverse and turn.

Ultrasonic Distance Sensor

An ultrasonic sensor measures distance by transmitting a sound pulse above the normal range of human hearing and measuring the time taken for the reflected pulse to return.

Sensor
  โ”‚
  โ”‚  Ultrasonic Pulse
  โ–ผ
Object
  โ”‚
  โ”‚  Echo
  โ–ผ
Sensor

The controller can calculate distance from the measured echo time.

Distance Measurement

The basic relationship is:

Distance = Time ร— Speed of Sound / 2

The division by two is required because the ultrasonic pulse travels from the sensor to the object and then back to the sensor.

The speed of sound varies with environmental conditions, particularly temperature, so the calculated distance is an approximation unless environmental compensation is applied.

Obstacle Detection

The controller compares the measured distance with a predefined obstacle threshold.

Measure Distance
       โ”‚
       โ–ผ
Distance < Threshold?
    โ”Œโ”€โ”€โ”ดโ”€โ”€โ”
   YES    NO
    โ”‚      โ”‚
    โ–ผ      โ–ผ
 Stop    Forward
Robot    Motion

For example, the robot could begin its avoidance routine when an obstacle is detected within a selected distance.

Microcontroller

An Arduino-compatible controller, ESP32 or another suitable microcontroller can control the robot.

The controller handles:

  • Distance measurement.
  • Obstacle detection.
  • Motor control.
  • Direction selection.
  • Timing.
  • Navigation logic.

Motor Driver

The microcontroller cannot normally provide enough current to drive the DC motors directly.

A motor-driver circuit is therefore used between the controller and motors.

Microcontroller
      โ”‚
      โ–ผ
 Motor Driver
   โ”‚       โ”‚
   โ–ผ       โ–ผ
Left     Right
Motor    Motor

The driver should be selected according to the motor voltage and stall-current requirements.

H-Bridge Motor Control

An H-bridge allows the direction of a DC motor to be reversed.

         +V
          โ”‚
      โ”Œโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”
      โ”‚ H-BRIDGE
      โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜
          โ”‚
        MOTOR
          โ”‚
         GND

By changing the switching states, the controller can make the motor rotate forward or backward.

Basic Motor Commands

A two-wheel robot can produce several useful movements.

Left Motor   Right Motor

Forward       Forward       โ†’ Forward

Reverse       Reverse       โ†’ Reverse

Forward       Stop          โ†’ Turn Right

Stop          Forward       โ†’ Turn Left

Reverse       Forward       โ†’ Rotate

The exact direction depends on the mechanical arrangement and motor wiring.

Forward Motion

During normal operation, both motors rotate in the forward direction.

      FRONT
        โ†‘

   โ†บ         โ†ป
 LEFT      RIGHT
 MOTOR     MOTOR

        โ†’ โ†’ โ†’

The robot continuously checks the distance sensor while moving.

Obstacle Avoidance

When an obstacle is detected, the robot can stop and determine which direction has more available space.

 Obstacle Detected โ”‚ โ–ผ STOP โ”‚ โ–ผ Measure Left โ”‚ โ–ผ Measure Right โ”‚ โ–ผ Choose Clearer Direction โ”‚ โ–ผ TURN โ”‚ โ–ผ Continue 

Scanning Left and Right

A servo motor can rotate the ultrasonic sensor to inspect different directions.

 Sensor โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ” โ”‚ SERVO โ”‚ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ”‚ LEFT โ† โ— โ†’ RIGHT 

The controller can measure the distance on the left and right before selecting the preferred direction.

Servo-Controlled Sensor

A small servo can rotate the sensor through a range of angles.

For example:

 90ยฐ โ”‚ โ–ผ FRONT 45ยฐ 135ยฐ โ”‚ โ”‚ โ–ผ โ–ผ LEFT RIGHT 

The controller can compare the measured distances and turn toward the direction with more free space.

Simple Avoidance Algorithm

A basic algorithm can operate as follows:

 Start โ”‚ โ–ผ Move Forward โ”‚ โ–ผ Measure Distance โ”‚ โ–ผ Obstacle? โ”Œโ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” NO YES โ”‚ โ”‚ โ–ผ โ–ผ Forward Stop โ”‚ โ–ผ Reverse Briefly โ”‚ โ–ผ Scan Directions โ”‚ โ–ผ Choose Direction โ”‚ โ–ผ Turn โ”‚ โ””โ”€โ”€โ”€โ”€โ–บ Forward 

Reverse Before Turning

If the robot is very close to an obstacle, it may first reverse for a short period before turning.

This provides additional space for the robot to rotate.

 Obstacle โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ โ†‘ โ”‚ Robot โ”‚ โ–ผ Reverse โ”‚ โ–ผ Turn โ”‚ โ–ผ Continue 

Turning Strategy

The robot can use several strategies when an obstacle is detected.

  • Always turn left.
  • Always turn right.
  • Choose the side with greater distance.
  • Alternate directions.
  • Use a predefined navigation pattern.

Choosing the direction with more free space generally provides more flexible behavior than always turning in the same direction.

Motor Speed Control

PWM can be used to control motor speed.

 PWM Low โ”‚ โ–ผ Slow Motor PWM Medium โ”‚ โ–ผ Medium Speed PWM High โ”‚ โ–ผ High Speed 

The motor-driver and motor must both be capable of operating with the chosen PWM method.

Differential Steering

The robot can turn by running the two motors at different speeds.

 Left Motor = 50% Right Motor = 100% โ†“ Robot curves left 

This allows smoother steering than simply stopping one motor.

Pivot Turning

A sharper turn can be achieved by running one motor forward and the other backward.

 Left Motor Reverse Right Motor Forward โ†“ Robot rotates 

This is useful when the robot needs to turn within a small area.

Battery Supply

The robot requires a battery capable of supplying both the motors and control electronics.

Motor current can vary significantly, especially during acceleration and when the wheels are obstructed.

The battery and wiring should therefore be selected according to the motor's actual current requirements.

Separate Logic Supply

Motor noise and voltage fluctuations can interfere with the microcontroller.

The design can use appropriate regulation and decoupling for the logic supply.

 Battery โ”‚ โ”œโ”€โ”€โ”€โ”€โ–บ Motor Driver โ”€โ”€โ”€โ–บ Motors โ”‚ โ””โ”€โ”€โ”€โ”€โ–บ Regulator โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Microcontroller 

The grounds must be connected appropriately for the chosen driver and control architecture.

Motor Noise

Brushed DC motors can generate electrical noise through their brushes and commutators.

Suitable suppression, wiring, decoupling and PCB layout can reduce the effect of this noise on the controller.

Sensor Blind Spots

An ultrasonic sensor does not provide perfect coverage of the area around the robot.

Very small objects, angled surfaces and objects outside the sensor's effective field can be difficult to detect.

A sensor mounted on a servo can improve coverage by scanning multiple directions.

Multiple Sensors

More advanced robots can use multiple distance sensors.

 FRONT โ”Œโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ” โ”‚ L โ”‚ R โ”‚ โ”‚Sensor Sensor โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ Robot 

Multiple sensors can reduce blind spots and allow faster obstacle detection.

Infrared Distance Sensors

Infrared distance sensors can also be used for obstacle detection.

They can be useful at short distances and can complement ultrasonic sensors.

The choice depends on the required range, object characteristics and environment.

Navigation State Machine

A state machine makes the robot's behavior easier to organize.

 FORWARD โ”‚ โ–ผ OBSTACLE โ”‚ โ–ผ REVERSE โ”‚ โ–ผ SCAN โ”‚ โ–ผ TURN โ”‚ โ””โ”€โ”€โ”€โ”€โ–บ FORWARD 

Each state performs a specific action before transitioning to another state.

Stuck Detection

A robot can sometimes become trapped between obstacles.

The software can detect repeated unsuccessful movements and initiate an escape routine.

 Repeated Obstacle โ”‚ โ–ผ Change Strategy โ”‚ โ–ผ Reverse Longer โ”‚ โ–ผ Rotate โ”‚ โ–ผ Search Again 

Wheel Slip

Motor commands do not always correspond directly to actual movement.

Wheel slip can occur on smooth, dusty or uneven surfaces.

For simple obstacle avoidance this is generally acceptable because the robot relies primarily on distance sensing rather than precise position tracking.

Encoders

Wheel encoders can provide feedback about motor rotation.

 Wheel โ”‚ โ–ผ Encoder โ”‚ โ–ผ Pulse Count โ”‚ โ–ผ Microcontroller โ”‚ โ–ผ Movement Feedback 

Encoders allow more advanced control of speed, distance and turning angle.

Improving Navigation

The basic obstacle-avoidance algorithm can be expanded with:

  • Wheel encoders.
  • Multiple distance sensors.
  • Servo scanning.
  • Speed control.
  • Gyroscope or IMU.
  • Mapping.
  • Path planning.

These additions can turn the simple robot into a more sophisticated autonomous platform.

Testing the Robot

  1. Inspect the mechanical assembly.
  2. Check wheel alignment.
  3. Verify motor wiring.
  4. Verify battery voltage.
  5. Test each motor independently.
  6. Test the motor driver.
  7. Test the distance sensor.
  8. Verify distance readings.
  9. Test forward movement.
  10. Test reverse movement.
  11. Test left and right turns.
  12. Test obstacle detection.
  13. Test the complete avoidance routine.
  14. Test the robot on different surfaces.

Troubleshooting

Robot Does Not Move

  • Check the battery.
  • Check motor-driver supply.
  • Check motor wiring.
  • Check motor-driver enable signals.
  • Check the controller outputs.

One Motor Runs Backwards

  • Reverse the motor's two wires.
  • Alternatively invert its direction in software.

Distance Sensor Gives Incorrect Readings

  • Check sensor wiring.
  • Check the trigger and echo signals.
  • Check supply voltage.
  • Check sensor alignment.
  • Check the object's surface.

Robot Keeps Turning

  • Check the distance threshold.
  • Check sensor readings.
  • Check motor direction.
  • Check the navigation state machine.
  • Check whether the robot is trapped.

Microcontroller Resets

  • Check battery voltage.
  • Check motor startup current.
  • Improve power decoupling.
  • Separate motor and logic power paths where appropriate.
  • Check motor noise.

Common Mistakes

  • Driving motors directly from microcontroller GPIO pins.
  • Using an undersized motor driver.
  • Ignoring motor stall current.
  • Using a weak battery.
  • Failing to provide adequate controller decoupling.
  • Using an unsuitable obstacle threshold.
  • Ignoring sensor blind spots.
  • Failing to handle situations where the robot becomes trapped.
  • Testing at full speed before verifying the control system.

Applications

  • Educational robotics.
  • Autonomous vehicle experiments.
  • Robotics competitions.
  • Microcontroller projects.
  • Sensor experiments.
  • Motor-control experiments.
  • Autonomous navigation research.

Safety Notes

  • Use a battery appropriate for the motor system.
  • Protect exposed battery terminals.
  • Keep fingers and loose objects away from moving wheels.
  • Do not operate the robot near stairs or dangerous drop-offs.
  • Secure wiring so it cannot contact moving parts.
  • Use appropriate protection for rechargeable batteries.
  • Disconnect the battery before servicing the electronics.

Skills Learned

  • Ultrasonic distance measurement.
  • Obstacle detection.
  • DC motor control.
  • H-bridge operation.
  • PWM speed control.
  • Differential steering.
  • Servo control.
  • Autonomous navigation.
  • State-machine programming.
  • Robotics troubleshooting.

Key Points

  • An obstacle-avoiding robot uses sensors to detect objects without human intervention.
  • Ultrasonic sensors can provide useful distance measurements.
  • A motor driver is required to control DC motors safely from a microcontroller.
  • Two independently controlled motors allow differential steering.
  • A servo can rotate a sensor to inspect different directions.
  • Hysteresis and appropriate distance thresholds can improve stable behavior.
  • A state machine makes autonomous navigation logic easier to manage.
  • Encoders and additional sensors can significantly improve navigation.
  • Motor power requirements must account for startup and stall current.
  • Good power and wiring design helps prevent controller resets caused by motor noise.

Next Project

Continue exploring robotics, sensors and autonomous control projects in the ExotElectronics project series.