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
uno const int trigPin = 10; const int echoPin = 9; int led1 = 11;//red int led2 = 12;//green long duration; float distance=0; void setup() { pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(trigPin,OUTPUT); pinMode(echoPin , INPUT); Serial.begin(9600); }
void loop() { digitalWrite(trigPin,LOW); delay(20); digitalWrite(trigPin,HIGH); delay(20); digitalWrite(trigPin,LOW); duration = pulseIn(echoPin,HIGH); distance = duration * 0.034/2; Serial.print("Distance = "); Serial.print(distance); Serial.println(" cm");
if(distance >20){ digitalWrite(led2,LOW); digitalWrite(led1,HIGH); delay(300); digitalWrite(led1,LOW); delay(300); } else{ digitalWrite(led1,LOW); digitalWrite(led2,HIGH); delay(300); digitalWrite(led2,LOW); delay(300); }
}
Average error = sum/ number of readings Average error = (0.07+0.12+0.08+0.07+0.08)/5 = 0.84
Arduino uno is interfaced with Ultrasonic sensor and output values are indicated on a graph.