0% found this document useful (0 votes)
30 views29 pages

Hand-Following Car - Arduino and Ultrasonic Sensors Powered

asdadaw wxdx aqw dac dad

Uploaded by

codecrusher0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views29 pages

Hand-Following Car - Arduino and Ultrasonic Sensors Powered

asdadaw wxdx aqw dac dad

Uploaded by

codecrusher0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

Hand-Following Car: Arduino and Ultrasonic Sensors


Powered

By Ilan-Haris-Ummath

Published March 10, 2024 on Instructables.com

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.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 1/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

Utilizing components like the L293D IC, Ultrasonic sensors, BO motors, and
Arduino microcontroller, we've crafted a prototype vehicle that autonomously
tracks object movements.

In this comprehensive guide, I would like to give you a step-by-step walk


through the process of building your own autonomous vehicle that follows
your move. If you're new to this field, don't worry! With this guide, it will be a
piece of cake for you to make this project, and I believe it will teach you more
about Arduino and its integration.

Get ready to bring your ideas to life and amaze with the power of hands-free
navigation!

PROJECT INTRODUCTION

This hand-following car project harnesses the capabilities of Arduino as its


central control unit. Utilizing a motor shield, the Arduino precisely regulates
the speed and direction of the motors, facilitating seamless movement.

Additionally, ultrasonic sensors are used to detect objects ahead of the


vehicle. These sensors emit ultrasonic waves and calculate the time it takes
for the waves to return, enabling accurate distance measurement (by using a
bit of math).

Through coding, the car is commanded to move forward while maintaining a


safe distance from detected objects.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 2/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

This integration of hardware and software enables the vehicle to navigate


autonomously, exemplifying the transformative potential of technology in
motion.

REAL-WORLD APPLICATIONS

Automated Surveillance

Configure it as a basic surveillance device that can follow an intruder's


movements, possibly integrating a camera for recording purposes.

Automated Material Handling

In warehouses, vehicles like these can be used to move materials towards a


sorting point. They can detect packages and transport them from one place to
another without human intervention.

Disaster Response

Small autonomous vehicles can be employed in disaster-hit areas to navigate


through debris and locate survivors using heat sensors or other detection
mechanisms.

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.

Parking Lot Car Finder

A similar system could be implemented in vehicles to help them locate their


owners in large parking lots by moving towards an activated signal.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 3/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

Healthcare Facilities

In hospitals or care facilities, this car can transport medical supplies by


following a healthcare provider's hand movements, ensuring swift and
hygienic delivery of essential items without direct contact.

Supplies

More

Keep in mind that the following are the supplies 'Ɪ' used to make this
prototype.

×1_Arduino Microcontroller

×1_Motor Shield (or L293D IC)

×3_Ultrasonic Sensors (HC-SR04 or similar)

×2_DC Motors (BO motors)

×1_Breadboard (optional, for prototyping)

×2_Wheels

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 4/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

×1_Castor Wheel

×1_Power Source (e.g., battery pack or power supply)

Jumper Wires

Basic Tools (for assembly)

Chassis or Frame for the Car

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:

Arduino, a programmable microprocessor, enables the creation of an array of


projects ranging from LED blinkers to robots and drones, while seamlessly
interacting with a diverse range of devices and components.

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

Specifically, I'll be using the Arduino UNO R3.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 5/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

➩ L293D IC

◆ INTRODUCTION

The L293D is a 16-pin Motor Driver IC (Integrated Circuit) specifically designed


to control the speed and direction of motors. Motor drivers provide essential
protection against back electromotive force (EMF) generated by the motors.
Directly connecting motors to the Arduino board can risk damaging it due to
this EMF.

◆ SPECIFICATIONS

Motor voltage Vcc2 - 4.5V to 36V

Supply Voltage to Vcc1 - 4.5V to 7V

Maximum Peak motor current - 1.2A

Maximum Continuous Motor Current - 600mA

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

Alternatively, if you're not interested in varying the speed, connecting the


