Skip to the content.

Self-Driving Car

In this project, I have built a self-driving car using an Arduino UNO and Sunfounder’s IoT kit. The goal of the project is to create a small-scale autonomous vehicle that can navigate a predefined track without human intervention. The car is equipped with sensors and actuators to detect and respond to its environment, allowing it to follow the track, avoid obstacles, and make decisions on its own.

Engineer School Area of Interest Degree
Remston D. New York University Computer Science Master of Science

Here is an autonomous self-driving car that I built using ARDUINO UNO and SUNFOUNDER’S tool kit

Self-Driving Car Image

Final Milestone

Since the previous milestone, several accomplishments have been made in the self-driving car project:

1. Implementation of the self-driving functionality: The code was successfully uploaded, and the car can now drive autonomously. It utilizes the IR obstacle avoidance modules and the ultrasonic module to detect and avoid obstacles during its movement.
2. Enhanced obstacle avoidance capabilities: The car is programmed to respond appropriately when obstacles are detected. It can back up, adjust its direction, and continue moving forward while avoiding collisions.

Challenges and Triumphs:

1. Challenges: One of the main challenges faced during this project was fine-tuning the obstacle detection and avoidance algorithm. Ensuring reliable obstacle detection and designing an effective evasion strategy required experimentation and iterative improvements.
2. Triumphs: Overcoming the challenges and achieving a functional self-driving car was a significant triumph. The successful integration of different components, such as the IR modules and ultrasonic module, and the development of the control logic to navigate the car autonomously were notable accomplishments.

Key Topics Learned:

1. Hardware integration: Understanding how to connect and wire different hardware components, such as motors, sensors, and microcontrollers, to create a functional system.
2. Sensor integration and data processing: Learning how to interface with sensors, read their data, and process it to make informed decisions and control the car's movements.
3. Algorithm design: Designing algorithms and control logic to enable autonomous behavior, obstacle detection, and evasion strategies.

Future Learning Goals:

After completing the self-driving car project at BSE, there are several areas to explore and learn further:

1. Advanced autonomous navigation: Diving deeper into advanced algorithms and techniques for autonomous navigation, including path planning, mapping, and localization.
2. Machine learning for self-driving cars: Exploring machine learning approaches, such as deep learning and reinforcement learning, to enhance the car's perception and decision-making capabilities.
3. Real-world applications: Applying the knowledge gained to real-world scenarios and challenges, such as developing self-driving car prototypes for specific environments or tasks.

By continuing to explore these areas, I hope to deepen my understanding of autonomous systems, contribute to the field of self-driving technology, and stay up to date with the latest advancements in the industry.

Second Milestone

First Milestone: Self-Driving Car Assembly and Basic Functionality

Code

The car will drive freely once the code has been uploaded successfully. When the IR obstruction module on both sides detects an obstacle, it will move in the opposite direction for emergency evasion; if there is an obstacle within 2~10cm directly in front of the car, it will back up to the left, adjust its direction, and then move forward.

const int in1 = 5;
const int in2 = 6;
const int in3 = 9;
const int in4 = 10;

const int echoPin = 4;
const int trigPin = 3;

const int rightIR = 7;
const int leftIR = 8;

float readSensorData() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  float distance = pulseIn(echoPin, HIGH) / 58.00; //Equivalent to (340m/s*1us)/2
  return distance;
}


void moveForward(int speed) {
  analogWrite(in1, 0);
  analogWrite(in2, speed);
  analogWrite(in3, speed);
  analogWrite(in4, 0);
}

void moveBackward(int speed) {
  analogWrite(in1, speed);
  analogWrite(in2, 0);
  analogWrite(in3, 0);
  analogWrite(in4, speed);
}


void backLeft(int speed) {
  analogWrite(in1, speed);
  analogWrite(in2, 0);
  analogWrite(in3, 0);
  analogWrite(in4, 0);
}

void backRight(int speed) {
  analogWrite(in1, 0);
  analogWrite(in2, 0);
  analogWrite(in3, 0);
  analogWrite(in4, speed);
}

void stopMove() {
  analogWrite(in1, 0);
  analogWrite(in2, 0);
  analogWrite(in3, 0);
  analogWrite(in4, 0);
}

void setup() {
  Serial.begin(9600);

  //motor
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  //ultrasonic
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);

  //IR obstacle
  pinMode(leftIR, INPUT);
  pinMode(rightIR, INPUT);
}

void loop() {

  int left = digitalRead(leftIR);  // 0: Obstructed   1: Empty
  int right = digitalRead(rightIR);

  if (!left && right) {
    backLeft(150);
  } else if (left && !right) {
    backRight(150);
  } else if (!left && !right) {
    moveBackward(150);
  } else {
    float distance = readSensorData();
    Serial.println(distance);
    if (distance > 50) { // Safe
      moveForward(200);
    } else if (distance < 10 && distance > 2) { // Attention
      moveBackward(200);
      delay(1000);
      backLeft(150);
      delay(500);
    } else {
      moveForward(150);
    }
  }
}