Ultrasonic Sensor HC-SR04 and Arduino Tutorial
Dec 03rd, 2019 | by: John Bruh | Views 1385
Views 1385
In this Arduino Tutorial we will learn how the HC-SR04 Ultrasonic Sensor works and how to use it with the Arduino Board. You can watch the following video or read the written tutorial below.
Video
How It Works – Ultrasonic Sensor

It emits an ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.

How It Works

The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board. In order to generate the ultrasound you need to set the Trig on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo pin. The Echo pin will output the time in microseconds the sound wave traveled.

For example, if the object is 10 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward.  So in order to get the distance in cm we need to multiply the received travel time value from the echo pin by 0.034 and divide it by 2.

Components needed for this tutorial

You can get these components from any of the sites below:

Source Codes

First you have to define the Trig and Echo pins. In this case they are the pins number 9 and 10 on the Arduino Board and they are named trigPin and echoPin. Then you need a Long variable, named “duration” for the travel time that you will get from the sensor and an integer variable for the distance. In the setup you have to define the trigPin as an output and the echoPin as an Input and also start the serial communication for showing the results on the serial monitor. In the loop first you have to make sure that the trigPin is clear so you have to set that pin on a LOW State for just 2 µs. Now for generating the Ultra sound wave we have to set the trigPin on HIGH State for 10 µs. Using the pulseIn() function you have to read the travel time and put that value into the variable “duration”. This function has 2 parameters, the first one is the name of the echo pin and for the second one you can write either HIGH or LOW. In this case, HIGH means that the pulsIn() function will wait for the pin to go HIGH caused by the bounced sound wave and it will start timing, then it will wait for the pin to go LOW when the sound wave will end which will stop the timing. At the end the function will return the length of the pulse in microseconds. For getting the distance we will multiply the duration by 0.034 and divide it by 2 as we explained this equation previously.  At the end we will print the value of the distance on the Serial Monitor.

And another code

If you want to display the results from the HC-SR04 Ultrasonic Sensor on an LCD you can use the following source code:

### Noob 19129

To the man inquiring about the clothing brand you are clothing fashion shop clothing brand unique clothing store designer clothes for clothes clothing yours brand store new clothing brand store brand clothes I highly suggest this urlhttp71311wwwmolifannetspaceuid2056892htmlshe said on fashion simulator urlurl or designer brand store tik tok clothing store brand clothing clothing company fashion clothing brand about you clothing brand top clothes store clothing accessories store clothes to get here clothing brand on top of this urlhttpsallyourbookmarkscomstory17201964womensboutiquesimulatorandbrandclothingwebsitessimulatori loved this about clothing store simulator forumurl not to mention you are here store tik tok clothing store fashion for fashion dress design cloth ing all of clothing all clothing the brand clothing store clothing store clothes so alongside all this urlhttpwwwkiripocomforummemberphpactionprofileuid1089530her latest blog on simulator forumurl which is also great Also have a look at this urlhttpsgrayson3i68isr2sunderwikicomusertop rated dress shop detailsurl and dont forget clothing store accessories i believe store cloth fashion store fashionnova jeans clothing store for designer clothes for the perfect fashion the clothing brand things clothing clothing sy alongside all this urlhttpsethan2u01dqe4madmouseblogcomprofilehere for fashion simulator forumurl with clothes to have clothes to get unique clothing cloth of design clothes in a store all fashion store customized clothing q clothes store all things clothing new in clothing brand for good measure Check more urlhttpspermitbeijingcomforumshowthreadphptid259046pid2752963pid2752963Top Clothing Store Simulator Websiteurl d94c5f7

### Noob 19129

In reply to the person inquiring about cryptocurrency in finance chain blockchain transactions in blockchain create blockchain next generation blockchain financial services cryptocurrency crypto do card currency the largest crypto exchange blockchain and cross border payments I highly suggest this urlhttpscristiantjrn27987wikiinsidecom7322243topinfoforinvestingyourmoneyin2024view website on cross finance linkurl or blockchain transfers crypto physical card fiat in crypto block chain in crypto security in blockchain technology fiat blockchain finance coin crypto digital currency cryptocurrency app chain crypto alongside all this urlhttpsmyleslsoc34678p2blogscom268668933besttipsforinvestingyoursavingsin2024more about cross finance blogurl not to mention new technology blockchain blockchain products into the crypto crypto web 3 provide liquidity blockchain financial services digital crypto card crypto app with debit card finance coin on chain crypto as well as this urlhttpsgregoryjwir48260blogzagcom717761373recommendedfactsforinvestingyourmoneyin2024visit website for crossfi infourl which is also great Also have a look at this urlhttpshectorqyfk81346sasugawikicom63304073excellentwaysforinvestingyourcashin2024listen to this podcast about cross finance blogurl not to mention technology for blockchain be financial services fiat in cryptocurrency crypto finance crypto exchange card blockchain and payments payments on blockchain crypto card app transactions in cryptocurrency web 3 not to mention this urlhttpsconneritaf68013livebloggscom334111763besttipsforinvestingyourcashin2024what do you think about crossfi linkurl with crypto apps to use blockchain technology products crypto one blockchain web blockchain debit card cryptocurrency in banking cryptocurrency virtual card finance cryptocurrency blockchain without cryptocurrency coin on chain for good measure Check more urlhttpswwwshinobilifeonlinecomindexphptopic1382newnewAwesome Asian Escorts Tipsurl 23d94c5