enable pins to the Arduino's 5V pin will run the motors at maximum speed or
very close to it.

If you examine the pinout image of the IC, you'll find that most of the pins are
self-explanatory.

The following is the pinout:

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.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 7/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

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.

➩ Ultrasonic sensor HC-SR04:

In this project, we're utilizing three of this sensor—one for each side except for
the back.

◆ INTRODUCTION

The HC-SR04 is an affordable and easy to use distance measuring sensor


which has a range from 2cm to 400cm

◆ SPECIFICATIONS

Operating Voltage - 5v DC

Operating Current - 15mA

Operating Frequency - 40KHz

Minimum Range - 2cm / 1 inch

Maximum Range - 400cm / 13 feet

Accuracy - 3mm

Measuring Angle - <15°

Dimension - 45 x 20 x 15mm

◆ PINOUT

• VCC: Connected to Arduino 5V.

• GND: Connected to Arduino GND.


https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 8/29
7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

• 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

It emits an ultrasound at a frequency of 40,000 Hz, which propagates through


the air. If there's an obstruction in its path, the signal reflects back to the
module. By accounting for the travel time and the speed of sound, one can
accurately determine the distance.

◆ 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:

Distance = Speed x Time

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.

Distance = Speed x Time / 2

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 9/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

We need to convert the speed of sound, 343 m/s, to cm/µs


(centimeter/microsecond) since all our other values are in that unit. We do
that by multiplying the value with 100/1000000. This gives us 0.0343 cm/µs.
Rounding off the value, we get 0.034 cm/µs.

Distance = 0.034 x Time / 2

➩ DC motors (BO motors):

A BO motor is essentially a DC motor with gears. The gearing results in lower


speeds compared to standard DC motors. Despite utilizing the same DC motor
for rotation, the presence of gear sets significantly enhances torque
compared to the DC motor.

We will be using L-shaped BO motor in our car.

The BO motors (Brushed DC) are the driving force behind our car.

➩ Chassis or Frame for the 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:

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 10/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

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.

I used a similar caster wheel available at link (https://robu.in/product/wheel-


of-metal/).

Step 1: Designing the Circuit

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.

Tinkercad (www.tinkercad.com (http://www.tinkercad.com/)) offers a drag-


and-drop interface and a wide range of components, making circuit creation a
breeze. Its built-in simulation feature enables real-time testing and
troubleshooting. Whether you're a hobbyist or a seasoned engineer
prototyping a new project, Tinkercad's user-friendly interface simplifies circuit
design. If you're new to electronics and curious to learn, I highly recommend
giving Tinkercad a try!

As mentioned earlier, I'll be using a motor shield.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 11/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

Step 2: Constructing the Car Chassis

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).

I recommend planning ahead for the placement of the three ultrasonic


sensors. This foresight can save you from potential struggles during the
assembly process that I encountered.

To mount the motors securely, I highly recommend screwing or fastening


them tightly to the chassis. I opted for screwing. To do this, I attached a
lightweight multiwood block under the chassis using wood glue to provide a
stable base for the screws.

If you're purchasing your chassis, ensure it meets your specific requirements.


Look for one designed to accommodate two motors at the back, a caster
wheel at the front, an Arduino, a breadboard, and three ultrasonic sensors. I
https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 12/29
7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

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

Step 3: Assembling Components Onto the Chassis

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.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 13/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

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.

Arduino and Breadboard

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:

Sensor-1: Positioned to face straight forward.


Sensor-2: Placed right next to Sensor-1 at a 45° angle.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 14/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

Sensor-3: Positioned opposite to Sensor-2, right next to Sensor-1, also at a


45° angle.

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)

An external power source might be necessary for your motor shield,


depending on its type. While some motor shields draw power from the
Arduino itself, this can result in a decrease in motor speed due to the
Arduino's limited 5V output power. However, if you're using the L293D IC
directly, external power will be required and should be inputted to pin 8 for
the motor to run.

You can just use double-sided tape to attach it to the chassis.

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.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 15/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

