Line-Following Robot With Obstacle Avoidance

by AustinS89 in Circuits > Arduino

541 Views, 1 Favorites, 0 Comments

Line-Following Robot With Obstacle Avoidance

circuit_image (30).png

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.

  1. Project LinkLine-Following Robot With Obstacle Avoidance
  2. DocumentationProject Documentation

Supplies

  1. 1 x Arduino UNO
  2. 1 x L298N DC Motor Driver
  3. 2 x Hobby Gearmotor with 48:1 gearbox
  4. 1 x HC-SR04 Ultrasonic Sensor
  5. 2 x IR Sensor Modules
  6. 1 x 4 x AAA Battery Holder
  7. 4 x AAA Batteries
  8. 1 x Rocker Switch
  9. Jumper Wires
  10. Breadboard or Perfboard (Optional)
  11. Robot Chassis (Optional)
  12. 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

F6TXPPYM1RVOEUU.png

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

  1. 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:

  1. Battery to Rocker Switch:
  2. Connect the positive wire (+) from the battery holder to the input terminal of the rocker switch.
  3. 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.
  4. Rocker Switch to L298N:
  5. Connect the output terminal of the rocker switch to the 12V input pin on the L298N motor driver.
  6. L298N to Arduino UNO:
  7. 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:

  1. Motor A (Left Motor):
  2. Connect OUT1 and OUT2 on the L298N to the two terminals of the left gearmotor.
  3. Motor B (Right Motor):
  4. Connect OUT3 and OUT4 on the L298N to the two terminals of the right gearmotor.

Control Connections:

  1. Enable Pins:
  2. Connect ENA to digital pin D5 on the Arduino UNO.
  3. Connect ENB to digital pin D6 on the Arduino UNO.
  4. Input Pins:
  5. Connect IN1 to digital pin D12 on the Arduino UNO.
  6. Connect IN2 to digital pin D13 on the Arduino UNO.
  7. Connect IN3 to digital pin D10 on the Arduino UNO.
  8. Connect IN4 to digital pin D11 on the Arduino UNO.

3. Connecting the IR Sensors

First IR Sensor (Left Sensor):

  1. VCC to 5V on the Arduino UNO.
  2. GND to GND on the Arduino UNO.
  3. OUT to digital pin D4 on the Arduino UNO.

Second IR Sensor (Right Sensor):

  1. VCC to 5V on the Arduino UNO.
  2. GND to GND on the Arduino UNO.
  3. OUT to digital pin D2 on the Arduino UNO.

4. Connecting the HC-SR04 Ultrasonic Sensor

  1. VCC to 5V on the Arduino UNO.
  2. GND to GND on the Arduino UNO.
  3. TRIG to digital pin D9 on the Arduino UNO.
  4. ECHO to digital pin D8 on the Arduino UNO.

5. Power Supply Setup

  1. Insert four AAA batteries into the battery holder.
  2. Ensure the rocker switch is in the "off" position before connecting.
  3. 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:

  1. Connect Your Arduino UNO:
  2. Use a USB cable to connect your Arduino UNO to your computer.
  3. Open the Code Tab:
  4. In the Cirkit Designer IDE, navigate to the Code tab.
  5. Select the USB Port:
  6. Click on the Port button within the IDE.
  7. Select the USB port that corresponds to your connected Arduino UNO.
  8. Upload the Code:
  9. Click the Upload button.
  10. The Cirkit Designer IDE will compile and upload the code directly to your Arduino UNO.
  11. 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.

  1. Prepare the Test Surface:
  2. Create a track with a black line on a white background (or vice versa) for the robot to follow.
  3. Ensure the line is wide enough for the IR sensors to detect.
  4. Power On the Robot:
  5. Turn on the rocker switch to supply power to the circuit.
  6. Observe the Robot's Behavior:
  7. The robot should move forward while following the line.
  8. If an obstacle is detected within 10 cm, the robot should stop.
  9. Troubleshooting:
  10. If the robot doesn't move, check the motor connections and ensure the batteries are charged.
  11. 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.


// Define pins for motor speed control (EnA and EnB)
int VelocidadMotor1 = 6;
int VelocidadMotor2 = 5;

// Define control pins for motor direction (In1, In2, In3, In4)
int Motor1A = 13;
int Motor1B = 12;
int Motor2C = 11;
int Motor2D = 10;

// Infrared sensors - left and right
int infraPin = 2; // Right IR sensor
int infraPin1 = 4; // Left IR sensor

// Variables to capture sensor values: 0 - light background, 1 - black line
int valorInfra = 0;
int valorInfra1 = 0;

