## **超聲波測距傳感器**
> **SEN136B5B**是一款來自**Seeedstudio**的超聲波測距傳感器。(不推薦購買,Seeed實在太貴,可以買其他替代)它能探測從傳感器前3cm到400cm的物體。它的原理是先發射一束超聲波并且開始計時,等到超聲波碰到前方物體就會發生反射,超聲波測距傳感器在收到回聲后會停止計時,通過這個時間結合聲音的速度可以判定前方距離。?
> 要啟動測距過程,Arduino或Genuino板必須要在超聲波測距傳感器的引腳上發送一個很短的脈沖。然后使用**pulseln()**函數監聽相同引腳上的脈沖寬度。測量到的脈沖寬度就是聲音傳播的時間了。
### **所需硬件**
* Arduino板或Genuino板
* SEN136B5B超聲波測距傳感器(可以用國產便宜的替代哦)
* 跳線
* 連接線
### **電路**
?
SEN136B5B的**5V**引腳連接到板子上的**+5V**,**GND**引腳連接到板子上的**GND**。SIG引腳(signal (信號)的縮寫)連接到7號引腳。
### **原理圖**

### **代碼**
~~~
/*
超聲波測距傳感器
本例通過讀取超聲波測距傳感器的信息來確定與前方物體的距離。首先,我們應該在7號引腳上發送一個短脈沖來初始化測距。接著,我們對7號引腳上的脈寬進行測量,以確定聲音傳播的時間。最終我們根據這個時間通過數學運算測量距離。
電路搭建:
* +V 連接到 +5V
* GND 連接到 GND
* SIG 連接到 連接到7號引腳
代碼公開
*/
// 不可變常量:
const int pingPin = 7;
void setup() {
// 初始化串口:
Serial.begin(9600);
}
void loop() {
// 創建變量存儲脈沖寬度和距離(用英尺和厘米兩種單位保存);
// 超聲波測距傳感器的測距過程可以通過一個2毫秒或以上的脈沖來觸發。(脈沖并不是很神秘,說白了就是引腳先輸出低電平,突然輸出2ms的高電平然后再拉低,就產生了2ms的脈沖。因而只需要用普通引腳進行數字操作)
// 先拉低,確保脈沖明顯(下面會產生一個5ms脈沖):
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// 在相同引腳上我們需要測量脈寬,這個新脈沖是超聲波傳感器產生的,它的長度就是聲音傳播的時間。
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//將時間換算成距離
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToInches(long microseconds) {
// 根據Parallax提供的數據手冊,超聲波的速度是73.746毫秒每英寸(1130英尺每秒)。我們已經可以算出聲音傳播的距離,別忘了除以2,因為聲音是過去再回來,走了兩遍。
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
// 聲速是340 m/s(每走1cm大約29ms)
// 我們已經可以算出聲音傳播的距離,別忘了除以2,因為聲音是過去再回來,走了兩遍。
return microseconds / 29 / 2;
}
~~~
### **相關資料**
[pinMode()](https://www.arduino.cc/en/Reference/PinMode)?
[delayMicroseconds()](https://www.arduino.cc/en/Reference/DelayMicroseconds)?
[pulseIn()](https://www.arduino.cc/en/Reference/PulseIn)?
[digitalWrite()](https://www.arduino.cc/en/Reference/DigitalWrite)?
[return](https://www.arduino.cc/en/Reference/Return)?
[serial.begin()](https://www.arduino.cc/en/Serial/Begin)?
[serial.print()](https://www.arduino.cc/en/Serial/Print)?
[原文鏈接](https://www.arduino.cc/en/Tutorial/Ping)
- 說明
- 系統示例文件目錄結構及說明
- 01.Basics
- AnalogReadSerial
- BareMinimum
- Blink
- DigitalReadSerial
- Fade
- ReadAnalogVoltage
- 02.Digital
- BlinkWithoutDelay
- Button
- Debounce
- DigitalInputPullup
- StateChangeDetection
- toneKeyboard
- toneMelody
- toneMultiple
- tonePitchFollower
- 03.Analog
- AnalogInOutSerial
- AnalogInput
- AnalogWriteMega
- Calibration
- Fading
- Smoothing
- 04.Communication
- ASCIITable
- Dimmer
- Graph
- Midi
- MultiSerial
- PhysicalPixel
- ReadASCIIString
- SerialCallResponse
- SerialCallResponseASCII
- SerialEvent
- SerialPassthrough
- VirtualColorMixer
- 05.Control
- Arrays
- ForLoopIteration
- IfStatementConditional
- switchCase
- switchCase2
- WhileStatementConditional
- 06.Sensors
- ADXL3xx
- Knock
- Memsic2125
- Ping
- 07.Display
- barGraph
- RowColumnScanning
- 08.Strings
- CharacterAnalysis
- StringAdditionOperator
- StringAppendOperator
- StringCaseChanges
- StringCharacters
- StringComparisonOperators
- StringConstructors
- StringIndexOf
- StringLength
- StringLengthTrim
- StringReplace
- StringStartsWithEndsWith
- StringSubstring
- StringToInt
- 09.USB
- Keyboard
- KeyboardLogout
- KeyboardMessage
- KeyboardReprogram
- KeyboardSerial
- KeyboardAndMouseControl
- Mouse
- ButtonMouseControl
- JoystickMouseControl
- 10.StarterKit_BasicKit (與特定硬件相關,暫無)
- p02_SpaceshipInterface
- p03_LoveOMeter
- p04_ColorMixingLamp
- p05_ServoMoodIndicator
- p06_LightTheremin
- p07_Keyboard
- p08_DigitalHourglass
- p09_MotorizedPinwheel
- p10_Zoetrope
- p11_CrystalBall
- p12_KnockLock
- p13_TouchSensorLamp
- p14_TweakTheArduinoLogo
- p15_HackingButtons
- 11.ArduinoISP(暫無)
- ArduinoISP