ZDIRY-TUFWT-EBONM-EYJ00-IDBLANTER.COM
ZDIRY-TUFWT-EBONM-EYJ00
BLANTERWISDOM105

HC-SR04 Parking using Arduino

Thursday, May 10, 2018
HC-SR04 (image form www.sparkfun.com)

Ultrasonic sensor is a sensor that works using high frequency sound waves. this sensor can be used to determine the distance of an object. Example to determine the distance of your car park, measure the height of people, knowing the presence of people on your doorstep, and others. The working principle is that these sensors generally have two funnels, the first funnel used for transmitter sound waves and a second funnel in use to receive the reflection of sound waves. The distance between the sensor and the object will be calculated using the formula:

Distance = speed of sound * T / 2

Information:
Speed of sound is 343 m/s.
T is how long travel time when ultrasonic wave is transmitted and get back recived.
"2" is time required for transmitte to object and from object to receive.

Now we will choose HC-SR04 sensor.  This is sensor have 4-pin, Vcc, Gnd, Trig and Echo. Trig or Trigger pin is used to transmitte and Echo used to receive. The principle this pin:
1. Trig pin will be High for 10 m/s then low.

  digitalWrite(pin_trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(pin_trig, LOW);

2. This sensor will be transmitte square wave 40KHz.
3. The reflection of the square wave will be received and will count how long the time this wave recived. In our program we used:

   double times = pulseIn(pin_echo, HIGH);

4. After time is obtained, we can calculate distance of object using this  formula "Distance = speed of sound * T / 2".
How to set these formula will measure in "cm"? Because speed of sound is 343 m/s or 34300 cm/s,  T using m/s (1 m/s = 10-6), then Distance = 34300 * (T / 10-6) / 2 = 0.0343 * (T / 2).

   double range = 0.0343 * ( times / 2);


Connect your arduino to HC-SR04:
Arduino               HC-SR04
   5V                           Vcc
   Gnd                         Gnd
   13                            Trig
   12                            Echo
   11                            LED for 10 cm
   10                            LED for 25 cm
     9                            LED for 40 cm
     8                            Buzzer


Program:
// ---------------------------------------------
//   Distance Sensor HC-SR04
// ---------------------------------------------
#define pin_trig 13
#define pin_echo 12
#define pin_10cm 11
#define pin_25cm 10
#define pin_40cm 9
#define pin_buz  8

void setup()
{
  Serial.begin (9600)
  pinMode(pin_trig, OUTPUT);
  pinMode(pin_echo, INPUT);
  pinMode(pin_10cm, OUTPUT);
  pinMode(pin_25cm, OUTPUT);
  pinMode(pin_40cm, OUTPUT);
  pinMode(pin_buz, OUTPUT);
}

void loop()
{
  //transmite
  digitalWrite(pin_trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(pin_trig, LOW);

  // receive
  double times = pulseIn(pin_echo, HIGH);

  // calculate distance
  double range = 0.0343 * ( times / 2);

  //if distance is less equal to 10cm
  if (range <= 10 )
  {
    digitalWrite(pin_10cm, HIGH);
    digitalWrite(pin_buz, HIGH);
    Serial.print(range);
    delay(50);
    digitalWrite(pin_10cm, LOW);
    digitalWrite(pin_buz, LOW);
    delay(50);
  }

  //if distance is less equal to 10.5cm - 25cm
  if ((range >=10.5) && (range <= 25))
  {
    digitalWrite(pin_25cm, HIGH);
    digitalWrite(pin_buz, HIGH);
    Serial.println(range);
    delay(100);
    digitalWrite(pin_25cm, LOW);
    digitalWrite(pin_buz, LOW);
    delay(100);
  }

  //if distance is less equal to 25.5cm - 40cm
  if ((range >=25.5) && (range <= 40))
  {
    digitalWrite(pin_40cm, HIGH);
    digitalWrite(pin_buz, HIGH);
    Serial.println(range);
    delay(200);
    digitalWrite(pin_40cm, LOW);
    digitalWrite(pin_buz, LOW);
    delay(200);
  }

  else
  {
    digitalWrite(pin_10cm, LOW);
    digitalWrite(pin_25cm, LOW);
    digitalWrite(pin_40cm, LOW);
    digitalWrite(pin_buz, LOW);
    Serial.print(range);
    Serial.println(" cm");
  }
  delay(100); // delay 100ms
}

Video:
Share This :
Prof. Apis

Hello Greetings all. I created this website to document the knowledge that I have learned. Also to help all friends. Suggestions and criticisms are very welcome to make this website better. Thank you

0 comments