4.Obstacle Avoidance Robot
Obstacle Avoidance Robot
which incorporates more features and adds interactivity: **"Obstacle Avoidance Robot"**.
Project: Obstacle Avoidance Robot
This project involves building a simple robot that uses an ultrasonic sensor to detect obstacles and navigate around them. The robot can autonomously move in different directions and avoid collisions, making it perfect for exploring how robots can interact with their environment.
Components Needed:
- Arduino board (e.g., Arduino Uno)
- HC-SR04 ultrasonic sensor
- 2 DC motors with motor driver module (e.g., L298N)
- Chassis for the robot
- 9V battery or power supply for motors
- Jumper wires and breadboard
- Wheels and caster wheel for balance
Circuit Connections:
1. **Motor Driver Module (L298N):**
- **Power Supply:**
- Connect the 12V and GND pins to the external battery.
- Connect 5V pin to the Arduino 5V.
- **Motor Connections:**
- Connect the motors to the output pins of the driver module (OUT1, OUT2 for Motor A; OUT3, OUT4 for Motor B).
- **Control Pins:**
- Connect the control pins (IN1, IN2, IN3, IN4) to digital pins on the Arduino (e.g., 3, 4, 5, 6).
2. **HC-SR04 Ultrasonic Sensor:**
- VCC to 5V on Arduino
- GND to GND on Arduino
- TRIG to a digital pin (e.g., pin 9)
- ECHO to another digital pin (e.g., pin 10)
Code:
The code below will control the robot, allowing it to move forward, and it will turn away from obstacles when detected by the ultrasonic sensor.
cpp
#define TRIG_PIN 9
#define ECHO_PIN 10
#define IN1 3
#define IN2 4
#define IN3 5
#define IN4 6
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop() {
long duration, distance;
// Send a 10us pulse to trigger
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo time
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance in cm
distance = duration * 0.034 / 2;
if (distance < 20) {
// Obstacle detected, turn to avoid
stop();
delay(200);
turnRight();
delay(500);
} else {
// Move forward
moveForward();
}
}
void moveForward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void stop() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void turnRight() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
```
How It Works:
1. **Ultrasonic Sensor:** The HC-SR04 continuously measures the distance to detect obstacles in front of the robot.
2. **Motor Driver Control:** The motor driver module is used to control the motors based on the signals from the Arduino.
3. **Obstacle Avoidance Logic:**
- If an obstacle is detected within 20 cm, the robot stops and turns right.
- Otherwise, it keeps moving forward.
Additional Features:
- **Enhanced Navigation:** Implement algorithms like left-right scanning to find the best route around obstacles.
- **Speed Control:** Add a PWM signal to control motor speed for smoother movement.
- **Multiple Sensors:** Use multiple ultrasonic sensors for more accurate obstacle detection (front, left, right).
Here is the step-by-step procedure for building the **Obstacle Avoidance Robot** project:
### Step 1: Gather Materials
Make sure you have the following components ready:
- Arduino Uno (or similar board)
- HC-SR04 ultrasonic sensor
- L298N motor driver module
- 2 DC motors
- Robot chassis with wheels
- 9V battery or power supply
- Jumper wires and breadboard
- Caster wheel for stability
### Step 2: Assemble the Robot Chassis
1. **Attach Motors to Chassis:**
- Fix the DC motors to the chassis using screws or holders.
- Attach the wheels to the motors.
2. **Add Caster Wheel:**
- Fix a caster wheel to the front or back of the chassis to help balance the robot.
3. **Mount Components:**
- Attach the L298N motor driver and Arduino to the chassis securely.
- Position the ultrasonic sensor at the front of the robot for optimal obstacle detection.
### Step 3: Connect Components
1. **Ultrasonic Sensor Connections:**
- **VCC:** Connect to the 5V pin on the Arduino.
- **GND:** Connect to the GND pin on the Arduino.
- **TRIG:** Connect to digital pin 9 on the Arduino.
- **ECHO:** Connect to digital pin 10 on the Arduino.
2. **Motor Driver (L298N) Connections:**
- **Power Supply:**
- Connect the external power supply (e.g., 9V battery) to the 12V and GND terminals of the motor driver.
- Connect the 5V output from the L298N to the 5V pin on the Arduino to power the board.
- **Motor Outputs:**
- Connect **OUT1** and **OUT2** to one motor.
- Connect **OUT3** and **OUT4** to the other motor.
- **Control Pins:**
- Connect **IN1** to digital pin 3 on the Arduino.
- Connect **IN2** to digital pin 4 on the Arduino.
- Connect **IN3** to digital pin 5 on the Arduino.
- Connect **IN4** to digital pin 6 on the Arduino.
### Step 4: Power Connections
- Connect a 9V battery or appropriate power supply to power the motors through the motor driver.
- Make sure the Arduino is powered through either USB (for testing) or via the 5V output of the motor driver module.
### Step 5: Upload the Code
1. **Install the Arduino IDE:**
- If you haven’t already, download and install the Arduino IDE from the official website.
2. **Write the Code:**
- Use the code provided above, which allows the robot to move forward and avoid obstacles by turning right when needed.
3. **Upload the Code:**
- Connect the Arduino to your computer via a USB cable.
- Upload the code to the Arduino board.
### Step 6: Test the Robot
1. **Position the Robot:**
- Place the robot on the floor in an open area.
2. **Power On:**
- Turn on the power supply, and watch as the robot moves forward.
- The robot should detect obstacles within 20 cm and turn away to avoid collisions.
### Step 7: Troubleshooting
1. **Motors Not Working:**
- Check motor driver connections.
- Make sure the battery is providing sufficient power to the motors.
2. **Sensor Issues:**
- Ensure the ultrasonic sensor is positioned at the front and connected properly.
- If the distance readings are inaccurate, check the wiring of TRIG and ECHO pins.
3. **Robot Not Avoiding Obstacles:**
- Make sure the logic in the code matches the wiring.
- Ensure the control pins are correctly set to operate the motors.
### Step 8: Additional Improvements (Optional)
1. **Fine-Tune Distance Threshold:**
- Adjust the obstacle distance threshold in the code to improve performance.
2. **Add More Sensors:**
- Add additional ultrasonic sensors for better awareness (e.g., left and right).
3. **Adjust Motor Speed:**
- Implement PWM control for the motors to adjust speed and enhance turning smoothness.
4. **Add an LCD or LEDs:**
- Add an LCD to display sensor readings or LEDs to indicate when the robot is turning.
### Summary
By following these steps, you can successfully build an **Obstacle Avoidance Robot**. The ultrasonic sensor detects obstacles, and the Arduino sends commands to the motor driver to control the robot’s movements. This project helps you learn about sensors, motor control, and autonomous navigation.
Comments
Post a Comment