In this article, we will build an Arduino robot with an ultrasonic sensor for obstacle avoidance.
Obstacle avoidance is one of the critical features of robot automation because it enables them to navigate around objects without requiring human input.
And one of the easiest ways to build this feature is to use an ultrasonic sensor.
The device has a simple operating mechanism, which we’ll utilize in its programming to calculate the distance to objects and avoid them.
Let’s get right into this Arduino project.
Table of Contents
- What Are Ultrasonic Sensors?
- How To Build a Robot With an Ultrasonic Sensor
- Wrap Up
What Are Ultrasonic Sensors?
Ultrasonic sensors are electrical instruments that generate and detect ultrasonic sound waves to measure the distance between themselves and the objects ahead.
At the core of this device is a transducer, which consists of a transmitter to convert electrical signals into ultrasound and a receiver to convert the ultrasound echo back into electrical signals.
An ultrasonic sensor for electronics projects
A processor analyzes the echo or reflected signals to determine if there is an obstacle ahead and how far it is.
In robotics, this device is critical for determining if the path ahead is clear.
So instead of a camera, you can use an ultrasonic sensor and the eye for the robot.
How To Build a Robot With an Ultrasonic Sensor
Before diving into the project, let’s look at how the sensor works.
As stated earlier, this component transmits and receives ultrasonic waves.
But how does it determine there is an obstacle ahead? It measures the following.
- The time it takes the transmitter to send the signals
- Receiving period (time takes for the echo to hit the receiver)
So it sends out the sound wave at a specific frequency and listens to echo at the same frequency while measuring the time delay.
The device calculates the distance using the following formula.
Distance = (Time x Sound Speed in Air)/2, where the speed of sound in air is 343 m/s.
But it is vital to note that the distance sensor might not detect some objects because their shapes might reflect the sound waves in a way that does not get back to the sensor.
Also, some obstacles might be too small or slim/narrow to reflect enough sound waves for detection.
But the sensors are highly reliable and accurate because they have millimeter-level precision on objects that reflect sound waves to the receiver.
What You Need
- Arduino Nano (you can use the Arduino Mega2560 or Arduino UNO, but the Nano is compact and ideal for this project)
- HC-SR04 ultrasonic sensor
- L298N motor driver module (you can use a motor shield)
- Two DC motors (5V)
- Robot chassis
- 9V battery
- Jumper wires
Why Use a Motor Shield/Driver Module?
This regulator cannot handle high current levels and can overheat if you draw electric current from it to run the motors.
The L298N motor driver module
So we must find another way to power the two motors without drawing current from the onboard regulator.
The driver module or shield is the solution.
Besides powering the wheels, this layer makes it easy to connect and wire the motors and allows features like motor direction reversal.
So with this setup, the board will only provide control signals.
|Nano, L298N, HC-SR04
|Vin (Nano), 12V (L298N)
|GND (Nano), GND (L298N), GND (HC-SR04)
|OUT1 & OUT2
|OUT3 & OUT4
A circuit diagram of the Arduino robot with an ultrasonic sensor
Assemble these components in the robot chassis, then connect the wheels to the motors.
Make sure you balance the weight evenly across the robot chassis, and place the HC-SR04 distance sensor at the front with no obstacles covering the transmitter and receiver.
A programmable 4-wheel robot with an ultrasonic sensor at the front
Obstacle Avoidance Code
Connect the Nano board to your computer, open the Arduino IDE, then write the following code and run it on the board.
We begin by defining the echo and trig pins for the ultrasonic sensor, which connect to the Arduino Nano’s GPIO pins 9 and 10, respectively.
Next, we define the motor driver module’s input pins to instruct the motor direction.
These pins (IN1 to IN4) connect to the Arduino Nano to receive the driving signals.
After that, the Setup () function defines the direction of data for the connected GPIO pins.
We’ll set the trig pin and four motor pins as outputs, but the echo will be an input pin.
Immediately after is the Loop () function, which gets the distance of the obstacle from the ultrasonic sensor.
The code sends a beam of ultrasonic waves for 10 microseconds and receives the echo after 10 microseconds.
A two-wheel programmable robot with an ultrasonic sensor at the front
In the if () condition statement, we check if the calculated distance to the obstacle is greater than the defined distance (19).
If greater than 19, the motors will keep spinning to move the robot forward.
But if the distance is less than 18, there is an obstacle right ahead, and the motors should stop, move back a little, then turn to face another direction.
Try to alter the GPIO-pin numbers if you don’t get the required robot wheel movements.
As you can see, building an Arduino robot with obstacle avoidance using an ultrasonic sensor is an easy task.
And if you can’t find a robot chassis, you can order a 3D-printed one or use a toy. The other steps are easy to follow.
But if you encounter any challenges, contact us for further guidance. We’ll be happy to help.