Making The Car
Components
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.
It is advised to check the power, voltage and other specifications of items before purchase.
Sl. No. | Item | Qty | Use | Type | Link |
---|---|---|---|---|---|
1 | Arduino UNO R4 WiFi | 1 | Main microcontroller | Functional | Link |
2 | L298N Motor Driver | 1 | Controls DC motors | Functional | Link |
3 | DC Motor with wheels | 4 | Drives the car | Functional | Link |
4 | Chassis | 1 | Car body/frame | Structural | Link |
5 | 3.7V 18650 Li-Ion Battery (2000+ mAh) | 4 | Powers the car | Power | Link |
6 | 4 Cell Battery (18650) Holder | 1 | Holds batteries | Structural | Link |
7 | Ultrasound Distance Sensor (HC-SR04) | 1 | Measures distance to obstacles | Sensor | Link |
8 | Ultrasound Distance Sensor Holder | 1 | Mounts distance sensor | Structural | Link |
9 | Servo Motor | 1 | Controls sensor direction | Functional | Link |
10 | Jumper Wires & Connectors | 20+ | Electrical connections | Wiring | Link |
11 | Mini Breadboard (optional) | 1 | Prototyping connections | Prototyping | Link |
12 | Cellotape and cardboard | Sufficient | Holding the car together or as adhesives | Structural | - |
Connections
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
Explore and edit the interactive diagram from below or click here.
Image 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.
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));
}