Ultrasonic module experiment

Ultrasonic module experiment

Experimental phenomena

The distance between the serial port output ultrasonic module and the blocking object, in cm

Theoretical study

The principle of ultrasonic ranging is to emit ultrasonic waves from the ultrasonic transmitter. It is based on the time difference when the receiver receives the ultrasonic waves, which is similar to the principle of radar ranging. The ultrasonic transmitter emits ultrasonic waves in a certain direction and starts timing at the same time as the launch time. The ultrasonic waves propagate in the air and return immediately when encountering obstacles on the way. The ultrasonic receiver immediately stops timing when it receives the reflected waves.
Insert picture description here

Schematic diagram

Insert picture description here

Code writing

#define trigpin 2
#define echopin 3
float value_cm;
void setup() {
    
    
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT);
}

void loop() {
    
    
  // put your main code here, to run repeatedly:
  digitalWrite(trigpin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin, LOW);
  value_cm = float(pulseIn(echopin, HIGH) * 17) / 1000;
  Serial.print(value_cm);
  Serial.println("cm");
  delay(1000);
}

Guess you like

Origin blog.csdn.net/qq_45671732/article/details/109614588