// Ultrasonic sensor pins
#define PIN_TRIG 9
#define PIN_ECHO 8

long duration, distance;

// Initial setup
void setup() {
Serial.begin(9600);
delay(1000);
// Configure ultrasonic sensor
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
// Set up IR sensor pins
pinMode(infraPin, INPUT);
pinMode(infraPin1, INPUT);

// Set up motor control pins
pinMode(Motor1A,OUTPUT);
pinMode(Motor1B,OUTPUT);
pinMode(Motor2C,OUTPUT);
pinMode(Motor2D,OUTPUT);
pinMode(VelocidadMotor1, OUTPUT);
pinMode(VelocidadMotor2, OUTPUT);

// Set initial motor speeds
analogWrite(VelocidadMotor1, 150);
analogWrite(VelocidadMotor2, 180);

// Initialize motor direction
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, LOW);
}

// Main loop
void loop() {
// Ultrasonic sensor measurement
digitalWrite(PIN_TRIG, LOW);
delayMicroseconds(5);
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
duration = pulseIn(PIN_ECHO, HIGH);
distance = (duration / 2) / 29.1; // Convert time to distance in cm
Serial.print("Distance to object: ");
Serial.print(distance);
Serial.println(" cm.");
// Read IR sensor values
valorInfra = digitalRead(infraPin); // Right sensor
valorInfra1 = digitalRead(infraPin1); // Left sensor

Serial.print("Right Sensor: ");
Serial.println(valorInfra);
Serial.print("Left Sensor: ");
Serial.println(valorInfra1);

if (distance > 10){ // If no obstacle is detected within 10 cm
// Move forward
if(valorInfra == 0 && valorInfra1 == 0){
Serial.println("Moving Forward");
// Set motors to move forward
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor2D, HIGH);
delay(20);
digitalWrite(Motor1A, LOW);
digitalWrite(Motor2D, LOW);
delay(20);
}
// Turn right
else if(valorInfra == 0 && valorInfra1 == 1){
Serial.println("Turning Right");
digitalWrite(Motor1A, LOW);
digitalWrite(Motor2D, LOW);
delay(25);
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor2D, LOW);
delay(20);
}
// Turn left
else if(valorInfra == 1 && valorInfra1 == 0){
Serial.println("Turning Left");
digitalWrite(Motor1A, LOW);
digitalWrite(Motor2D, LOW);
delay(25);
digitalWrite(Motor1A, LOW);
digitalWrite(Motor2D, HIGH);
delay(20);
}
// Stop at end of line
else if(valorInfra == 1 && valorInfra1 == 1){
Serial.println("Line End Detected");
// Stop motors
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, LOW);
}
}
else{
Serial.println("Obstacle Detected - Stopping");
// Stop motors
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, LOW);
}
}


Notes:

  1. Motor Speed Adjustments:
  2. You can adjust the values in analogWrite(VelocidadMotor1, 150); and analogWrite(VelocidadMotor2, 180); to change motor speeds.
  3. Sensor Calibration:
  4. If the robot isn't following the line correctly, you may need to calibrate the IR sensors.

How It Works

IR Sensors:

  1. The two IR sensors detect the presence of a line beneath them.
  2. When the sensor detects a black line (high contrast), it changes its output.
  3. The code uses these readings to determine if the robot needs to turn left, right, or move forward.

Ultrasonic Sensor:

  1. The HC-SR04 ultrasonic sensor measures the distance to objects in front of the robot.
  2. If an obstacle is detected within 10 cm, the robot stops moving to avoid collision.

Motor Control:

  1. The L298N motor driver controls the speed and direction of the two DC motors.
  2. By adjusting the input pins and the enable pins, the robot can move forward, turn left, or turn right.

Control Logic:

  1. Moving Forward:
  2. Both IR sensors detect the white surface (no line detected).
  3. The robot moves forward by setting both motors to move in the forward direction.
  4. Turning Left:
  5. The left IR sensor detects the line while the right does not.
  6. The robot adjusts by turning left.
  7. Turning Right:
  8. The right IR sensor detects the line while the left does not.
  9. The robot adjusts by turning right.
  10. Stopping:
  11. Both IR sensors detect the line (end of the line) or an obstacle is detected.
  12. The robot stops by setting all motor control pins to LOW.

Additional Tips

Sensor Calibration:

  1. Adjust the sensitivity of the IR sensors using the potentiometer on the module if available.

Power Supply:

  1. Ensure your batteries are fully charged. Low voltage can affect performance.

Wiring:

  1. Double-check all connections for errors or loose wires.

Serial Monitor:

  1. 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.