├── LICENSE ├── README.md └── maincode.ino /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Abhineet Raj 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino Self-Driving Car with Ultrasonic Sensors 2 | 3 | This project involves building an Arduino-based auto-driving car that utilizes ultrasonic sensors to detect obstacles and make decisions on movement. The car can be started using a push button, and it is equipped with four motors for movement control. A servo motor is used to steer the front wheels. The car is programmed to respond to sensor readings and perform actions accordingly: turning left or right, moving forward, or stopping. In specific scenarios, such as detecting obstacles from multiple directions simultaneously, the car will emit a continuous beeping sound. 4 | 5 | ## Working Mechanism 6 | 7 | 1. The car's operation is initiated by pressing a push button, which sets the "isStarted" flag to true. 8 | 9 | 2. The Arduino continuously reads the distance measurements from the three ultrasonic sensors: front, left, and right. 10 | 11 | 3. Based on the sensor readings, the car's behavior is determined using conditional statements. 12 | 13 | 4. If all sensors indicate a distance below the threshold, the car stops and emits a continuous beeping sound. 14 | 15 | 5. If the front sensor detects an obstacle, the car's response depends on the readings of the left and right sensors. 16 | - If the left sensor detects an obstacle while the right sensor does not, the car turns left. 17 | - If the right sensor detects an obstacle while the left sensor does not, the car turns right. 18 | - If both the left and right sensors detect obstacles, the car stops and emits a continuous beeping sound. 19 | 20 | 6. If neither the left nor right sensors detect obstacles while the front sensor does, the car moves forward. 21 | 22 | 7. The car's movement is controlled by activating the appropriate motor pins, while the servo motor steers the front wheels. 23 | 24 | ## Installation 25 | 26 | To recreate this project, follow these steps: 27 | 28 | 1. Gather the required components: 29 | - Arduino board (e.g., Arduino Uno) 30 | - Ultrasonic sensors (3x) 31 | - Servo motor 32 | - Push button 33 | - Four motors 34 | - Buzzer 35 | - Jumper wires 36 | - Breadboard or PCB 37 | - Power source (e.g., battery pack) 38 | 39 | 2. Set up the hardware: 40 | - Connect the ultrasonic sensors to the appropriate trigger and echo pins on the Arduino. 41 | - Connect the servo motor to the designated pin for steering. 42 | - Connect the motors for movement control to the corresponding motor pins. 43 | - Connect the push button to a digital input pin. 44 | - Connect the buzzer to a digital output pin. 45 | 46 | 3. Install the required libraries: 47 | - Install the "Servo" library for servo motor control. 48 | - Install the "NewPing" library for ultrasonic sensor measurements. 49 | 50 | 4. Open the Arduino IDE and create a new sketch. 51 | 52 | 5. Copy and paste the provided code (from maincode.ino) into the Arduino IDE. 53 | 54 | 6. Verify and upload the code to the Arduino board. 55 | 56 | 7. Connect the power source to the Arduino and the motor driver. 57 | 58 | 8. Press the push button to start the car, and observe its behavior based on the sensor readings. 59 | 60 | Note: Adjust the sensor thresholds, pin assignments, and other parameters according to your specific hardware configuration. 61 | 62 | 9. Enjoy your Arduino auto-driving car with ultrasonic sensors! 63 | 64 | Remember to ensure proper safety precautions while working with the hardware and during testing. 65 | 66 | ## Circuit 67 | 68 | | Arduino pins | Electronic component | 69 | |----------------|:--------------------:| 70 | | D2 | Push Button | 71 | | D4 | Ultrasonic Sensor (Front Trigger) | 72 | | D5 | Ultrasonic Sensor (Left Trigger) | 73 | | D6 | Ultrasonic Sensor (Left Echo) | 74 | | D7 | Ultrasonic Sensor (Right Trigger) | 75 | | D8 | Ultrasonic Sensor (Right Echo) | 76 | | D9 | Motor 1 | 77 | | D10 | Motor 2 | 78 | | D11 | Motor 3 | 79 | | D12 | Motor 4 | 80 | | D13 | Servo Motor | 81 | | A0 | Buzzer | 82 | 83 | 84 | ## Programming languages used 85 | blender 86 | 87 | 88 | ## Developer 89 | * [abhineetraj1](http://github.com/abhineetraj1) 90 | -------------------------------------------------------------------------------- /maincode.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Pin Definitions 5 | #define BUTTON_PIN 2 6 | #define FRONT_SENSOR_TRIGGER_PIN 3 7 | #define FRONT_SENSOR_ECHO_PIN 4 8 | #define LEFT_SENSOR_TRIGGER_PIN 5 9 | #define LEFT_SENSOR_ECHO_PIN 6 10 | #define RIGHT_SENSOR_TRIGGER_PIN 7 11 | #define RIGHT_SENSOR_ECHO_PIN 8 12 | #define MOTOR1_PIN 9 13 | #define MOTOR2_PIN 10 14 | #define MOTOR3_PIN 11 15 | #define MOTOR4_PIN 12 16 | #define SERVO_PIN 13 17 | #define BUZZER_PIN A0 18 | 19 | // Constants 20 | #define SENSOR_THRESHOLD 20 // Distance in centimeters 21 | #define TURN_DURATION 1000 22 | #define BEEP_DURATION 200 23 | 24 | // Variables 25 | Servo servo; 26 | bool isStarted = false; 27 | NewPing frontSensor(FRONT_SENSOR_TRIGGER_PIN, FRONT_SENSOR_ECHO_PIN); 28 | NewPing leftSensor(LEFT_SENSOR_TRIGGER_PIN, LEFT_SENSOR_ECHO_PIN); 29 | NewPing rightSensor(RIGHT_SENSOR_TRIGGER_PIN, RIGHT_SENSOR_ECHO_PIN); 30 | 31 | void setup() { 32 | // Set the pin modes 33 | pinMode(BUTTON_PIN, INPUT_PULLUP); 34 | pinMode(MOTOR1_PIN, OUTPUT); 35 | pinMode(MOTOR2_PIN, OUTPUT); 36 | pinMode(MOTOR3_PIN, OUTPUT); 37 | pinMode(MOTOR4_PIN, OUTPUT); 38 | pinMode(BUZZER_PIN, OUTPUT); 39 | 40 | // Attach the servo to the appropriate pin 41 | servo.attach(SERVO_PIN); 42 | } 43 | 44 | void loop() { 45 | // Check if the button is pressed to start the car 46 | if (digitalRead(BUTTON_PIN) == LOW) { 47 | isStarted = true; 48 | delay(500); // Button debounce delay 49 | } 50 | 51 | // Check if the car is started 52 | if (isStarted) { 53 | // Read sensor values 54 | int frontDistance = frontSensor.ping_cm(); 55 | int leftDistance = leftSensor.ping_cm(); 56 | int rightDistance = rightSensor.ping_cm(); 57 | 58 | // Check the sensor conditions 59 | if (frontDistance < SENSOR_THRESHOLD && leftDistance < SENSOR_THRESHOLD && rightDistance < SENSOR_THRESHOLD) { 60 | // Stop the car and beep continuously 61 | stopCar(); 62 | beepContinuously(); 63 | } else if (frontDistance < SENSOR_THRESHOLD) { 64 | if (leftDistance < SENSOR_THRESHOLD && rightDistance >= SENSOR_THRESHOLD) { 65 | // Turn left 66 | turnLeft(); 67 | } else if (leftDistance >= SENSOR_THRESHOLD && rightDistance < SENSOR_THRESHOLD) { 68 | // Turn right 69 | turnRight(); 70 | } else if (leftDistance >= SENSOR_THRESHOLD && rightDistance >= SENSOR_THRESHOLD) { 71 | // Stop the car and beep continuously 72 | stopCar(); 73 | beepContinuously(); 74 | } 75 | } else if (leftDistance < SENSOR_THRESHOLD && rightDistance < SENSOR_THRESHOLD) { 76 | // Move forward 77 | moveForward(); 78 | } else { 79 | // Stop the car and beep continuously 80 | stopCar(); 81 | beepContinuously(); 82 | } 83 | } else { 84 | // Stop the car if not started 85 | stopCar(); 86 | } 87 | } 88 | 89 | void moveForward() { 90 | digitalWrite(MOTOR1_PIN, HIGH); 91 | digitalWrite(MOTOR2_PIN, LOW); 92 | digitalWrite(MOTOR3_PIN, HIGH); 93 | digitalWrite(MOTOR4_PIN, LOW); 94 | } 95 | 96 | void turnLeft() { 97 | digitalWrite(MOTOR1_PIN, LOW); 98 | digitalWrite(MOTOR2_PIN, LOW); 99 | digitalWrite(MOTOR3_PIN, HIGH); 100 | digitalWrite(MOTOR4_PIN, LOW); 101 | servo.write(90); // Turn the servo to the left 102 | delay(TURN_DURATION); 103 | } 104 | 105 | void turnRight() { 106 | digitalWrite(MOTOR1_PIN, HIGH); 107 | digitalWrite(MOTOR2_PIN, LOW); 108 | digitalWrite(MOTOR3_PIN, LOW); 109 | digitalWrite(MOTOR4_PIN, LOW); 110 | servo.write(0); // Turn the servo to the right 111 | delay(TURN_DURATION); 112 | } 113 | 114 | void stopCar() { 115 | digitalWrite(MOTOR1_PIN, LOW); 116 | digitalWrite(MOTOR2_PIN, LOW); 117 | digitalWrite(MOTOR3_PIN, LOW); 118 | digitalWrite(MOTOR4_PIN, LOW); 119 | servo.write(45); // Reset the servo to the center position 120 | } 121 | 122 | void beepContinuously() { 123 | while (true) { 124 | digitalWrite(BUZZER_PIN, HIGH); 125 | delay(BEEP_DURATION); 126 | digitalWrite(BUZZER_PIN, LOW); 127 | delay(BEEP_DURATION); 128 | } 129 | } 130 | --------------------------------------------------------------------------------