Ultrasonic Sensor Vs IR Sensor
Last Updated :
07 Apr, 2025
Arduino is a microcontroller that requires sensors to sense the surroundings to process and react to it. Ultrasonic Sensor is an electronic device that measures the distance between the transmitter and the obstetrical using ultrasonic sound waves. It converts the sound reflected sound into an electric signal to provide a numeric output.
Ultrasonic waves travel faster than the speed of audible sound because their frequency is lower and wavelength is maximum. The ultrasonic sensor measures the distance depending upon the time taken to receive the wave back, hence it measures the total distance traveled by the sound, which is double the distance between the transmitter and obstacle.
What is an Ultrasonic Sensor?
An ultrasonic sensor is an electronic device that calculates the time taken by the ultrasonic wave to travel from the emitter to the receiver. Ultrasound waves travel faster than sound. Therefore, the operation of ultrasonic sensors involves sound waves to determine the distance to an object. A transducer is used to send and receive ultrasonic pulses.
Ultrasonic SensorComponents and Pin Diagram of Ultrasonic Sensor
Ultra Sensor haves only 4 pins: Trig, Echo, GND, Vcc. Lets see the components and its functionality with these pins:
- Transmitter: It transmits ultrasonic waves when input signal is provided to the sensor using the Triger Pin(Trig).
- Receiver: It receives the transmitted ultrasonic wave and measures the total distance and send the output to the Echo Pin, which takes the data to the micro controller for further operation.
- Vcc Pin: This is a power source pin which provides power supply to the sensor.
- GND Pin: This is a ground pin which neutralize the power from the sensor and mention the flow of current in the circuit.
Features of an Ultrasonic Sensor
- Supply voltage: 5V (DC).
- Supply current: 15mA.
- Modulation frequency: 40Hz.
- Output: 0 – 5V (Output high when obstacle detected in range).
- Beam Angle: Max 15 degrees.
- Distance: 2 cm – 400 cm.
- Accuracy: 0.3cm.
- Communication: Positive TTL pulse.
Working Principle of Ultrasonic Sensor
Ultrasonic Sensor works upon the principle of sound waves. Ultrasonic sensors transmits short, high-frequency sound waves at regular intervals. These signal moves through the air at the speed of sound. When these signal hits an object, they return back to the sensor as an echo signal, and the sensor itself calculates the distance to the target based on the time delay of the transmission signal and receives an echo.
In order to calculate the distance, this formula is being used:
D = ½ T x C
where,
D is the distance,
T is the time, and
C is the speed of sound (346 meters/second)
Ultrasonic sensor having mainly two component: the transmitter (emits the ultrasonic sound waves) and the receiver (which encounters the ultrasonic sound wave which travels to and from the obstacle).
Code for Ultrasonic Sensor
Code for Ultrasonic Sensor:
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration; // stores time taken to recieve the refleced sound wave
int distance; // stores distance from object
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output Pin
pinMode(echoPin, INPUT); // Sets the echoPin as an Input Pin
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the TrigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds which allows Input from Ultrasonic Sensor
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin and returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
What is an IR Sensor?
IR Sensor is an electronic device that measure and detects infrared radiation from the surrounding environment. It do not measures the distance between the sensor and the object. It is fast responsive and non atmosphere dependent sensor. It works upon the principle of Infrared Light. Its frequency is high that's why its range is very low. It only detects obstacle in straight line.
IR Sensor
Features of an Infrared (IR) Sensor
- Supply Voltage: 5V (DC)
- Supply Current: 20mA
- Modulation Frequency: 38kHz
- Output: Digital signal (0 or 1) (Output high when obstacle detected)
- Detection Range: 2 cm – 30 cm (varies based on model)
- Beam Angle: Approximately 35 degrees
- Accuracy: ±0.5 cm
- Communication: Digital output (compatible with microcontrollers)
Components of IR Sensor and its Working
There are different types of infrared emitters depending upon wavelength, output power and response time of operation. The infrared sensor consists of two main components:
- An infrared LED: This is an emitter which emits light waves which hits the object.
- An infrared Photodiode: This is used to receive the reflected light waves which was hit by the object.
Note: Collectively an IR LED and an IR PhotoDiode is called an OptoCoupler or PhotoCoupler.
Code for IR Sensor
int LEDpin = 13;
int obstaclePin = 10;
int hasObstacle = LOW; // Initialy set for no obsticle
void setup()
{
pinMode(LEDpin, OUTPUT);
pinMode(obstaclePin, INPUT);
Serial.begin(9600);
}
void loop()
{
hasObstacle = digitalRead(obstaclePin);
if (hasObstacle == HIGH)
{
Serial.println("Stop something is ahead!!");
digitalWrite(LEDpin, HIGH);
}
else
{
Serial.println("Path is clear");
digitalWrite(LEDpin, LOW);
}
delay(200);
}
Difference Between Ultrasonic and IR Sensor
Given Below is the Table between Ultrasonic and IR Sensor
Parameter | IR Sensor | Ultrasonic Sensor |
---|
Work Principle | InfraRed Light | Ultrasonic Sound |
Response Time | High | Low |
Atmospheric Impacts | No Impact on Accuracy | Impacts Accuracy |
Measures Distance | No | Yes |
Range | 10cm - 80cm | 2cm - 10m |
Beam Angle | 75 Degree | 30 Degree |
Beam Pattern | Narrow(line) | Conical |
Frequency | 353 THz | 40KHz |
Unit Cost | 750 INR | 130 INR |
Conclusion
Arduino - Ultrasonic Sensor is a multipurpose, low cost, highly useful electronic device which is a part of IOT products development. This sensor can be used to track record of data where Infrared Sensor fails to deliver good performance.
Similar Reads
Arduino - Ultrasonic Sensor Sensors are widely used for detecting devices by approximating their distance from the source. One such example of a sensor is the HC-SR04 ultrasonic sensor which uses the SONAR technique for the sensing purpose. The main feature of this sensor is to mimic the nature of bats and therefore predict th
7 min read
Temperature Sensor - Types of Sensor Temperature sensor: Temperature sensor is a device, used to measure the temperature using an electrical signal. It requires a thermocouple or RTD(Resistance temperature Detector). It is the most common and most popular sensor.Temperature sensor, the change in the temperature correspond to change in
3 min read
Distance measurement using Ultrasonic sensor and Arduino In This Article, We will learn how to measure the distance using an Ultrasonic sensor & Arduino. Arduino:It is an open-source electronics platform. It consists ATmega328 8-bit Microcontroller. It can be able to read inputs from different sensors & we can send instructions to the microcontrol
7 min read
Arduino - Water Detector / Sensor Sensors are widely used for detecting different quantities like the presence of light, level of water, etc. One such example of a sensor is the water sensor which uses various principles of physics to detect the presence of water. This sensor is known for detecting different water levels with accura
8 min read
Difference Between Sensor and Transducer Sensors and transducers are devices used in measurement systems, but they serve different purposes. A sensor detects and measures a physical property, providing raw data about the environment. A transducer, on the other hand, converts one form of energy into another, often transforming the signal fr
4 min read
Difference Between Sensor and Transducer Sensors and transducers are devices used in measurement systems, but they serve different purposes. A sensor detects and measures a physical property, providing raw data about the environment. A transducer, on the other hand, converts one form of energy into another, often transforming the signal fr
4 min read