Step 4: Wiring

Now comes the hard part, wiring.

Wiring the Sensors

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

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 16/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

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...

Wiring the Motors

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.

The connections for the L293D IC Arduino are as follows:

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 17/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

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.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 18/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

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.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 19/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

// 1. Define motor pins


const int MotorL_pin1 = 2;
const int MotorL_pin2 = 3;
const int MotorR_pin1 = 4;
const int MotorR_pin2 = 5;

// 2. Define ultrasonic sensor pins


const int trig1 = 10;
const int echo1 = 9;
const int trig2 = 8;
const int echo2 = 7;
const int trig3 = 11;
const int echo3 = 12;

// 3. Variables for calculating distances


int distance1;
int distance2;
int distance3;
long duration;

// 4. Function to stop both motors


void Stop() {
digitalWrite(MotorL_pin1, LOW);
digitalWrite(MotorL_pin2, LOW);
digitalWrite(MotorR_pin1, LOW);
digitalWrite(MotorR_pin2, LOW);
}

// 5. Function to move both motors forward


void Forward() {
digitalWrite(MotorL_pin1, HIGH);
digitalWrite(MotorL_pin2, LOW);
digitalWrite(MotorR_pin1, HIGH);
digitalWrite(MotorR_pin2, LOW);
}

// 6. Function to turn left


void Left() {
digitalWrite(MotorL_pin1, HIGH);
digitalWrite(MotorL_pin2, LOW);
digitalWrite(MotorR_pin1, LOW);
digitalWrite(MotorR_pin2, LOW);
}

// 7. Function to turn right


void Right() {
digitalWrite(MotorL_pin1, LOW);
digitalWrite(MotorL_pin2, LOW);
digitalWrite(MotorR_pin1, HIGH);
digitalWrite(MotorR_pin2, LOW);
}

void setup() {
Serial.begin(9600);

// 8. Set motor pins as outputs


pinMode(MotorL_pin1, OUTPUT);
pinMode(MotorL_pin2, OUTPUT);
pinMode(MotorR_pin1, OUTPUT);
pinMode(MotorR_pin2, OUTPUT);

// 9. Set ultrasonic sensor pins


pinMode(trig1, OUTPUT);
pinMode(echo1, INPUT);
pinMode(trig2, OUTPUT);
pinMode(echo2, INPUT);
pinMode(trig3, OUTPUT);
pinMode(echo3, INPUT);
}
https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 20/29
7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

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;

// 11. Ultrasonic sensor 2


digitalWrite(trig2, LOW);
delayMicroseconds(2);
digitalWrite(trig2, HIGH);
delayMicroseconds(10);
digitalWrite(trig2, LOW);
duration = pulseIn(echo2, HIGH);
distance2 = duration * 0.034 / 2;

// 12. Ultrasonic sensor 3


digitalWrite(trig3, LOW);
delayMicroseconds(2);
digitalWrite(trig3, HIGH);
delayMicroseconds(10);
digitalWrite(trig3, LOW);
duration = pulseIn(echo3, HIGH);
distance3 = duration * 0.034 / 2;

// 13. Print distances


// Serial.print("Distance1: "); Serial.println(distance1);
// Serial.print("Distance2: "); Serial.println(distance2);
// Serial.print("Distance3: "); Serial.println(distance3);

// 14. Control movement based on distances


if (distance1 < 4 || distance2 < 4 || distance3 < 4) {
Stop(); // If any sensor detects an obstacle too close, stop.
} else if (distance2 < 25 && distance3 < 25) {
Forward(); // If object in front of sensor 2 and sensor 3, move forward.
} else if (distance1 < 25) {
Forward(); // If object in front, move forward.
} else if (distance2 < 25) {
Left(); // If object to the left, turn left.
} else if (distance3 < 25) {
Right(); // If object to the right, turn right.
} else {
Stop(); // Otherwise, stop (no object detected).
}
}

