Ultrasonic sensor HC-SR04 arduino project with code

Many times, when parking our cars in the garage, we accidentally scratch them against the walls or cause damage due to a lack of visibility regarding the remaining distance between the car and the wall. To address this issue, we can create a straightforward electronic project using Arduino and an ultrasonic sensor. This project aims to measure the distance between the car and the wall, providing assistance in parking the car precisely in the garage without causing any damage. The project involves utilizing the HC-SR04 ultrasonic sensor with Arduino and includes the necessary code for implementation.

Electronic components needed :

The components required for this project include:

– HC-SR04 ultrasonic module
– 7 LEDs
– 7 resistors (220 Ohm)
– Electrical wires
– Arduino UNO
– Breadboard

These components are necessary to build and implement the project successfully.

circuit diagram

Arduino programming code :

// ultrasonic sensor Pins
const int trig = 10;
const int echo = 11;

// led Pins variables
const int LED1 = 2;
const int LED2 = 3;
const int LED3 = 4;
const int LED4 = 5;
const int LED5 = 6;
const int LED6 = 7;
const int LED7 = 8;

int duration = 0;
int distance = 0;

// By www.andprof.com
void setup()
{
  // ultrasonic sensor
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
 // led
  pinMode(LED1 , OUTPUT);
  pinMode(LED2 , OUTPUT);
  pinMode(LED3 , OUTPUT);
  pinMode(LED4 , OUTPUT);
  pinMode(LED5 , OUTPUT);
  pinMode(LED6 , OUTPUT);
  pinMode(LED7 , OUTPUT);
 
  Serial.begin(9600);
}

void loop()
{
  digitalWrite(trig , HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig , LOW);

  duration = pulseIn(echo , HIGH);
  distance = (duration/2) / 28.5 ;
  Serial.println(distance);

  if ( distance <= 5 )
  {
    digitalWrite(LED1, HIGH);
  }
  else
  {
    digitalWrite(LED1, LOW);
  }
  if ( distance <= 7 )
  {
    digitalWrite(LED2, HIGH);
  }
  else
  {
    digitalWrite(LED2, LOW);
  }
  if ( distance <= 10 )
  {
    digitalWrite(LED3, HIGH);
  }
  else
  {
    digitalWrite(LED3, LOW);
  }
  if ( distance <= 15 )
  {
    digitalWrite(LED4, HIGH);
  }
  else
  {
    digitalWrite(LED4, LOW);
  }
  if ( distance <= 17 )
  {
    digitalWrite(LED5, HIGH);
  }
  else
  {
    digitalWrite(LED5, LOW);
  }
  if ( distance <= 20 )
  {
    digitalWrite(LED6, HIGH);
  }
  else
  {
    digitalWrite(LED6, LOW);
  }
  if ( distance <= 25 )
  {
    digitalWrite(LED7, HIGH);
  }
  else
  {
    digitalWrite(LED7, LOW);
  }
}

Explainer video :


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top