Editing Ultrasonic Sensor HC-SR04

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 3: Line 3:
 
It sends out an ultrasound signal and detects the echo. By measuring the time you can calculate the distance.
 
It sends out an ultrasound signal and detects the echo. By measuring the time you can calculate the distance.
  
[[File:Hc-sr04-1.JPG|x300px]]
+
[[File:Ultra1.JPG|300px]]
[[File:Ultra1.JPG|x300px]]
 
  
 
more details:
 
more details:
Line 12: Line 11:
 
= How to connect it electrically =
 
= How to connect it electrically =
  
[[File:Hc-sr04-e.JPG|x420px]]
+
[[File:Stepper-c.JPG|x420px]]
 +
 
  
 
= Required Module and Files =
 
= Required Module and Files =
Line 43: Line 43:
 
= Related Tutorial Videos =
 
= Related Tutorial Videos =
 
<youtube>PJdDUZWeIDs</youtube>
 
<youtube>PJdDUZWeIDs</youtube>
 
 
= Background =
 
connecting it to the ESP8266 and reading the signal in without a library/module.
 
 
Connect Trig to Pin5 and Echo to Pin4 of the ESP8266.
 
 
[[File:Hc-03.JPG|x300px]]
 
[[File:Hc04-03.JPG|x300px]]
 
[[File:Hc-sr04-1.JPG|x300px]]
 
 
 
<syntaxhighlight lang="python" line='line'>
 
import machine
 
import utime
 
 
 
while True:
 
  trig=machine.Pin(5, machine.Pin.OUT)
 
  trig.off()
 
  utime.sleep_us(2)
 
  trig.on()
 
  utime.sleep_us(10)
 
  trig.off()
 
  echo=machine.Pin(4, machine.Pin.IN)
 
  while echo.value() == 0:
 
    pass
 
  t1 = utime.ticks_us()
 
  while echo.value() == 1:
 
    pass
 
  t2 = utime.ticks_us()
 
  cm = (t2 - t1) / 58.0
 
  print(cm)
 
  utime.sleep(2)
 
 
 
</syntaxhighlight>
 

Please note that all contributions to Sketching with Hardware at LMU Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see My wiki:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)