Arduino Ultrasonic Ranging rise piece 07-

Ultrasonic Ranging

Ultrasound is a mechanical wave vibration frequency higher than 20KHz, which has a high frequency, wave length, small diffraction phenomenon, good direction, to become oriented with the spread of radiation and other characteristics, widely used in industrial, defense, biomedical, this article describes the use of ultrasonic distance measurement.

1. ranging principle

The transmitter transmits ultrasonic sensor to the ultrasonic wave in one direction, while the emission start timing of ultrasonic wave propagation in the air, on the way to return immediately to encounter an obstacle, the reflected ultrasonic wave received by the receiver immediately stop the clock. The timer records the time t, the speed of sound in air is 340m / s, it can be calculated from the emission point from the obstacle s, namely: s = 340m / s × t / 2. This is called time difference ranging method.

2. Introduction ultrasonic sensor

The ultrasonic sensor is to convert ultrasonic signals into other energy signal "is usually an electrical signal" sensor. HC-SR04 is more commonly used ultrasonic sensor module, the robot ranging widely used, and so the car obstacle avoidance.

Ultrasonic module

The main parameters

  • Voltage: DC5V
  • Current: 15mA
  • Frequency: 40KHz
  • Induction angle: <15 degrees
  • Detection range: 2cm-400cm
  • Accuracy: 3mm

Instructions for use

  • Trig trigger pin using the ranging, to the high-level signal at least 10us.
  • Module automatically sends 8 40KHz square wave, and automatically detects whether a signal is returned.
  • After the signal returned by Echo pin output high, the amount of time acquired by the high level duration.

3. Experimental Materials

  • Uno R3 Development Board
  • Supporting USB data cable
  • Bread plate and supporting cables
  • HC-SR04 ultrasonic sensor module

4. Experimental Procedure

1. The schematic circuit diagram of a building.

Ultrasonic sensor module VCC, GND are connected to the development board 5V, GND. Trig sensor pin, Echo pins are connected to pins 2 and 3 developed a digital board.

Principle is shown below:

Experimental schematics

Physical connection is shown below:

Physical connection diagram

2. Create a new sketch, the following code replacement copies of the automatically generated code and save it.

#define TrigPin 2
#define EchoPin 3

float Value_cm;

void setup() {
  Serial.begin(9600);
  pinMode(TrigPin, OUTPUT);
  pinMode(EchoPin, INPUT);
}

void loop() {
  digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
  delayMicroseconds(2);
  digitalWrite(TrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigPin, LOW);
  Value_cm = float( pulseIn(EchoPin, HIGH) * 17 ) / 1000; 
  //读取一个引脚的脉冲(HIGH或LOW)。例如,如果value是HIGH,pulseIn()会等待引脚变为HIGH,开始计时,再等待引脚变为LOW并停止计时。
  //接收到的高电平的时间(us)*340m/s/2=接收到高电平的时间(us)*17000cm/1000000us = 接收到高电平的时间*17/1000(cm) 
  Serial.print(Value_cm);
  Serial.println("cm");
  delay(1000);
}

3. Development Board connector, and set the port number corresponding to the type of boards, for download.

Download

The experimental results

Open the port monitor, the program provided baud rate consistent 9600. Changing the distance of the ultrasonic sensor and the shield, the monitor will display the corresponding distance.

Experimental phenomena
Focus on micro-channel public number: TonyCode
the Arduino learning exchange group: 868 283 450

More, I welcome the attention of the public number. Sweep the micro-channel to follow the Fanger Wei code:
Micro channel scan code added public number: TonyCode

Published 63 original articles · won praise 250 · Views 230,000 +

Guess you like

Origin blog.csdn.net/TonyIOT/article/details/103232332