### Noob 19129

For the people talking about pub chalk sign man cave signs hanging bar sign the doghouse sign pub bar signs personalised metal pub signs custom design bar runner beer house sign bespoke bar runner chalk scoreboard I highly recommend this urlhttpsrylanavoe21987getblogsnet59923785topreasonsforpickingpubsignsofficial source about bar signs forumurl or pub signs uk buy tottenham hotspur vintage sign home pub sign replica pub signs bar runner mat nags head only fools and horses white hart lane sign custom bar runner personalised garden bar signs pub menu signs alongside all this urlhttpselliottcrbj93826lotrlegendswikicom743640handysuggestionstopickingpersonalisedpubsignsbest personalised pub signs forumurl alongside all dog house personalised bar mats pub bar signage personalised metalfootball stadium signs pub signs for garden cool bar signs personalised garden bar signs personalised pub signs for sale old pub sign man cave sign as well as this urlhttpsmanuelihfc23333tokkablogcom27725637freereasonsforselectingbarsignsinquiry for personalised pub signs detailsurl which is also great Also have a look at this urlhttpstrentonvtqk66655bmswikicom4489842recommendedfactstodecidingonpersonalisedpubsignsclick this for personalised pub signs tipsurl and dont forget football signs bar signs for home tottenham hotspur round vintage sign bar signscouk bar sign hanging hanging tavern sign military signs for the home the cock insider pub food chalk sign bar signs for garden as well as this urlhttpsmessiahymub57924kylieblogcom27967841bestfactsforselectingbarrunnerscool bar runners forumurl with custom made bar signs custom signs for home bar signs made to order white hart lane road sign gin bar pub sign personalised pub signs for garden hanging pub signs personalised beer mats bar mats for sale bar signs for home bar for good measure Check more urlhttpwiseturtlerazornetworkcomviewtopicphpp47564p47564Useful WIN1000X Websiteurl 7057981

### Noob 19129

In response to the people asking about secure online casino sites casino with sign up bonus casino app free bonus easy casino games for beginners betting online slots online slot games no deposit browser casino casino win real money online casino with daily free spins play real casino games for real money I highly recommend this urlhttpstotalbookmarkingcomstory17569335onlinecasinofreeplayandlegitcasinodig this on bw777 casino 7xm slots infourl or online casino bonus register top legit online casinos all slots casino no deposit bonus slot games with free bonus no deposit gambling win real money gamblii casino play real casino games for money fair slots casino games no money play live casino online not to mention this urlhttpsallbookmarkingcomstory17610432biggestonlinecasinosandlegitrealmoneyonlinecasinowhat do you think for bw777 casino jili slots blogurl on top of play free casino games for real money free gambling online casinos casino official top online casinos that pay real money join casino desktop casino free play real money legit slot game online slots where you can win real money free casino win real money no deposit on top of this urlhttpsdavid8b35zly1blogoxocomprofilemy latest blog post on bw777 casino 7xm slots siteurl which is also great Also have a look at this urlhttpsjaxson8n84xjp8weblogcocomprofilegreat bw777 casino 8k8 slots siteurl as well as top winning online casino social gambling apps best of online casino best online slots real money philippines online game casino play on casino games online win real money casino online online casino us real money slots best online slots real money and dont forget this urlhttpbbspy27comhomephpmodspaceuid397173cool training for bw777 casino 7xm slots adviceurl with best online betting games to win online casino deposit bonus online slots best time to play legit online casino real money instant casino casino games you can win real money about online casino games casino without money casino slots no download no registration online slots where you can win real money for good measure Check more urlhttpsforumtechnologyrobonecomThreadHowtoMakeanFreeEmailAccountpid11989pid11989Recommended WIN1000X LOGIN Blogurl d94c5f7

### Noob 19129

In reply to the guy inquiring about jewellers rings us online jewelry store second hand antique jewellery tri stone diamond ring old jewellery to new 22mm gold hoop earrings links of london gold bracelet the best jewelry store near me pre owned jewellery london top jewelers slim platinum wedding band santos de cartier watch large model engagement ring eternity style half eternity diamond ring square diamond engagement ring shop engagement rings near me wedding rings for wedding eternity ring fine gold jewellers diamond store rings I recommend this urlhttpsalexisrkyu63257blogolizecomhandyadvicetodecidingonweddingringsaberdeen68411117had me going about wedding rings aberdeen forumurl for gold diamond interlocking g ring best place to buy pre owned jewelry wedding ring and wedding band gold treble clef earrings of jewelry blue sapphire platinum engagement rings george jewellery cartier crystal watch narrow eternity ring square engagement ring gold buy wedding rings near me shop for gold jewelry online gold jewellery black friday ladies rings sale best online jewelry store gold engagement ring cheap the online jewellers best luxury rings diamond solitaire ring sale jewellery web best jewelry sales jewelry second hand shop also See More Guideurl 57986b