Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
pat
Published © LGPL

Line Following Car

Build a line-following Arduino car with IR sensors! Perfect for robotics enthusiasts. Dive into automation with this hands-on project.

BeginnerFull instructions provided244
Line Following Car

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
DC Motor, 12 V
DC Motor, 12 V
×2
Dual H-Bridge motor drivers L293D
Texas Instruments Dual H-Bridge motor drivers L293D
×1
IR Range Sensor
Digilent IR Range Sensor
×1
AA Batteries
AA Batteries
×3
USB Cable 2.0 Type A/B for Arduino Uno
DIYables USB Cable 2.0 Type A/B for Arduino Uno
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

10 Pc. Jumper Wire Kit, 5 cm Long
10 Pc. Jumper Wire Kit, 5 cm Long
Solder Wire, Lead Free
Solder Wire, Lead Free
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Schematics

Code

Code

C/C++
#include <AFMotor.h>

// Initialize motors
AF_DCMotor leftMotor(1); // Motor 1 connected to M1
AF_DCMotor rightMotor(4); // Motor 2 connected to M4

// IR sensor pins
const int leftIR = A0; // Use analog pin A0 for the left IR sensor
const int rightIR = A1; // Use analog pin A1 for the right IR sensor

void setup() {
  pinMode(leftIR, INPUT);
  pinMode(rightIR, INPUT);
  leftMotor.setSpeed(200); // Set initial speed values
  rightMotor.setSpeed(200); // Set initial speed values
}

void loop() {
  int leftValue = analogRead(leftIR); // Read left IR sensor value
  int rightValue = analogRead(rightIR); // Read right IR sensor value

  if (leftValue == LOW && rightValue == LOW) {
    moveForward(); // Move forward if both sensors detect the line
  } else if (leftValue == LOW && rightValue == HIGH) {
    turnLeft(); // Turn left if the left sensor detects the line
  } else if (leftValue == HIGH && rightValue == LOW) {
    turnRight(); // Turn right if the right sensor detects the line
  } else {
    moveStop(); // Stop if no line is detected
  }
}

void moveForward() {
  leftMotor.run(FORWARD);
  rightMotor.run(FORWARD);
}

void moveBackward() {
  leftMotor.run(BACKWARD);
  rightMotor.run(BACKWARD);
}

void turnLeft() {
  leftMotor.run(BACKWARD);
  rightMotor.run(FORWARD);
}

void turnRight() {
  leftMotor.run(FORWARD);
  rightMotor.run(BACKWARD);
}

void moveStop() {
  leftMotor.run(RELEASE);
  rightMotor.run(RELEASE);
}

Credits

pat
13 projects • 4 followers
AI Convergence Engineering, Software Engineering, ML, deep learning, image processing, computer vision, circuit design, and Edge AI devices.
Contact

Comments

Please log in or sign up to comment.