Skip to main content

Making The Car

Components

note

The items mentioned below are those which were used in our project. Anyone can use any alternatives or similar components to build it as they find suitable.

tip

It is advised to check the power, voltage and other specifications of items before purchase.

Sl. No.ItemQtyUseTypeLink
1Arduino UNO R4 WiFi1Main microcontrollerFunctionalLink
2L298N Motor Driver1Controls DC motorsFunctionalLink
3DC Motor with wheels4Drives the carFunctionalLink
4Chassis1Car body/frameStructuralLink
53.7V 18650 Li-Ion Battery (2000+ mAh)4Powers the carPowerLink
64 Cell Battery (18650) Holder1Holds batteriesStructuralLink
7Ultrasound Distance Sensor (HC-SR04)1Measures distance to obstaclesSensorLink
8Ultrasound Distance Sensor Holder1Mounts distance sensorStructuralLink
9Servo Motor1Controls sensor directionFunctionalLink
10Jumper Wires & Connectors20+Electrical connectionsWiringLink
11Mini Breadboard (optional)1Prototyping connectionsPrototypingLink
12Cellotape and cardboardSufficientHolding the car together or as adhesivesStructural-

Connections

note

Some components maybe different or not shown in the connection diagrams due to lack of components or unavailibilty in the software libraries. However, the connections remain the same and we have tried our best to show and describe all possible connections.

Interactive Diagram

tip

Explore and edit the interactive diagram from below or click here.

Image Diagram

Connection Diagram
Figure: Connection Diagram. See the interactive diagram from above to learn more.

Code / Script

The code needs to be uploaded to the Arduino Board through the Arduino IDE or other methods applicable.

tip

Check the code below and understand it yourself to make necessary changes and tune your car!

#include <Servo.h>
#include <NewPing.h>

#define SERVO_PIN 3
#define ULTRASONIC_SENSOR_TRIG 11
#define ULTRASONIC_SENSOR_ECHO 12
#define MAX_REGULAR_MOTOR_SPEED 60
#define MAX_MOTOR_ADJUST_SPEED 250
#define DISTANCE_TO_CHECK 30

// Right motor
int enableRightMotor = 5;
int rightMotorPin1 = 7;
int rightMotorPin2 = 8;

// Left motor
int enableLeftMotor = 6;
int leftMotorPin1 = 9;
int leftMotorPin2 = 10;

NewPing mySensor(ULTRASONIC_SENSOR_TRIG, ULTRASONIC_SENSOR_ECHO, 400);
Servo myServo;

void setup() {
// Motor pin setup
pinMode(enableRightMotor, OUTPUT);
pinMode(rightMotorPin1, OUTPUT);
pinMode(rightMotorPin2, OUTPUT);

pinMode(enableLeftMotor, OUTPUT);
pinMode(leftMotorPin1, OUTPUT);
pinMode(leftMotorPin2, OUTPUT);

// Servo setup
myServo.attach(SERVO_PIN);
myServo.write(90); // Center the servo initially
rotateMotor(0, 0); // Stop motors initially
}

void loop() {
int distance = mySensor.ping_cm();

// If an object is detected within the threshold distance
if (distance > 0 && distance < DISTANCE_TO_CHECK) {
rotateMotor(0, 0); // Stop motors
delay(500);

rotateMotor(-MAX_MOTOR_ADJUST_SPEED, -MAX_MOTOR_ADJUST_SPEED); // Reverse
delay(200);

rotateMotor(0, 0); // Stop motors
delay(500);

// Rotate the servo to the left and check the left side distance
myServo.write(180);
delay(500);
int distanceLeft = mySensor.ping_cm();

// Rotate the servo to the right and check the right side distance
myServo.write(0);
delay(500);
int distanceRight = mySensor.ping_cm();

// Return the servo to the center position
myServo.write(90);
delay(500);

// Handle invalid distance readings (0 means no reading)
if (distanceLeft == 0) distanceLeft = 999; // Large value if no valid reading
if (distanceRight == 0) distanceRight = 999;

// Decide on which way to turn based on the side distances
if (distanceLeft > distanceRight) {
rotateMotor(MAX_MOTOR_ADJUST_SPEED, -MAX_MOTOR_ADJUST_SPEED); // Turn left (more space on the left)
delay(200);
} else {
rotateMotor(-MAX_MOTOR_ADJUST_SPEED, MAX_MOTOR_ADJUST_SPEED); // Turn right (more space on the right)
delay(200);
}

rotateMotor(0, 0); // Stop motors
delay(200);
} else {
// If no obstacle in front, move forward
rotateMotor(MAX_REGULAR_MOTOR_SPEED, MAX_REGULAR_MOTOR_SPEED);
}
}

// Function to control motor rotation based on speed
void rotateMotor(int rightMotorSpeed, int leftMotorSpeed) {
// Right motor direction
if (rightMotorSpeed < 0) {
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, HIGH);
} else {
digitalWrite(rightMotorPin1, HIGH);
digitalWrite(rightMotorPin2, LOW);
}

// Left motor direction
if (leftMotorSpeed < 0) {
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, HIGH);
} else {
digitalWrite(leftMotorPin1, HIGH);
digitalWrite(leftMotorPin2, LOW);
}

// Set motor speeds
analogWrite(enableRightMotor, abs(rightMotorSpeed));
analogWrite(enableLeftMotor, abs(leftMotorSpeed));
}