Skip to main content

Ultrasonic Sensor with Arduino

The HC-SR04 ultrasonic sensor uses SONAR to determine the distance of an object just like the bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package from 2 cm to 400 cm or 1” to 13 feet.

The operation is not affected by sunlight or black material, although acoustically, soft materials like cloth can be difficult to detect. It comes complete with ultrasonic transmitter and receiver module.

Technical Specifications


  • Power Supply − +5V DC
  • Quiescent Current − <2mA
  • Working Current − 15mA
  • Effectual Angle − <15°
  • Ranging Distance − 2cm – 400 cm/1″ – 13ft
  • Resolution − 0.3 cm
  • Measuring Angle − 30 degree
The ultrasonic sensor HC-SR04 includes four pins:
  • VCC pin needs to be connected to VCC (5V)
  • GND pin needs to be connected to GND (0V)
  • TRIG pin this pin receives the control signal (pulse) from Arduino.
  • ECHO pin this pin sends a signal (pulse) to Arduino. Arduino measures the duration of pulse to calculate distance. 
  • How It Works
    This section is the in-depth knowledge. DON'T worry if you don't understand. Ignore this section if it overloads you, and come back in another day. Keep reading the next sections.
  1. 1.  Micro-controller: generates a 10-microsecond pulse on the TRIG pin.
  2. 2. The ultrasonic sensor automatically emits the ultrasonic waves.
  3. 3. The ultrasonic wave is reflected after hitting an obstacle.
  4. 4. The ultrasonic sensor:
Detects the reflected ultrasonic wave.
    • Measures the travel time of the ultrasonic wave.
  1. 5. Ultrasonic sensor: generates a pulse to the ECHO pin. The duration of the pulse is equal to the travel time of the ultrasonic wave.
  2. 6. Micro-controller measures the pulse duration in the ECHO pin, and then calculate the distance between sensor and obstacle.
  3. Components Required

    You will need the following components −
    • 1 × Breadboard
    • 1 × Arduino Uno R3
    • 1 × ULTRASONIC Sensor (HC-SR04)






Procedure


Follow the circuit diagram and make the connections as shown in the image given below.

Sketch

Open the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit. Open a new sketch File by clicking New.
Arduino Code
/*******
 All the resources for this project:
 https://lihuo.blogspot.com
*******/
const int pingPin = 11; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 12; // Echo Pin of Ultrasonic Sensor


long microsecondsToInches(long microseconds) {   return microseconds / 74 / 2; }
long microsecondsToCentimeters(long microseconds) {   return microseconds / 29 / 2; }


void setup() {
  
Serial.begin(9600); // Starting Serial Terminal
}
void loop() {
  
long duration, inches, cm;
   pinMode
(pingPin, OUTPUT);
   digitalWrite
(pingPin, LOW);
   delayMicroseconds
(2);
   digitalWrite
(pingPin, HIGH);
   delayMicroseconds
(10);
   digitalWrite
(pingPin, LOW);
   pinMode
(echoPin, INPUT);
   duration
= pulseIn(echoPin, HIGH);
   inches
= microsecondsToInches(duration);
   cm
= microsecondsToCentimeters(duration);
  
Serial.print(inches);
  
Serial.print("in, ");
  
Serial.print(cm);
  
Serial.print("cm");
  
Serial.println();
   delay
(100);


} Code to Note

The Ultrasonic sensor has four terminals - +5V, Trigger, Echo, and GND connected as follows −


  • Connect the +5V pin to +5v on your Arduino board.
  • Connect Trigger to digital pin 7 on your Arduino board.
  • Connect Echo to digital pin 6 on your Arduino board.
  • Connect GND with GND on Arduino.
In our program, we have displayed the distance measured by the sensor in inches and cm via the serial port.
Result
You will see the distance measured by sensor in inches and cm on Arduino serial monitor.


Comments

Popular posts from this blog

The laws of set algebra & Laws derivable >> in the table 5.1 & 5.2 :

Ans :       Commutative laws                             Ans:          Associative laws Ans:         Distributive laws       Ans:            Identity laws Ans:         Complement laws Ans:          Absorption laws Ans:         Minimization laws Ans:         De Morgan’s laws

Exercise Chapter7 : Vectors

VECTORS AND SCALARS: BASIC CONCEPTS Exercise 7.2 Solution : 1. For the arbitrary points A, B, C, D and E, and a single  vector which is equivalent to A:   DC + CB    = DB B:  CE + DC   = DE 2.  shows a cube. Let p =  AB, q =  AD and  r =  AE . Express the vectors representing  BD,  AC and  AG in terms of p, q and r. Consider the triangle ABD shown in Figure. We note that  BD represents the third  side of the triangle formed when AD are placed head to tail. Using the triangle law we find :                      AB + BD = AD               => BD = AD - AB                             = q - p Consider the triangle  ADC  shown in Figure. We note that  AD  represe...

Matrix algebra

                 Matrices provide a means of storing large quantities of information in such a way that each piece can be easily identified and manipulated. They permit the solution of large systems of linear equations to be carried out in a logical and formal way so that computer implementation follows naturally. Applications of matrices extend over many areas of engineering including electrical network analysis and robotics. 1-BASIC DEFINITIONS                  Matrix is a rectangular pattern or array of numbers.      For example:                    are all matrices. Note that we usually use a capital letter to denote a matrix, and enclose the array of numbers in brackets. To describe the size of a matrix we quote its number of rows and columns in that order so, for example, an r × s matrix has r rows and s colu...