Line-Following Robot With Obstacle Avoidance
by AustinS89 in Circuits > Arduino
541 Views, 1 Favorites, 0 Comments
Line-Following Robot With Obstacle Avoidance
In this project, we'll build a line-following robot with obstacle avoidance capabilities using an Arduino UNO. The robot uses two infrared (IR) sensors to detect and follow a line and an ultrasonic sensor to detect obstacles in its path. When an obstacle is detected within a certain distance, the robot stops to avoid collision.
This project was created by alda10076 using Cirkit Designer. Full credit goes to alda10076 for designing and sharing this innovative project with the community.
- Project Link: Line-Following Robot With Obstacle Avoidance
- Documentation: Project Documentation
Supplies
- 1 x Arduino UNO
- 1 x L298N DC Motor Driver
- 2 x Hobby Gearmotor with 48:1 gearbox
- 1 x HC-SR04 Ultrasonic Sensor
- 2 x IR Sensor Modules
- 1 x 4 x AAA Battery Holder
- 4 x AAA Batteries
- 1 x Rocker Switch
- Jumper Wires
- Breadboard or Perfboard (Optional)
- Robot Chassis (Optional)
- Wheels and Caster Wheel
Circuit Design
The complete circuit diagram and code are available in the Cirkit Designer IDE. You can view, edit, and interact with the project directly through the provided link.
View the Circuit Diagram and Code: Open the project link in Cirkit Designer to access the detailed schematic and code.
Assemble the Circuit
Follow these detailed instructions to assemble the circuit. Refer to the circuit diagram in the Cirkit Designer IDE for visual guidance.
1. Setting Up the Arduino UNO
- Place your Arduino UNO on your workspace or mount it onto your robot chassis if you're using one.
2. Connecting the L298N Motor Driver
Power Connections:
- Battery to Rocker Switch:
- Connect the positive wire (+) from the battery holder to the input terminal of the rocker switch.
- Connect the negative wire (−) from the battery holder to the GND pin on the L298N motor driver and the GND pin on the Arduino UNO.
- Rocker Switch to L298N:
- Connect the output terminal of the rocker switch to the 12V input pin on the L298N motor driver.
- L298N to Arduino UNO:
- Connect the 5V output pin on the L298N motor driver to the 5V pin on the Arduino UNO. This powers the Arduino from the motor driver's voltage regulator.
Motor Connections:
- Motor A (Left Motor):
- Connect OUT1 and OUT2 on the L298N to the two terminals of the left gearmotor.
- Motor B (Right Motor):
- Connect OUT3 and OUT4 on the L298N to the two terminals of the right gearmotor.
Control Connections:
- Enable Pins:
- Connect ENA to digital pin D5 on the Arduino UNO.
- Connect ENB to digital pin D6 on the Arduino UNO.
- Input Pins:
- Connect IN1 to digital pin D12 on the Arduino UNO.
- Connect IN2 to digital pin D13 on the Arduino UNO.
- Connect IN3 to digital pin D10 on the Arduino UNO.
- Connect IN4 to digital pin D11 on the Arduino UNO.
3. Connecting the IR Sensors
First IR Sensor (Left Sensor):
- VCC to 5V on the Arduino UNO.
- GND to GND on the Arduino UNO.
- OUT to digital pin D4 on the Arduino UNO.
Second IR Sensor (Right Sensor):
- VCC to 5V on the Arduino UNO.
- GND to GND on the Arduino UNO.
- OUT to digital pin D2 on the Arduino UNO.
4. Connecting the HC-SR04 Ultrasonic Sensor
- VCC to 5V on the Arduino UNO.
- GND to GND on the Arduino UNO.
- TRIG to digital pin D9 on the Arduino UNO.
- ECHO to digital pin D8 on the Arduino UNO.
5. Power Supply Setup
- Insert four AAA batteries into the battery holder.
- Ensure the rocker switch is in the "off" position before connecting.
- After all connections are made, you can use the rocker switch to turn the robot on and off.
Note: Ensure all GND connections are connected together to create a common ground.
Upload the Code Using Cirkit Designer
The code for this project is included with the Cirkit Designer project link. You can upload it directly to your Arduino UNO without the need for any additional software.
Steps to Upload the Code:
- Connect Your Arduino UNO:
- Use a USB cable to connect your Arduino UNO to your computer.
- Open the Code Tab:
- In the Cirkit Designer IDE, navigate to the Code tab.
- Select the USB Port:
- Click on the Port button within the IDE.
- Select the USB port that corresponds to your connected Arduino UNO.
- Upload the Code:
- Click the Upload button.
- The Cirkit Designer IDE will compile and upload the code directly to your Arduino UNO.
- Wait for the upload to complete. A confirmation message will appear once it's done.
Note: Cirkit Designer automatically manages library installations and configurations, simplifying the upload process.
Test the Robot
Now that your circuit is assembled and the code is uploaded, it's time to test the robot.
- Prepare the Test Surface:
- Create a track with a black line on a white background (or vice versa) for the robot to follow.
- Ensure the line is wide enough for the IR sensors to detect.
- Power On the Robot:
- Turn on the rocker switch to supply power to the circuit.
- Observe the Robot's Behavior:
- The robot should move forward while following the line.
- If an obstacle is detected within 10 cm, the robot should stop.
- Troubleshooting:
- If the robot doesn't move, check the motor connections and ensure the batteries are charged.
- If the robot doesn't follow the line correctly, adjust the positions of the IR sensors.
Code
Below is the Arduino code for the line-following robot with obstacle avoidance. You can also access and edit this code directly in the Cirkit Designer IDE.
Notes:
- Motor Speed Adjustments:
- You can adjust the values in analogWrite(VelocidadMotor1, 150); and analogWrite(VelocidadMotor2, 180); to change motor speeds.
- Sensor Calibration:
- If the robot isn't following the line correctly, you may need to calibrate the IR sensors.
How It Works
IR Sensors:
- The two IR sensors detect the presence of a line beneath them.
- When the sensor detects a black line (high contrast), it changes its output.
- The code uses these readings to determine if the robot needs to turn left, right, or move forward.
Ultrasonic Sensor:
- The HC-SR04 ultrasonic sensor measures the distance to objects in front of the robot.
- If an obstacle is detected within 10 cm, the robot stops moving to avoid collision.
Motor Control:
- The L298N motor driver controls the speed and direction of the two DC motors.
- By adjusting the input pins and the enable pins, the robot can move forward, turn left, or turn right.
Control Logic:
- Moving Forward:
- Both IR sensors detect the white surface (no line detected).
- The robot moves forward by setting both motors to move in the forward direction.
- Turning Left:
- The left IR sensor detects the line while the right does not.
- The robot adjusts by turning left.
- Turning Right:
- The right IR sensor detects the line while the left does not.
- The robot adjusts by turning right.
- Stopping:
- Both IR sensors detect the line (end of the line) or an obstacle is detected.
- The robot stops by setting all motor control pins to LOW.
Additional Tips
Sensor Calibration:
- Adjust the sensitivity of the IR sensors using the potentiometer on the module if available.
Power Supply:
- Ensure your batteries are fully charged. Low voltage can affect performance.
Wiring:
- Double-check all connections for errors or loose wires.
Serial Monitor:
- Use the Serial Monitor in the Arduino IDE to read sensor values and debug.
Conclusion
You've successfully built a line-following robot with obstacle avoidance using an Arduino UNO. This project serves as an excellent introduction to robotics, sensor integration, and motor control. Feel free to enhance the robot by adding features like Bluetooth control, additional sensors, or more advanced obstacle avoidance algorithms.
Acknowledgments
All credit for this project goes to alda10076, who designed and shared this project using Cirkit Designer. We appreciate the opportunity to showcase this innovative design and thank alda10076 for contributing to the Cirkit Designer community.