Ultrasonic sensor HC-SR04 arduino project with code

Park the car precisely in the garage
Park the car precisely in the garage

Oftentimes, we scratch the car against the wall or cause damage to it, while park it in the garage, due to the lack of clarity of the remaining rear distance between the car and the wall.
In this post, we will create a simple electronic project using Arduino and Ultrasonic, that will measure the distance between the car and the wall, which will help: To Park the car precisely in the garage, without causing damage to the car, Ultrasonic sensor HC-SR04 arduino project with code

Electronic components needed :

  • Ultrasonic module hc-sr04
  • 7 LEDs
  • 7 Resistances ( 220 Ohm )
  • Electrical wires
  • Arduino UNO
  • Breadboard

Ultrasonic sensor HC-SR04 arduino project with code

circuit diagram

Park the car precisely in the garage
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 :

Discover also :

Control Any Device by Remote Control

Clap Switch with Arduino and Sound Sensor

How to make fire detector using arduino

How to control LED lights with Arduino

LEAVE A REPLY

Please enter your comment!
Please enter your name here