Reset USB Devices on RaspberryPi Ubuntu

Hey everyone,

I ran into a small issue today on my RaspberryPi. Occasionally, when restarting scripts that utilised usb devices (lidar, arduino) the usb connections were locking up. This meant that when I tried to restart the scripts I wasn’t able to reconnect.

My initial solution was to simply restart the Pi each time it occurred. Unfortunately, this became pretty tedious when testing required a lot of relaunching.

In order to get around having to restart each time I used usbreset in a small script attached to my launch files. This allowed me to ensure that all usb devices were ready to go.

for id in $(lsusb | grep -oP 'ID \S+' | awk '{print $2}'); do
    echo "Resetting device $id"
    sudo usbreset "$id"
    sleep 1  # Add a small delay between resets
done

Note that you can also simply paste this into your shell. This was done on a RaspberryPi that has been imaged with Ubuntu, but I expect it will work on Raspbian as well.

Leave a comment