|

楼主 |
发表于 2019-9-21 16:06:24
|
显示全部楼层
- /*
- 【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
- 实验一百一十八:US-015 高分辨超声波测距模块 超声波传感器 US-020升级版
- 项目:测试距离(单位mm)
- 原理:声波在空气中传播速度为340m/s,根据计时器记录时间t,即可算出发射点距离障碍物的距离S,即S=340m/s*t/2,这就是所谓的时间差测距法。
- */
- unsigned int EchoPin = 2; // connect Pin 2(Arduino digital io) to Echo at US-015
- unsigned int TrigPin = 3; // connect Pin 3(Arduino digital io) to Trig at US-015
- unsigned long Time_Echo_us = 0;
- unsigned long Len_mm = 0;
- void setup()
- { //Initialize
- Serial.begin(9600); //Serial: output result to Serial monitor
- pinMode(EchoPin, INPUT); //Set EchoPin as input, to receive measure result from US-015
- pinMode(TrigPin, OUTPUT); //Set TrigPin as output, used to send high pusle to trig measurement (>10us)
- }
- void loop()
- {
- digitalWrite(TrigPin, HIGH); //begin to send a high pulse, then US-015 begin to measure the distance
- delayMicroseconds(20); //set this high pulse width as 20us (>10us)
- digitalWrite(TrigPin, LOW); //end this high pulse
-
- Time_Echo_us = pulseIn(EchoPin, HIGH); //calculate the pulse width at EchoPin,
- if((Time_Echo_us < 60000) && (Time_Echo_us > 1)) //a valid pulse width should be between (1, 60000).
- {
- Len_mm = (Time_Echo_us*34/100)/2; //calculate the distance by pulse width, Len_mm = (Time_Echo_us * 0.34mm/us) / 2 (mm)
- Serial.print("Present Distance is: "); //output result to Serial monitor
- Serial.print(Len_mm, DEC); //output result to Serial monitor
- Serial.println("mm"); //output result to Serial monitor
- }
- delay(1000); //take a measurement every second (1000ms)
- }
复制代码
|
|