Use an ultrasonic sensor to take distance measurements with your Raspberry Pi Pico.
How to Use an Ultrasonic Sensor with Raspberry Pi Pico : Read more
How to Use an Ultrasonic Sensor with Raspberry Pi Pico : Read more
You can save 3 wires if you line up the Trigger with pin 16 and echo with 17, that puts GND with GND .
One wire is needed to go from a 3V3 or the VSYS/VBUS if you need 5V and are powering via micro-USB.
I'm curious how some of this works in terms of the code.
I keep looking at this section:
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
dist = (timepassed * 0.0343) / 2
I keep trying to figure out exactly how this works. To my brain, it seems like this is just measuring the length of the incoming pulse. Someone correct me if I'm wrong, or let me know how this all works, but this is my logic -
How am I thinking of this wrong? Am I misunderstanding what utime.ticks_us() does? Is that simply counting up from the time it is called and then starting to count again when it starts detecting a high signal? My thought would be to take just a single measurement of utime.ticks_us() immediately after sending a high pulse from the trigger. That would find some value of microseconds and store it as a value, then we wait until we get a high pulse and do nothing until then. When we do get a high pulse we mark the beginning of that high pulse in terms of microseconds, and then we can find the difference between the initial out pulse and the return pulse, multiply by the speed of sound in cm/s, and then divide by 2.
- Send out a pulse
- While we are not detecting a received pulse, keep updating a number. Ex:
- 1001, 1002, 1003, 1004, 1005
- While we are receiving a pulse, update a different number. Ex:
- 1006, 1007, 1008
- Subtract the first from the second:
- 1008 - 1005 = 3
- This just measures the number of microseconds that we actually detect the pulse returning and not the time that it took for the pulse to bounce back.
Edit: Obviously I know that it does work, I'm just trying to piece together exactly how it works. Also, the signaloff and signalon are defined in the while loops, but then they are used later on again outside of those indents. Why does it allow this?