
How to Make an Obstacle-Avoiding Robot using Arduino Uno, L298 Motor Driver, and Ultrasonic Sensor
In this tutorial, we will guide you through the process of building an obstacle-avoiding robot using an Arduino Uno, L298 motor driver, and an ultrasonic sensor. This robot will be able to detect obstacles and navigate around them automatically.
Materials Needed:
1. Arduino Uno board
2. L298 motor driver
3. Ultrasonic sensor (HC-SR04)
4. DC motors (2)
5. Chassis (robot body)
6. Wheels (2)
7. Jumper wires
8. 9V battery and battery clip
9. Breadboard (optional)
10. 2x motors and wheels for the robot
Step 1: Wiring the Motors and L298 Motor Driver
First, we need to wire the motors to the L298 motor driver. Here’s how:
• Motor A:
• Connect the two terminals of the first motor to the Out1 and Out2 pins of the L298 motor driver.
• Motor B:
• Connect the two terminals of the second motor to the Out3 and Out4 pins of the L298 motor driver.
• Power:
• Connect the 12V pin on the L298 to the 9V battery for motor power.
• Connect the GND pin on the L298 to the negative terminal of the battery.
• Control Pins:
• Connect IN1 and IN2 to Arduino digital pins (e.g., pin 6 and 7).
• Connect IN3 and IN4 to Arduino digital pins (e.g., pin 8 and 9).
• Enable Pins:
• Connect ENA to the 5V pin on Arduino for Motor A.
• Connect ENB to the 5V pin on Arduino for Motor B.
Step 2: Wiring the Ultrasonic Sensor (HC-SR04)
Next, connect the ultrasonic sensor to the Arduino for obstacle detection:
• VCC pin of the ultrasonic sensor to the 5V pin of Arduino.
• GND pin of the ultrasonic sensor to the GND pin of Arduino.
• TRIG pin of the ultrasonic sensor to an Arduino digital pin (e.g., pin 10).
• ECHO pin of the ultrasonic sensor to an Arduino digital pin (e.g., pin 11).
Step 3: Writing the Code
Now, let’s write the code for the obstacle-avoiding robot. Open Arduino IDE and upload the following code:
//sensor pins
const = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name
void setup(){
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
servo_motor.attach(8); //our servo pinservo_motor.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
Step 4: Uploading the Code to the Arduino
After writing the code, upload it to the Arduino Uno using the Arduino IDE.
Step 5: Testing the Robot
Now that everything is set up:
1. Power on the robot by connecting the battery.
2. The robot will start moving forward.
3. When it detects an obstacle, it will stop, turn right, and continue moving forward.
Conclusion
Congratulations! You’ve just built an obstacle-avoiding robot using Arduino Uno, L298 motor driver, and an ultrasonic sensor. You can modify the robot’s behavior by adjusting the code, such as changing the movement patterns or adding additional sensors for better obstacle detection.
I hope this step-by-step guide is helpful for your website! Feel free to reach out if you need any more details or modifications.
Leave a Reply