Introduction: Object Detection With Ultrasonic Sensor (HC-SR04)

About: A diy learner and I love computers..

Since for our smart street light intensity control system we will be needing a sensor to detect object(vehicles) in night when ldr detection fails from our previous instructable.

So here is ultrasonic ping sensor for object detection and distance.

Step 1: Components Required and Connection

1. Ultrasonic sensor(hc-sr04)

2. led

3.resistor(your wish with led)

4. arduino

5. jumper wires

The connection is pretty simple ultrasonic sensors have 4 pins namely: trig,echo,vcc,gnd to be connected with arduino .

The echo pin is connected to the arduino digital pin 5 and the trigg pin of sensor is connected to digital pin 13 of the arduino . Vcc is connected to 5V from the arduino and gnd pin of sensor is connected to the gnd pin of the arduino.

The led is connected to pin 2 (+ of the led) while the cathode(-) of the led will be grounded via a low resistor value.

Step 2: Code and Understanding the Working of Sensor

The code is pretty simple which detects the presence of object in front of the sensor (eyes side) and glows an led until the object is taken away from the sensor (here approx 50 cm) .

The sensor works on the principle of 'echolocation' , yeah the same technique that allows the bat and whales to see. The wave(pulse) is generated from the sensor trig pin using the arduino and the pulses return to the sensor after striking an object to the echo pin which transfers the digital(0,1) if the object is detected and approx distance of object from the sensor can be noted via Serial port terminal.

The trig wave is generated from the sensor of 1ms duration in our program i.e. the width of pulse is 1ms which is accquired using the function pulseIn() and saved in the variable duration . The pulse travels at the speed of sound about 340m/s and the duration is double the time taken by the wave to reach the object (since it bounces back and then noted via echo pin).So distance will be duration * speed .

From the datasheet of src-04 the distance is half the duration divided by 29.1 to perform above steps as given in the code also which is taken from codebender https://codebender.cc/sketch:242411#Ultrasonic%20w...

Here is a link to understand the working of src-04 sensor http://howtomechatronics.com/tutorials/arduino/ult...

Step 3: Demo

The object is detected in the video as depicted by the glowing of led and the distance shown at the serial monitor .