Open In App

Smart Collision Detector using Arduino

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards can read digital & analog inputs from the sensors and the HR SC-04 Ultrasonic sensor uses sonar to find the distance between objects. It can measure distances ranging from 2cm to 400cm with an accuracy of 0.3cm. Collision detector is a valuable system and finds great application in the automobile industry. It is used to avoid accidents and in self-driving cars. 

In this article, We will learn how we can make a Smart Collision Detector using Arduino. When the distance between two objects is less than 20cm Red LED will glow and the buzzer will start. A step-by-step explanation of the project is given with the program to execute this. (Design is also included.)
 

Components Required

  • Arduino UNO R3 -> A microcontroller board is based on the ATmega328P
  • HR SC-04 Ultrasonic sensor -> Which detects the distance between objects using SONAR
  • Piezo Buzzer -> Device that produces sound or alarm
  • LED -> Light-emitting diode
  • 10kΩ Resistor ->To resist current
  • Jumper Wires -> For connecting the elements of the circuit

Schematic diagram

Schematic Diagram

Schematic Diagram

How does this work?

In this circuit, the HR SC-04 Ultrasonic sensor detects the time the wave takes to hit and reach the sensor again i.e from the trig pin to the echo pin and sends the value to the Arduino, which converts the time into distance using the speed formula. This value is compared to a safe distance; if the distance is between 50cm and 20cm, the blue light will be illuminated. Otherwise, if the distance is below 20cm, the red light will be illuminated and the buzzer starts to ring.

Pins Connection

  • Arduino Digital pin 13 is connected with the (+ve) pin of the Buzzer
  • Arduino Digital pin 12 is connected with the (+ve) pin of LED1(Blue)
  • Arduino Digital pin 8 is connected with the (+ve) pin of LED2(Red)
  • Arduino Digital pin 3 is connected with the TRIG Pin of the HR SC-04 Ultrasonic sensor
  • Arduino Digital pin 2 is connected with the ECHO Pin of the HR SC-04 Ultrasonic sensor
  • Arduino 5v Power pin is connected with the VCC pin of the HR SC-04 Ultrasonic sensor
  • Arduino GND Power pin is connected with the GND pin of the HR SC-04 Ultrasonic sensor
  • Arduino GND Power pin is connected with (-ve) pins of LEDs with resistance
  • Arduino GND Power pin is connected with (-ve) pins of a Buzzer

Formula Explained:

distance = time * speed

Speed of sound = 343 m/s = 0.034 cm/s

Divide by 2, since the time is recorded for both to and fro i.e, before & after hitting the object

Program

C++




// Initializing Pins
// Sensor Pins - Trigger to 3 and Echo to 2
const int trigPin = 3;
const int echoPin = 2;
 
// Buzzer to 13 pin
int buzz = 13;
 
// Defining global variables
long duration;
int distance;
 
// power the board
void setup()
{
    // initialize pins as output for Sensor
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
 
    // initialize pins as output for LEDs
    pinMode(12, OUTPUT);
    pinMode(8, OUTPUT);
    Serial.begin(9600);
}
 
// the loop function runs over and over again forever
void loop()
{
    // Clears the trigPin
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
 
    // Set HIGH trigPin
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
 
    // Reads the echoPin in microseconds
    duration = pulseIn(echoPin, HIGH);
 
    // Calculating the distance
    distance = duration * 0.034 / 2;
 
    if (distance <= 50 && distance >= 20) {
        digitalWrite(12, HIGH);
        // LED on
    }
    else {
        digitalWrite(12, LOW);
        // LED turn off
    }
    if (distance <= 20) {
        digitalWrite(8, HIGH);
        tone(buzz, 2000);
        delay(100);
        noTone(buzz);
        delay(100);
        tone(buzz, 2000);
        delay(100);
        noTone(buzz);
        delay(100);
        tone(buzz, 2000);
        delay(100);
        noTone(buzz);
        tone(buzz, 2000);
        delay(100);
        noTone(buzz);
        delay(100);
    }
    else {
        digitalWrite(8, LOW);
        // LED turn off
        // wait for a second
    }
}


Simulation

We have used Tinkercad to create this simulation – You can create an account(Google, Autodesk, email) and use Tinkercad for free. 

You can access the project file using this link.

 

You can also run the simulation using the project. After opening the project hit Simulate button and click Start Simulation to execute this project.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads