Code explanation of Human following Robot | Arduino programming | diy Human following Robot program
Science & Technology
Introduction
Welcome back to my channel! In this article, we will delve into the intricacies of programming a Human Following Robot using Arduino. If you haven't already watched the previous video discussing the robot's working principle, be sure to check the link in the description to familiarize yourself with the foundational concepts presented there.
Overview of the Ultrasonic Sensor and IR Sensors
To enable our robot to perceive its surroundings, we have incorporated both an ultrasonic sensor for distance measurement and infrared (IR) sensors to detect obstacles. In this program, we will detail how to code these functionalities.
Ultrasonic Sensor Distance Calculation
Setup of Pins: In the loop section of our code, we will first define which pins we will be utilizing and when they should go high or low.
Digital Commands: We will employ the
digitalWrite
command to set the HIGH and LOW states necessary for our distance calculations.Pulse Width Measurement: We will send a pulse from the trigger pin and then read the echo to compute the duration of the pulse return using the
pulseIn
function. This will help us in measuring the distance.Distance Formula: The formula we will use to calculate distance is:
[ \text(Distance) = \frac(\text{Duration) \times \text(Speed of Sound)}(2) ]
Here, we convert the speed of sound into centimeters per microsecond, which gives us approximately 0.034 cm/μs. The division by two is necessary because the measured duration accounts for the travel distance to the obstacle and back.
Display Distance: We will display this calculated distance in the Serial Monitor.
IR Sensors for Navigation
Initialization of Variables: Two additional variables for left and right IR sensors will be defined by utilizing
digitalRead
.Conditions to Move Forward: We will establish conditions based on sensor readings to determine when the robot should move forward. The conditions are:
- Right sensor must be HIGH
- Left sensor must be HIGH
- Distance should be between 10 cm and 30 cm.
If these conditions are true, we will use digitalWrite
to set the inputs to HIGH or LOW for the respective motors, allowing both motors to rotate forward.
Turning Logic
Right Turn: We will create a new
if
condition usingelse if
. If the right sensor detects an object (LOW) while the left sensor is clear (HIGH), the robot will perform a right turn by adjusting the motor inputs accordingly.Left Turn: The same logic can be applied for the left turn, where the left sensor detects an object (LOW), and the right sensor is clear (HIGH).
Stop Logic
To stop the robot effectively:
- Both IR sensors should be HIGH, indicating no obstructions.
- Or the distance should be measured between 1 cm and 10 cm to prevent collisions.
Integrating Servo and Speed Control
Servo Control: To control a servo motor, we will include the Servo library, attach it to a specified pin, and manage its position through a loop which will adjust between 0 and 180 degrees.
Speed Adjustment: We will use PWM (Pulse Width Modulation) for motor speed control by defining two enable pins connected to the motor driver. We will set initial values and allow dynamic adjustments to the motors’ speed.
Finally, after assembling all these code snippets, the program enables our robot to follow humans and avoid obstacles in its path.
For the complete program, I will provide the link in the description for you to download and review.
Keywords
- Human Following Robot
- Arduino Programming
- Ultrasonic Sensor
- IR Sensors
- DigitalWrite
- PWM (Pulse Width Modulation)
- Servo Motor
- Distance Calculation
FAQ
Q1: What is the main function of the ultrasonic sensor in the robot?
A: The ultrasonic sensor measures the distance to nearby objects, allowing the robot to navigate and avoid obstacles.
Q2: How do the IR sensors work?
A: The IR sensors detect the presence of obstacles by sending signals to the Arduino, which interprets these as HIGH or LOW values.
Q3: Why do we divide the distance calculation by two?
A: We divide by two because the duration measured includes the time taken for the ultrasonic wave to travel to the object and back.
Q4: How can I control the robot's speed?
A: The robot's speed can be controlled by adjusting the PWM values sent to the enable pins of the motor driver.
Q5: What happens when both IR sensors read HIGH?
A: If both IR sensors read HIGH, it indicates that there are no detected obstacles, and the robot will continue moving forward.