My wall avoiding robot kept stopping. The code was short, it was MicroPython on a Raspberry Pi Pico W, and it always hung in the same sort of place: a sleep.
This happened back in November 2023. In A month of learning the hard way I mentioned that my damaged Pico W made timing functions hang. This is a deeper dive into that timing problem and how it confused me for a few hours, written up from my lab notes, including the very convincing wrong answer I reached along the way.

This is one of the robots I was building for my local Coder Dojo, with a pair of RCWL-1601 ultrasonic distance sensors on the front.
Contents
- The symptom
- Can I reproduce it small?
- Closing in
- It gets weird
- The wrong conclusion
- The real answer
- What I’d do differently
The symptom
- Importing
timewas fine. - Running my wall avoiding code broke.
- Test scripts would eventually stop too.
- It always hung in a sleep.
I did have a suspicion early on: I’d had a wiring accident, and I might have damaged the Pico. I noted it, thought “I could try another Pico”, and carried on debugging the code.
Can I reproduce it small?
My test pattern was a loop which counts, so I could see how far it got before it died:
loop_count = 0
while True:
print("Loop count is:", loop_count)
sleep(0.1)
# The thing under test goes here
loop_count += 1
First I tried the ingredients of a distance reading on their own. I stopped each of these myself after a few hundred loops, as they seemed happy:
| Under test | Loops run |
|---|---|
sleep and sleep_us(5) (with a 0.5s sleep) |
622 |
time_pulse_us on one pin |
420 |
A trigger pulse, then time_pulse_us |
800 |
time_pulse_us on two pins |
699 |
Then I used my actual distance sensor library, and it died after 11 loops. Changing from import time to from time import sleep got 19. Not that.
To rule out the import, I pasted the whole class into the test file:
from machine import Pin, time_pulse_us
from time import sleep_us, sleep
class RCWL1601:
def __init__(self, trigger_pin, echo_pin):
self.trigger_pin = Pin(trigger_pin, Pin.OUT)
self.echo_pin = Pin(echo_pin, Pin.IN)
self.trigger_pin.value(0)
us_to_cm = 0.0343 / 2
def distance_cm(self):
self.trigger_pin.value(0) # Stabilise the sensor
sleep_us(5)
# Send a pulse
self.trigger_pin.value(1)
sleep_us(10)
self.trigger_pin.value(0)
# Measure the time it takes to get a pulse back
return_time = time_pulse_us(self.echo_pin, 1, 3000)
# Now we have the time, we can calculate the distance
return return_time * self.us_to_cm
left_sensor = RCWL1601(4, 5)
loop_count = 0
while True:
print("Loop count is:", loop_count)
print("Sleeping 0.1 seconds")
sleep(0.1)
print("Distance:", left_sensor.distance_cm())
loop_count += 1
It stopped after 8 loops. Flattening the class into a plain function got 4. Now I had the failure in one file, and could start removing things.
Closing in
The first two lines of distance_cm hold the trigger pin low for 5µs, to stabilise the sensor before sending the pulse. I took them out:
def distance_cm():
trigger_pin.value(1)
sleep_us(10)
trigger_pin.value(0)
return_time = time_pulse_us(echo_pin, 1, 3000)
return return_time * us_to_cm
Suddenly it ran for thousands of loops. A clue! Was it the quick flip of the pin, or the sleep_us(5)?
I removed time_pulse_us and kept the trigger code: dead after 2 loops. I removed the pin changes and kept only the two sleeps: dead after 3.
It gets weird
Here is where it stopped making sense. With the sleeps written directly in the loop, it ran for many loops:
sleep(0.1)
sleep_us(5)
sleep_us(10)
Put the same two sleeps in a function, and it died after 3:
from time import sleep_us, sleep
def trigger():
sleep_us(5)
sleep_us(10)
loop_count = 0
while True:
print("Loop count is:", loop_count)
print("Sleeping 0.1 seconds")
sleep(0.1)
trigger()
loop_count += 1
No pins, no sensor, no library - just sleeping. Changing what the function did gave:
| Inside the function | Result |
|---|---|
sleep_us(5) then sleep_us(10) |
Hangs after 3 loops |
sleep_us(10) twice |
Hangs after 3 or 4 loops |
sleep_us(10) once |
Fine |
| Larger sleeps, around 50µs | Seemed OK |
The wrong conclusion
I wrote down: “5µs sleeps seem to make MicroPython unstable”.
My own table disagrees, as two 10µs sleeps also hung it. Going back to the sensor code, lengthening the stabilise delay to 20µs still hung. Adding 20µs after the pulse in made it stable.
I had a workaround of sorts, but no explanation.
The real answer
The Pico W was damaged.
I had put the motor board’s power wires back the wrong way round, which is a story I told in that earlier post.
Afterwards, the pins connected to the motor board would no longer output a signal. The timing problem was another symptom of the same damage. The same script ran happily on my other Picos - only this one misbehaved.
The sensors weren’t even on the dead pins: the motors used GP0 to GP3, and the distance sensors GP4 to GP7. The last failing scripts didn’t touch a pin at all. I don’t know why this damage showed up as short sleeps hanging, but that’s where it showed up.
With a healthy board, there was nothing to fix. The distance sensor code I went on to use is in Coder Dojo Robot Wall Avoiding, along with the robot build and wiring.
What I’d do differently

An early model of the robot, wired on a breadboard.
Swap the board early. I suspected it at the start. Running the test on a second Pico would have been the quickest experiment of all.
I frequently use breadboards for wiring, and still would. They do mean taking great care to check the wiring, and I’ve listed what I learned about that in A month of learning the hard way.
I later moved these robots to the Cytron Maker Pi RP2040, which has polarised Grove connectors for sensors.
If your code is doing something which makes no sense, and the hardware has had a fright, try the same code on another board before you believe your theory.