Hand-Following Car - Arduino and Ultrasonic Sensors Powered
Hand-Following Car - Arduino and Ultrasonic Sensors Powered
By Ilan-Haris-Ummath
It all started when I got to know about the upcoming school exhibition and
felt motivated to create something innovative using Arduino and IoT stuff.. I
referred google for ideas but couldn't find something that caught my
attention. It wasn't until coming across a video thumbnail demonstrating a
hand-following robot that inspiration struck. I researched further on the topic
and found that most of the tutorials online relied on infrared(IR) sensors,
which I lacked. Undeterred, I devised my own version of the project.
Utilizing components like the L293D IC, Ultrasonic sensors, BO motors, and
Arduino microcontroller, we've crafted a prototype vehicle that autonomously
tracks object movements.
Get ready to bring your ideas to life and amaze with the power of hands-free
navigation!
PROJECT INTRODUCTION
REAL-WORLD APPLICATIONS
Automated Surveillance
Disaster Response
Precision Agriculture
Vehicles like this could be used in farming for tasks such as moving towards
plants that require attention, delivering water or nutrients directly to the spot
where it's needed.
Healthcare Facilities
Supplies
More
Keep in mind that the following are the supplies 'Ɪ' used to make this
prototype.
×1_Arduino Microcontroller
×2_Wheels
×1_Castor Wheel
Jumper Wires
All of these supplies are easily available in the market for a very low cost. I
can't tell you the exact cost of this project as I've used some of the supplies I
already had with me.
Description of Components
➩ Arduino:
This will serve as the brain of the rover. We can achieve our desired
functionalities with Arduino by programming it according to our needs using
the Arduino IDE
➩ L293D IC
◆ INTRODUCTION
◆ SPECIFICATIONS
Dimensions - 4 × 2 × 1 cm
◆ PINOUT
This IC requires external power to operate the motors but utilizes the
Arduino's 5V supply for its own operation.
On one side of the IC, there are pins dedicated to controlling one motor, while
the pins on the other side control another motor.
The IC has four GND pins and two enable pins, one on each side.
These enable pins are used to adjust the speed of the motor: connecting them
to a PWM pin on the Arduino allows for variable speed control (PWM pins on
the Arduino Uno are identified by the tilde (~) symbol next to the pin number).
In the code, the speed can be adjusted from 0 to 255.
https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 6/29
7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered
If you examine the pinout image of the IC, you'll find that most of the pins are
self-explanatory.
L293D
-----
pin 1 ↠ Enable 1
pin 2 ↠ Arduino digital pin
pin 3 ↠ Motor 1
pin 4 ↠ GND
pin 5 ↠ GND
pin 6 ↠ Motor 1
pin 7 ↠ Arduino digital pin
pin 8 ↠ External Power
pin 9 ↠ Enable 2
pin 10 ↠ Arduino digital pin
pin 11 ↠ Motor 2
pin 12 ↠ GND
pin 13 ↠ GND
pin 14 ↠ Motor 2
pin 15 ↠ Arduino digital pin
pin 16 ↠ Arduino 5v
Tip: You can identify the pins on an IC by using the small black dot at the
corner as a reference point.
➩ Motor Shield:
Most motor shields essentially integrate the L293D IC onto a PCB, providing a
neater appearance and simplified wiring.
Here, I'm opting for a Motor Shield instead of the L293D IC due to the
simplicity in making connections. The shield I will be using was included in an
Arduino beginner kit I bought long ago.
Both the L293D and motor shield serve the same purpose in motor control, but
the motor shield simplifies the setup process. Also, many motor shields on the
market today offer more than just two motor control capabilities.
In this project, we're utilizing three of this sensor—one for each side except for
the back.
◆ INTRODUCTION
◆ SPECIFICATIONS
Operating Voltage - 5v DC
Accuracy - 3mm
Dimension - 45 x 20 x 15mm
◆ PINOUT
• Trig (Trigger): This pin initiates the ultrasonic burst. When it receives a pulse
or trigger signal, the sensor sends out an ultrasonic wave. It is an input pin.
• Echo: The Echo pin receives the reflected ultrasonic signal. When the
transmitted signal bounces off an object and returns to the sensor, the Echo
pin generates a pulse whose duration corresponds to the time taken for the
signal to return. This is an output pin.
◆ HOW IT WORKS
◆ CALCULATING DISTANCE
By measuring the time it takes for the wave to return and considering the
speed of sound, the distance to the object can be accurately calculated.
Using the average speed of sound in air (343 m/s), the distance to the object
is calculated using the formula:
The time being measured here is the time taken for the ultrasonic waves to
travel to the object and return. Therefore, to obtain the time taken to reach
the object, we divide the measured value by 2.
The BO motors (Brushed DC) are the driving force behind our car.
You need a chassis to house all components on the vehicle. You can buy one
or make it yourself. I crafted mine from an old cardboard exam board, while
multiwood is also an option for construction.
You'll also need a structure slightly higher than the chassis to mount the
ultrasonic sensors.
➩ Castor Wheel:
Caster wheels are commonly found under furniture and various objects. Here,
we utilize miniature casters positioned at the front of the rover. This setup is
chosen for cost efficiency, requiring only two motors at the back.
Before we get our hands dirty with assembling the hardware, let's kick things
off by sketching out the circuit layout for our hand-following car project. This
step is super important to make sure all the pieces come together smoothly
and work nicely with our Arduino Uno.
I'm using Tinkercad to design and create the circuits for this project.
I've cut the chassis from cardboard. The dimensions are in the picture if you
need them.
You'll also need to create holes on the platform for securing the components.
The locations for the holes are also marked in red (The markings are not
precise, you might not even need this much holes if you are sticking the
components to the chassis).
doubt you'll find a chassis that perfectly fits these criteria, so you may need to
make modifications or additions to customize it for this project.
Here's a 3D model...
Hand-Following-Robot-MODEL
More
Motors
Let's begin by securing the motors onto the chassis. This ensures a sturdy
foundation for the rest of the components.
Drill holes in the previously attached block for the screws and securely attach
the L-shaped BO motors, ensuring that both motors are kept parallel to each
other. Tighten the screws firmly to affix the motors securely to the chassis.
Castor Wheel
Position the wheel correctly over the pre-drilled holes and secure it with
screws. Alternatively, you can use adhesive if preferred. Additionally, adjust
the distance between the castor wheel and the chassis if necessary, especially
if the front appears significantly lower than the back. I utilized double-sided
screw extenders similar to these ones (link (link removed)), to achieve the
correct height.
You can simply use double-sided tape to fix the Arduino Uno and breadboard
securely onto the chassis.
However, I chose to use screws since my Arduino already has pre-built screw
holes.
Ultrasonic Sensors
This is the most crucial part of the assembly process. There are many ways to
secure these sensors, but I decided to mount them at a specific height. If
you're doing this another way, just remember not to cover the two eyes of the
sensor.
Placing them directly on the chassis shouldn't pose any new issues. If you are
choosing this approach, pin them directly on to a breadboard for easier
wiring.
Sensor Orientation:
Regardless of how you secure the sensors, achieving precise orientation can
be hard. However, it's not necessary to achieve exact angles for the car to
function properly. Try to get the angles as close to 45° as possible. In my
initial attempt, I measured angles of approximately 40° and 35°, which still
allowed the car to operate effectively.
External Power(Batteries)
The Arduino Uno also requires power to operate when not connected via USB.
Therefore, you will need another battery pack to provide power to the
Arduino.
Step 4: Wiring
The connections for the 4-pin ultrasonic sensors and Arduino are as follows:
Ultrasonic 1 ↔ Arduino
-------------------------
VCC ↔ 5V
Trig ↔ pin 9
Echo ↔ pin 8
GND ↔ GND
Ultrasonic 2 ↔ Arduino
-------------------------
VCC ↔ 5V
Trig ↔ pin 11
Echo ↔ pin 10
GND ↔ GND
Ultrasonic 3 ↔ Arduino
-------------------------
VCC ↔ 5V
Trig ↔ pin 13
Echo ↔ pin 12
GND ↔ GND
The 'Trig' and 'Echo' pins of the ultrasonic sensors can be connected to any
available digital pins on the Arduino. However, if you're using a motor shield,
most of the digital pins may already be occupied by the shield's connections.
In such cases, many motor shields have additional headers for extending the
pin connections, allowing you to access more pins. Please refer to your motor
shield's datasheet for specific information on pin allocation.
Since we're using 3 ultrasonic sensors, we'll need a total of 6 digital pins for
the sensors themselves. Remember, analog pins on the Arduino can also
function as digital pins, if needed, without extra steps. The code remains the
same. Just ensure correct connections and assign pins in the code.
If you're connecting the ultrasonic sensors directly to the L293D IC, you'll
likely have plenty of pins available for use. In this scenario, follow the below
circuit...
If you're using a motor shield, consult its manual for connections. Most likely,
you'll have an Arduino-mounted shield, and the only connections you'll need
to make will be for the motor and shield.
L293D ↔ Arduino
--------------------
Pin 1 ↔ 5V
Pin 2 ↔ pin 5
Pin 3 ↔ Motor 1 anyone Terminal
Pin 4 ↔ GND
Pin 5
Pin 6 ↔ Motor 1 other Terminal
Pin 7 ↔ pin 4
Pin 8 ↔ External Power Positive Terminal
Pin 9 ↔ 5V
Pin 10 ↔ pin 2
Pin 11 ↔ Motor 2 anyone Terminal
Pin 12 ↔ External Power Negative Terminal
Pin 13
Pin 14 ↔ Motor 2 other Terminal
Pin 15 ↔ pin 3
Pin 16 ↔ 5V
The reason we left out some pins is because they are GND pins, and we have
already connected the Arduino GND to pin 4 of the L293D. Pin 5, pin 12, pin 13,
pin 4 all are GND pins.
Imp: You need to connect the negative terminal of the external power source
to the ground.
Hand-Following-Car
Step 5: Programming
Now that all our hardware components are set up, we can proceed to coding.
Below is the code I've written. You may need to adjust the pins in the program
according to your setup. I've indicated the pins I used. Copy Paste this code.
void setup() {
Serial.begin(9600);
void loop() {
// 10. Ultrasonic sensor 1
digitalWrite(trig1, LOW);
delayMicroseconds(2);
digitalWrite(trig1, HIGH);
delayMicroseconds(10);
digitalWrite(trig1, LOW);
duration = pulseIn(echo1, HIGH);
distance1 = duration * 0.034 / 2;
𝐂𝐨𝐝𝐞 𝐄𝐱𝐩𝐥𝐚𝐧𝐚𝐭𝐢𝐨𝐧
In this section also we are assigning variables. But to the pins where we
connected our three ultrasonic sensors' Trig and Echo pins.
Stop();
to stop both motors. This allows us to avoid repeating this piece of code every
time we need to stop the two motors. Instead, we can simply call this
function.
digitalWrite();
Forward();
digitalWrite();
In this function, we are turning the right motor forward to steer the vehicle
left.
Optionally, you could modify the code to also turn the left motor backward.
This would create sharper turns. However, in my case, I prefer smooth turns.
To turn the vehicle right, we are turning the left motor forward. This is the
opposite of what we did earlier to turn the vehicle left.
●Void Setup
The code in this section runs only once when the Arduino is powered on or
reset. It is typically used for initializing variables, setting pin modes, or
configuring libraries.
In this section of the code, we configure the pins connected to the L293D
motor driver module as outputs. By setting these pins as outputs, we instruct
the Arduino to send signals to the motor driver module rather than receiving
Here, we set the Trig pins of each sensor as output and the Echo pins as input.
This is because we send the signal to emit the ultrasonic burst through the
Trig pin.
We obtain the time value from the Echo pin, which receives the reflected
waves. Therefore, we set Echo as input because we need to read information
from it.
●Void Loop
The code in this section runs continuously in a loop until the Arduino is
powered off or reset. It is where the main functionality of the program, such
as reading sensors, processing data, and controlling outputs, is typically
implemented.
In this section, we calculate the distance of sensor 1 from the object using the
steps mentioned earlier.
First, we send the ultrasonic waves and get the time it took to return. Then,
we calculate the distance based on this time measurement.
digitalWrite();
command to set the trig pin HIGH. After a delay of 10 microseconds, we set
the trig pin LOW. Then, we read the value from the echo pin using the
pulseIn();
command and use this value in the equation to calculate the distance.
Setting the trig pin LOW at the beginning serves as a failsafe measure. In the
event that the trig pin is accidentally turned on, this step ensures it is reset
before sending the ultrasonic burst.
In this section, we perform the same steps as we did for sensor 1, but for the
second sensor.
Here also we do the same as before, but for the third sensor.
I have intentionally commented out this section. Printing all three values on
the serial monitor simultaneously can make it less readable. Therefore, you
can uncomment the first, second, or third line according to your need to read
the sensor value.
This is the most important part of the program. This if-else loop makes the car
run.
» In the first condition, we check if any of the distances from the sensors are
less than 4 cm:
This is a safety feature. It ensures that when the car gets very close to an
object (when the distance between any of the sensors and the object is less
than 4 cm), the car will stop by calling the 'stop function' we defined earlier.
However, this isn't a very dependable feature since it relies on the reflective
property of waves. If the obstacle is too thin, the waves may not have enough
space to hit and reflect back and the vehicle might hit the object. Therefore, I
recommend building a small cushion-like feature at the car's front to absorb
the force when it hits something. This is to protect all the other components
on the car.
If both conditions are true, the car goes forward. If this line of code wasn't
included, and if sensor 2 and sensor 3 detect an object in front of them, the
car would go right. This is because we check sensor 2's state before we check
sensor 3's state. So, if an object is detected in front of sensor 2, the car would
immediately turn right without checking sensor 3. By checking this combined
condition before the sensors are individually checked, we avoid such an issue.
» In the third condition, we check if the distance value from sensor 1 is less
than 25 cm:
» In the fourth condition, we check the distance between sensor 2 and the
object:
If this condition is true, we call the right function to turn the car right by
turning the left motor.
» In the last condition, we check the distance between sensor 3 and the
object:
If the value is less than 25, we turn the car left using the function 'Left()'.
» Finally, in the else loop, we call 'Stop()' to make the car stop. The loop will
enter this section only when NONE of the above conditions are met.
Step 6: Working
Here are the videos of the car running and distance value printing on the
serial monitor.
‣ If you notice that your Serial Monitor is printing values with a higher delay
than preset or slowly outputting zero values, check your connections and the
pin numbers in the code to ensure they match and are correct. If the issue
persists, double-check your connections and try again.
‣ If the motors aren't working or are turning in the opposite direction, try
changing the order in which you connected them. Swap the positive and
negative connections to see if it resolves the issue.
‣ If the motors are running too slowly, consider changing the batteries to
ensure they're getting sufficient power.