





Preview text:
HCSR04UltrasonicSensor ElijahJ.Morgan Nov.162014
ThepurposeofthisfileistoexplainhowtheHCSR04works.Itwillgiveabrief
explanationofhowultrasonicsensorsworkingeneral.Itwillalsoexplainhowtowire
thesensoruptoamicrocontrollerandhowtotake/interpretreadings.Itwillalsodiscuss
somesourcesoferrorsandbadreadings.
1. HowUltrasonicSensorsWork
2. HCSR04Specifications
3. Timingchart,PinexplanationsandTaking DistanceMeasurements
4. WiringHCSR04withamicrocontroller
5. ErrorsandBadReadings
1.HowUltrasonicSensorsWork
Ultrasonicsensorsusesoundtodeterminethedistancebetweenthesensorandthe
closestobjectinitspath.Howdoultrasonicsensorsdothis?Ultrasonicsensorsare
essentiallysoundsensors,buttheyoperateatafrequencyabovehumanhearing.
Thesensorsendsoutasoundwaveataspecificfrequency.Itthenlistensforthatspecific
soundwavetobounceoffofanobjectandcomeback(Figure1).Thesensorkeepstrack
ofthetimebetweensendingthesoundwaveandthesoundwavereturning.Ifyouknow
howfastsomethingisgoingandhowlongitistravelingyoucanfindthedistance
traveledwithequation1.
Equation1. d = v × t
Thespeedofsoundcanbecalculatedbasedontheavarietyofatmospheric
conditions,includingtemperature,humidityandpressure.Actuallycalculatingthe
distancewillbeshownlateroninthisdocument.
Itshouldbenotedthatultrasonicsensorshaveaconeofdetection,theangleof
thisconevarieswithdistance,Figure2showthisrelation.Theabilityofasensorto
detectanobjectalsodependsontheobjectsorientationtothesensor.Ifanobjectdoesn’t
presentaflatsurfacetothesensorthenitispossiblethesoundwavewillbounceoffthe
objectinawaythatitdoesnotreturntothesensor.
2.HCSR04Specifications
ThesensorchosenfortheFirefightingDroneProjectwastheHCSR04.This
sectioncontainsthespecificationsandwhytheyareimportanttothesensormodule.The
sensormodulesrequirementsareasfollows. ● Cost ● Weight
● Communityofhobbyistsandsupport
● Accuracyofobjectdetection
● Probabilityofworkinginasmokyenvironment ● Easeofuse
TheHCSR04Specificationsarelistedbelow.Thesespecificationsarefromthe
CytronTechnologiesHCSR04User’sManual(source1).
● PowerSupply:+5VDC
● QuiescentCurrent:<2mA
● Workingcurrent:15mA
● EffectualAngle:<15º
● RangingDistance:2400cm ● Resolution:0.3cm
● MeasuringAngle:30º
● TriggerInputPulsewidth:10uS
● Dimension:45mmx20mmx15mm
● Weight:approx.10g
TheHCSR04’sbestsellingpointisitsprice;itcanbepurchasedataround$2per unit.
3.TimingChartandPinExplanations
TheHCSR04hasfourpins,VCC,GND,TRIGandECHO;thesepinsallhave
differentfunctions.TheVCCandGNDpinsarethesimplesttheypowertheHCSR04.
Thesepinsneedtobeattachedtoa+5voltsourceandgroundrespectively.Thereisa
singlecontrolpin:theTRIGpin.TheTRIGpinisresponsibleforsendingtheultrasonic
burst.ThispinshouldbesettoHIGHfor10μs,atwhichpointtheHCSR04willsend
outaneightcyclesonicburstat40kHZ.AfterasonicbursthasbeensenttheECHOpin
willgoHIGH.TheECHOpinisthedatapinitisusedintakingdistance
measurements.AfteranultrasonicburstissentthepinwillgoHIGH,itwillstayhigh
untilanultrasonicburstisdetectedback,atwhichpointitwillgoLOW.
TakingDistanceMeasurements
TheHCSR04canbetriggeredtosendoutanultrasonicburstbysettingtheTRIG
pintoHIGH.OncetheburstissenttheECHOpinwillautomaticallygoHIGH.Thispin
willremainHIGHuntilthethebursthitsthesensoragain.Youcancalculatethedistance
totheobjectbykeepingtrackofhowlongtheECHOpinstaysHIGH.ThetimeECHO
staysHIGHisthetimetheburstspenttraveling.Usingthismeasurementinequation1
alongwiththespeedofsoundwillyieldthedistancetravelled.Asummaryofthisis
listedbelow,alongwithavisualrepresentationinFigure2. 1. SetTRIGtoHIGH
2. SetatimerwhenECHOgoestoHIGH
3. KeepthetimerrunninguntilECHOgoestoLOW 4. Savethattime
5. Useequation1todeterminethedistancetravelled Source2
Tointerpretthetimereadingintoadistanceyouneedtochangeequation1.The
clockonthedeviceyouareusingwillprobablycountinmicrosecondsorsmaller.Touse
equation1thespeedofsoundneedstodetermined,whichis343meterspersecondat
standardtemperatureandpressure.Toconvertthisintomoreusefulformuseequation2
tochangefrommeterspersecondtomicrosecondspercentimeter.Thenequation3can
beusedtoeasilycomputethedistanceincentimeters. Speed Equation2. istance D = Meters e S 58.772 μS 170.15 m × 100 cm × 1 6 μ 170.15 m × cm Equation3. istance D time s m = 58 = μ μs/cm = c
4.WiringtheHCSR04toaMicrocontroller
Thissectiononlycoversthehardwareside.Forinformationonhowtointegrate
thesoftwareside,lookatoneofthelinksbeloworlookintothespecificmicrocontroller youareusing.
TheHCSR04has4pins:VCC,GND,TRIGandECHO.
1. VCCisa5vpowersupply.Thisshouldcomefromthemicrocontroller
2. GNDisagroundpin.Attachtogroundonthemicrocontroller.
3. TRIGshouldbeattachedtoaGPIOpinthatcanbesettoHIGH
4. ECHOisalittlemoredifficult.TheHCSR04outputs5v,whichcoulddestroy
manymicrocontrollerGPIOpins(themaximumallowedvoltagevaries).Inorder
tostepdownthevoltageuseasingleresistororavoltagedividercircuit.Once
againthisdependsonthespecificmicrocontrolleryouareusing,youwillneedto
findoutitsGPIOmaximumvoltageandmakesureyouarebelowthat.
5.ErrorsandBadReadings
Ultrasonicsensorsaregreatsensorstheyworkwellformanyapplications
whereothertypesofsensorsfallshort.Unfortunately,theydohaveweaknesses.These
weaknessescanbemitigatedandworkedaround,butfirsttheymustbeunderstood.The
firstweaknessisthattheyusesound.Thereisalimittohowfastultrasonicsensorscan
getdistancemeasurements.Thelongerthedistance,theslowertheyareatreportingthe
distance.Thesecondweaknesscomesfromthewaysoundbouncesoffofobjects.In
enclosedspacesitispossible,ifnotprobablethattherewillbeunintendedechos.The
echoscanveryeasilycausefalseshortreadings.InFigure2apulsewassentout.It
bouncedoffofobject1andreturnedtothesensor.Thedistancewasrecordedandthena
newpulsewassent.Therewasanotherobjectfartheraway,sothatwhenthenewpulse
reachesobject1,thefirstsignalwillreachthesensor.Thiswillcausethesensortothink
thatthereisanobjectcloserthanisactuallytrue.Theoldpulseissmallerthanthenew
pulsebecauseithasgrownweaker.Thelongerthepulseexiststheweakeritgrowsuntil
itisnegligible.Ifmultiplesensorsarebeingused,thenumberofechoswillincrease
alongwiththenumberoferrors.Therearetwomainwaystoreducethenumberoferrors.
Thefirstistoprovideshieldingaroundthesensor.Thispreventsechoscominginfrom
angleoutsidewhatthesensorshouldactuallypickup.Thesecondistoreducethe
frequencyatwhichpulsesaresentout.Thisgivesmoretimefortheechostodissipate. WorksCited Source1.
“HCSR04User's_Manual.”
CytronTechnologies,May2013Web.5Dec. docs.google. 2009. aG73rE/edit> Source2.
“Attiny2313Ultrasonicdistance(HRSR04)example.”CircuitDB.n.a.7Sept.2014 Web.5Dec.2014. Links
Thesearenotformatted;youwillneedtocopyandpastethemintoyourwebbrowser.
WanttolearnaboutUltrasonicSensorsingeneral?
http://www.sensorsmag.com/sensors/acousticultrasound/choosingultrasonicsensorprox
imityordistancemeasurement825
AllabouttheHCSR04
● http://www.circuitdb.com/?p=1162
● http://www.micropik.com/PDF/HCSR04.pdf
● http://randomnerdtutorials.com/completeguideforultrasonicsensorhcsr04/
● http://www.ezdenki.com/ultrasonic.php
(^fantastictutorial,explainsalotofstuff)
● http://www.elecrow.com/hcsr04ultrasonicrangingsensorp316.html
(^thisonehassomecoolcharts)