Example Effect
Press the serial monitor, and you can see that the measured distance is 9.8cm-10.1cm with an error of about 0.2cm.
HC-SR04
An ultrasonic sensor used for distance measurement, widely applied in robotics to avoid obstacles and measure distances.
Its module uses Trig to trigger distance measurement,
which emits 8 40kHz square waves and automatically detects if there is a signal return.
If a signal is returned, it outputs a high level via echo, and the duration of the high level is twice the distance.
The formula is as follows:
Measuring Distance = (High Level Time * Speed of Sound) / 2
Main Technical Parameters
1: Operating Voltage: DC—5V 2: Static Current: less than 2mA 3: Output Level: High 5V 4: Output Level: Low 0V 5: Sensing Angle: not greater than 15 degrees 6: Detection Distance: 2cm-450cm 7: High Precision up to 0.2cm
BOM List
Arduino Uno *1
HC-SR04 Ultrasonic Sensor *1
Breadboard *1
Jumper Wires Several
Ruler *1
Obstacle *1
Pin Description: VCC — Supply 5V Power
TRIG — Trigger Control Signal Input
ECHO — Echo Signal Output and four other interface terminals
GND — Ground
Wiring Method
A relatively intuitive way:
Or refer to:
Program Implementation
Download the program for free: https://u16460183.ctfile.com/fs/16460183-295242528
#define Trig 2
#define Echo 3
float cm;
float temp;
void setup() {
Serial.begin(9600);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
}
void loop() {
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig,HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
temp = float(pulseIn(Echo, HIGH));
cm = (temp * 17 )/1000;
Serial.print("Echo =");
Serial.print(temp);
Serial.print(" | | Distance = ");
Serial.print(cm);
Serial.println("cm");
delay(100);
}
Leave a Comment
Your email address will not be published. Required fields are marked *