Build An Obstacle Avoiding Robot Using Arduino & Microcontroller
Obstacle Avoiding Robot Using Microcontroller (Arduino)
Robots have been used in many applications to help people with tasks and to make life easier. One of the most popular types of robots is the obstacle avoiding robot. This type of robot has been used for a variety of tasks from robotic vacuums to industrial manufacturing.
An obstacle avoiding robot can be programmed to detect obstacles in its path and take evasive action. This can be done by using sensors such as infrared, ultrasonic, or laser range finders. The robot then uses an onboard microcontroller to process the signals from the sensors and make decisions about its actions.
In this article we will look at how to create an obstacle avoiding robot using a microcontroller such as the Arduino. We will cover the components needed, the code required, and the construction of the robot. By the end of this article you should be able to build your own obstacle avoiding robot.
Components Needed
- Arduino Uno
- Breadboard
- DC Motors
- Motor Driver/Controller
- L293D Motor Driver IC
- Pulse Rate Modulation (PWM) Control
- Ultrasonic Sensor
- Battery Pack
- Power Supply
Programming the Arduino
The first step in programming the Arduino is to write the code. In this example, we are using the ultrasonic sensor to detect any obstacles in front of the robot. We then use the motor driver and the Arduino to control the direction and speed of the motors in order to avoid any obstacles detected.
The code for this example is fairly straightforward. We begin by initializing the motor pins and setting up the ultrasonic sensor. Then we create a loop that continuously checks for any obstacles in front of the robot and adjusts the speed and direction of the motors accordingly.
Building the Robot
Once you have written the code, the next step is to build the robot. Start by connecting the motors to the motor driver, and the motor driver to the Arduino. You can then connect the ultrasonic sensor to the Arduino and the battery pack to the power supply.
The last step is to test your robot and make any necessary adjustments. Make sure that the robot is able to sense obstacles and adjust its direction and speed accordingly. Once you are satisfied with your robot's performance, you are ready to start testing it in the real world!
Here's an example source code for an obstacle avoiding robot using a microcontroller (Arduino):
#include <AFMotor.h>
#define trigPin A0
#define echoPin A1
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
motor1.setSpeed(150);
motor2.setSpeed(150);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance > 15) {
motor1.run(FORWARD);
motor2.run(FORWARD);
}
else {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
delay(1000);
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(1000);
}
}
In this example, the AFMotor.h library is used to control the two DC motors. The trigPin and echoPin are defined as the pins used for the ultrasonic sensor. The motor1 and motor2 objects are created for the two DC motors.
In the setup() function, the trigPin is set as an output and the echoPin is set as an input. The speed of both motors is set to 150 using the setSpeed() function.
In the loop() function, the duration and distance variables are declared to store the duration of the ultrasonic pulse and the distance measured by the sensor. The digitalWrite() and delayMicroseconds() functions are used to trigger the ultrasonic sensor and measure the pulse duration. The distance is calculated using the speed of sound and the duration of the pulse.
If the measured distance is greater than 15cm, both motors are set to run forward using the run() function with the FORWARD parameter.
If the measured distance is less than or equal to 15cm, both motors are set to run backward using the run() function with the BACKWARD parameter. The robot then pauses for 1 second before turning to avoid the obstacle. The run() function is used again to set one motor to run forward and the other to run backward to turn the robot. Finally, the robot pauses for another 1 second before continuing to move forward.