roboattic Lab
Published © GPL3+

DIY Self-Driving Car for Beginners (Arduino & LiDAR)

This Obstacle Avoiding Car project leverages advanced sensing and mobility to create an autonomous robot capable of navigating complex areas

IntermediateFull instructions provided3 hours1,455
DIY Self-Driving Car for Beginners (Arduino & LiDAR)

Things used in this project

Story

Read more

Schematics

Circuit Diagram

Code

ARDUINO_OBSTACLE_AVOIDING_CAR.ino

C/C++
// LiDAR based Autonomous car || Obstacle Avoiding
// Created By roboattic Lab
// Contact me here https://www.instagram.com/roboattic_lab/

#include <AFMotor.h>
#include <Adafruit_VL53L0X.h>
#include <Wire.h>

#define MAX_DISTANCE 200
#define MAX_SPEED 190
#define MAX_SPEED_OFFSET 20

AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

boolean goesForward = false;
int distance = 100;
int speedSet = 0;

void setup() {

  Serial.begin(115200);
  delay(10);
  Serial.println("VL53L0X test");

  // Initialize I2C
  Wire.begin();

  // Add this to setup() before lox.begin() to scan for devices
  Serial.println("Scanning I2C bus...");
  for (byte i = 8; i < 120; i++) {
    Wire.beginTransmission(i);
    if (Wire.endTransmission() == 0) {
      Serial.print("Found device at address: 0x");
      Serial.println(i, HEX);
    }
  }

  // Try to initialize the VL53L0X (only call begin() once)
  if (!lox.begin()) {
    Serial.println("Failed to find VL53L0X sensor. Check wiring!");
    while (1)
      ;  // Stop execution if sensor not found
  }
  Serial.println("VL53L0X initialized successfully.");
}

void loop() {

  VL53L0X_RangingMeasurementData_t measure;

  lox.rangingTest(&measure, false);

  if (measure.RangeStatus == 4) {
    Serial.println("Out of range");
  } else {
    distance = measure.RangeMilliMeter / 10;
  }

  // Print the distance to the serial monitor (for debugging)
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");


  int distanceR = 0;
  int distanceL = 0;
  delay(40);

  if (distance <= 15) {
    moveStop();
    delay(100);
    moveBackward();
    delay(300);
    moveStop();
    delay(200);
    distanceR = lookRight();
    delay(200);
    distanceL = lookLeft();
    delay(200);

    if (distanceR >= distanceL) {
      turnRight();
      moveStop();
    } else {
      turnLeft();
      moveStop();
    }
  } else {
    moveForward();
  }
}

int lookRight() {
  rotateRight();
  delay(500);
  VL53L0X_RangingMeasurementData_t measure;

  lox.rangingTest(&measure, false);

  if (measure.RangeStatus == 4) {
    Serial.println("Out of range");
  } else {
    distance = measure.RangeMilliMeter / 10;
  }
  delay(500);
  rotateLeft();
  return distance;
  delay(100);
}

int lookLeft() {
  rotateLeft();
  delay(500);
  VL53L0X_RangingMeasurementData_t measure;

  lox.rangingTest(&measure, false);

  if (measure.RangeStatus == 4) {
    Serial.println("Out of range");
  } else {
    distance = measure.RangeMilliMeter / 10;
  }
  delay(500);
  rotateRight();
  return distance;
  delay(100);
}


void moveStop() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

void moveForward() {

  if (!goesForward) {
    goesForward = true;
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
    for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2)  // slowly bring the speed up to avoid loading down the batteries too quickly
    {
      motor1.setSpeed(speedSet);
      motor2.setSpeed(speedSet);
      motor3.setSpeed(speedSet);
      motor4.setSpeed(speedSet);
      delay(5);
    }
  }
}

void moveBackward() {
  goesForward = false;
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2)  // slowly bring the speed up to avoid loading down the batteries too quickly
  {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet);
    delay(5);
  }
}

void turnRight() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
  delay(500);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

void turnLeft() {
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
  delay(500);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}


void rotateLeft() {
  motor2.run(FORWARD);
  motor1.run(FORWARD);
  motor4.run(BACKWARD);
  motor3.run(BACKWARD);
}
void rotateRight() {
  motor2.run(BACKWARD);
  motor1.run(BACKWARD);
  motor4.run(FORWARD);
  motor3.run(FORWARD);
}

Credits

roboattic Lab
16 projects • 9 followers
YouTube Content Creator Robotics Enthusiast

Comments