To interface an ultrasonic pair and measure the distance in centimeters , calculate the error
- ultrasonic sensor module HC-SR04
- 1 KΩ resistor
- Arduino Uno
- USB Interfacing cable
- Connecting wires
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 uses sonar to determine the distance to an object. Here’s what happens:
The ultrasound transmitter (trig pin) emits a high-frequency sound (40 kHz). The sound travels through the air. If it finds an object, it bounces back to the module. The ultrasound receiver (echo pin) receives the reflected sound (echo). The time between the transmission and reception of the signal allows us to calculate the distance to an object. This is possible because we know the sound’s velocity in the air. Here’s the formula:
distance to an object = ((speed of sound in the air)*time)/2 speed of sound in the air at 20ºC (68ºF) = 343m/s
- Connect the circuit as per the circuit diagram
- Connect the board to your computer via the USB cable.
- If needed, install the drivers.
- Launch the Arduino IDE.
- Select the board (If you the board is arduino uno, select accordingly).
- Select your serial port, accordingly ( E.g. COM5 )
- Open the file of the program and verify the error , clear if any errors that are existing
- Upload the program and check for the physical working.
- Ensure safety before powering up the device
- Plot the graph for the output voltage vs the resistance
#define echoPin 9
#define trigPin 10
long duration;
int distance;
void setup(){
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop(){
digitalWrite(trigPin, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034/2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
Average error = sum/ number of readings
= 0.5+0.7+0.4+0.8+1.0
= 3.4/5
=0.68
Thus the distance value is measured in "CM" using ultrasonic sensor.