𝐂𝐨𝐝𝐞 𝐄𝐱𝐩𝐥𝐚𝐧𝐚𝐭𝐢𝐨𝐧

1. Define Motor Pins

In this section also, we are assigning variables to the pins where we


connected our L293D's pins 2, 7, 10, and 15. It just makes the code easier to
read.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 21/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

2. Define ultrasonic sensor pins

In this section also we are assigning variables. But to the pins where we
connected our three ultrasonic sensors' Trig and Echo pins.

3. Variables for calculating distances

In this section, we are declaring variables needed to calculate the distance


values from the three HC-SR04 sensors.

4. Function to stop both motors

Here, we are defining a function named

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.

To do this, we use the

digitalWrite();

command to set both pins of both motors to LOW.

5. Function to move both motors forward

Here, we are defining another function named

Forward();

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 22/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

to move both motors forward. This is also achieved by using the

digitalWrite();

command to set pin 1 of both motors to HIGH.

6. Function to turn left

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.

7. Function to turn right

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.

8. Set motor pins as outputs

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

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 23/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

signals from it.

9. Set ultrasonic sensor pins

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.

10. Ultrasonic sensor 1

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.

We begin by using the

digitalWrite();

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 24/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

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.

11. Ultrasonic sensor 2

In this section, we perform the same steps as we did for sensor 1, but for the
second sensor.

12. Ultrasonic sensor 3

Here also we do the same as before, but for the third sensor.

13. Print distances

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.

14. Control movement based on distances

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 25/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

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:

(distance1 < 4 || distance2 < 4 || distance3 < 4)

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.

» The second condition:

(distance2 < 25 && distance3 < 25)

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.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 26/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

» In the third condition, we check if the distance value from sensor 1 is less
than 25 cm:

(distance1 < 25)

If it is true, we call the forward function to make the car go forward.

» In the fourth condition, we check the distance between sensor 2 and the
object:

(distance2 < 25)

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:

(distance3 < 25)

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.

I recommend experimenting with changing these values in the conditions. I


used 25 as an average distance, but you can adjust it according to your
requirements.

Step 6: Working

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 27/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

Here are the videos of the car running and distance value printing on the
serial monitor.

Autonomous Hand-Following Car: Download (https://ibles-


Arduino and Ultrasonic Sensors content.tinkercad.com/FBR/25SD/LTLIY1
Powered - Step #6 WR/FBR25SDLTLIY1WR.mp4)
Autonomous Hand-Following Car: Download (https://ibles-
Arduino and Ultrasonic Sensors content.tinkercad.com/FZM/HWOD/LTLI
Powered - Step #6 Y1XG/FZMHWODLTLIY1XG.mp4)

Step 7: Testing and Validation

Functional Testing: Verify that all components are working correctly


together. Test each feature and functionality to ensure they operate as
intended.
Performance Testing: Evaluate the performance of your project. Check for
any delays, inaccuracies, or unexpected behavior in its operation.
Troubleshooting: Address any issues or bugs that arise during testing.
Debug and refine your project as needed to improve its reliability and
performance. Feel free to ask me if you encounter any difficulties.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 28/29


7/26/24, 11:31 AM Hand-Follow ing Car: Arduino and Ultrasonic Sensors Pow ered

Some Troubleshooting Tips

‣ 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 you have a multimeter, test the functionality of your jumper wires by


placing the terminals on both ends of the wire. This is particularly useful for
older jumper wires. Additionally, you can use a multimeter to verify if all pins
are connected properly.

‣ 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.

‣ If any sensors aren't functioning, try running a program to calculate the


distance value from each ultrasonic sensor individually. Run one sensor at a
time by changing the pin numbers in the code accordingly. Then, observe the
Serial Monitor to ensure that each sensor is printing values correctly.

‣ Just to be safe, consider securing the jumper connections with masking


tape. This helps prevent accidental disconnections during operation.

https://w w w .tinkercad.com/projects/Autonomous-Hand-Follow ing-Car-Arduino-and-Ultrason 29/29

You might also like