在本项目中,我们将使用超声波传感器来测量距离,检测物体并以常用的测量单位提供O/P。硬件组件:ArduinoUNO×1个超声波传感器-HC-SR04(通用)×1个面包板(通用)×1个跳线(通用)×1个USB-A至B电缆×1个软件应用程序和在线服务:ArduinoIDE可以在超声波传感器和Arduino之间直接建立连接(或),也可以使用Breadboard实现相同的功能。•将超声波传感器的Vcc引脚连接到Arduino上的5V•将超声波传感器的GND引脚连接到Arduino上的GND•将超声波传感器的Trig引脚连接到Arduino上的D6•将超声波传感器的回波针连接到Arduino上的D5带有Arduino的超声波传感器-使用面包板的电路连接:超声波传感器具有广泛的应用,例如距离测量,物体检测,避障等。该项目的设计牢记要寻找使用Arduino的超声波传感器的基本设置的人员。任何类型的Arduino均可用于此项目。工作非常简单,按照电路图进行连接并打开电源后,超声波传感器就会发送声波。当波与物体接触时,它们会反弹回传感器。因此,通过将速度乘以时间来计算距离。此处的速度是指声音的速度,即每秒340米。以厘米/微秒为单位为0.034,以英寸/微秒为单位0.0133858。我们必须将速度除以2,因为声波在撞击物体后向前传播并返回。另一方面,时间是声波向前传播和向后反弹所花费的持续时间。因此,很容易确定物体与超声波传感器的距离。您可以在下面的代码中找到计算。在串行监视器中看到的输出:相关代码:/**CreditstoallsourcesonGoogle,actingasreferencetothiscodebelow.Thiscodeisnot*TechValer'sowncreation.TechValerhasreferredtodifferentprojectsandwebsitesto*findaneasycodeforbeginnerstogetstartedwithUltrasonicSensorHC-SR04andArduino.*TechValerdoesnotclaimthiscodetobeitsown.TechValerisgreatlythankfulfororiginal*creatersofthiscodeandalsoallotherswhoactedasreference.*//**AboutTechValer**Whatcomestomindwhenyouthinkoftech...hmm,I'msureyou'rethinkingofiPhone,*Alexa,BostonDynamicsroboticdogetc.,atleastthat'swhatIwouldhavethoughtof.*Mypointhereis,whenyoulookinsidethistech...you'llfindweirdboardswith*componentsattachedtoit.ThisstuffiselectronicsandweatTechvalerdeeplyappreciate*thispieceoftech.Asthenamesuggests,weareTechEnthusiastswhoknowtheWorthand*Potentialoftheseamazingtechstuff!So,checkoutourwebsitetechvaler.comtofindout*more.I'msureyouwillnotregret...*//**Note:AnymodelofArduinocanbeusedforthisproject.Justkeepinmindthedigitalpins.*Thereadingsavailableintheserialmonitorarealmostaccurate.*Refertothedocumentationforbetterunderstandingofthisparticularcodeandproject.*//**ThankstoDronaAutomationsPvt.LtdforSponsoringthisproject!*/#definetrigPin6//ConnecttrigpintoD6onArduino#defineechoPin5//ConnectechopintoD5onArduino//definesvariableslongduration;//It'savariableforthedurationofsoundwavetravelintinchdistance;//It'savariableformeasurementofdistanceininchesintcmdistance;//It'savariableformeasurementofdistanceincentimetersvoidsetup(){pinMode(trigPin,OUTPUT);//SetsthetrigPinasanOUTPUTpinMode(echoPin,INPUT);//SetstheechoPinasanINPUTSerial.begin(9600);////ForSerialCommunication}voidloop(){digitalWrite(trigPin,LOW);//ClearsthetrigPinconditiondelayMicroseconds(2);digitalWrite(trigPin,HIGH);delayMicroseconds(10);digitalWrite(trigPin,LOW);duration=pulseIn(echoPin,HIGH);//ReadstheechoPin,returnsthesoundwavetraveltimeinmicroseconds//Calculatingthedistanceinchdistance=duration*0.0133858/2;//Tocalculatethedistanceininchcmdistance=duration*0.034/2;//Tocalculatethedistanceincm//DisplaysthedistanceontheSerialMonitorSerial.print("Distance:");Serial.print(inchdistance);Serial.println("inch");Serial.print(cmdistance);Serial.println("cm");}