# Raspberry Pi Pico with PiicoDev VL53L1X failing to connect

**URL:** <https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885>\
**Category:** Core Electronics\
**Created:** [7 May 2024 07:14 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885 "2024-05-07T07:14:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JamesD](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jamesd/32/8448_2.png) [@JamesD](https://forum.core-electronics.com.au/u/JamesD)\
**Post date:** [7 May 2024 07:14 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/1 "2024-05-07T07:14:48Z")

</div>

Hello!

I’ve been having some issues getting the PiicoDev VL53L1X laser distance sensor to connect to a Pi Pico and was wondering if I could get any input on something I might have overlooked or am obviously getting wrong:

- The VL53L1X is connected to I2C0 on the Pico, GP20 for SDA and GP21 for SCL, 3.3v for 3.3v and ground for ground.
- I have the latest PiicoDev python files and no upload/issues with other scripts running on the Pico itself
- Using the following code, I can see the device at address 0x41 and the device power LED is lit:

```auto
from PiicoDev_VL53L1X import PiicoDev_VL53L1X
from time import sleep
from machine import I2C

i2c = machine.I2C(0, scl=21, sda=20, freq=400000)
output = i2c.scan()
print("I2C Scan addresses:")
print(output)

```

Returns:

```auto
I2C Scan addresses:
[41]

```

But when passing this config into the constructor for the VL53L1X (either with the specified address or just leaving the constructor blank), I receive the following:

```auto
from PiicoDev_VL53L1X import PiicoDev_VL53L1X
from time import sleep
from machine import I2C

i2c = machine.I2C(0, scl=21, sda=20, freq=400000)
output = i2c.scan()
print("I2C Scan addresses:")
print(output)
print("Starting Distance Sensor:")
distSensor = PiicoDev_VL53L1X(0, 400000, 20, 21, 0x41)

while True:
    dist = distSensor.read() # read the distance in millimetres
    print(str(dist) + " mm") # convert the number to a string and print
    sleep(0.1)

```

```auto
Starting Distance Sensor:
Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
  File "PiicoDev_VL53L1X.py", line 112, in __init__
  File "PiicoDev_VL53L1X.py", line 136, in reset
  File "PiicoDev_VL53L1X.py", line 125, in writeReg
OSError: [Errno 5] EIO

```

I’ve checked for any connectivity issues with a basic multimeter and everything seems to be fine, even though the error showing seems to point at that. I’m using a 4pin cable to connect the VL53L1X back to the pico with a cable distance of \<76cm but am unsure if this is an issue.

 ![distance_sensor](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/2/266eff4ee7999bc964fa3fd682fd41167b7e0e1c.jpeg)

Any ideas on anything I may have missed?  
Thanks for any advice in advance!

James.

---

<div class="post-metadata">

**Author:** ![Liam120347](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/liam120347/32/5003_2.png) [@Liam120347](https://forum.core-electronics.com.au/u/Liam120347)\
**Post date:** [7 May 2024 09:41 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/2 "2024-05-07T09:41:30Z")

</div>

Hi James,

Welcome to the forum!

From memory the Piicodev initialisation requires the sda and scl objects to be pins instead of ints i.e. Pin(x) not x

---

<div class="post-metadata">

**Author:** ![JamesD](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jamesd/32/8448_2.png) [@JamesD](https://forum.core-electronics.com.au/u/JamesD)\
**Post date:** [7 May 2024 12:28 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/3 "2024-05-07T12:28:29Z")

</div>

Thanks for the reply Liam! Yep - I’m a moron. Changing those values to Pin(x) after importing Pin instead of straight ints and I’m now getting data from the sensor successfully. I must’ve missed this - since I switched the numbers between SDA and SCL as part of debugging and it returned a “Invalid SCL pin” error instead, so assumed the int numbers worked. Whoops.

Appreciate the help!

---

<div class="post-metadata">

**Author:** ![Michael](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/michael/32/3545_2.png) [@Michael](https://forum.core-electronics.com.au/u/Michael)\
**Post date:** [8 May 2024 01:16 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/4 "2024-05-08T01:16:33Z")

</div>

For those playing along at home, the solution was to update the initialisation to be:

```auto
from PiicoDev_VL53L1X import PiicoDev_VL53L1X
from time import sleep
from machine import I2C, Pin # Import Pin

i2c = machine.I2C(0, scl=Pin(21), sda=Pin(20), freq=400000) # Use Pin objects to initialise

```

---

<div class="post-metadata">

**Author:** ![Trevor277988](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Trevor277988](https://forum.core-electronics.com.au/u/Trevor277988)\
**Post date:** [20 August 2024 13:49 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/5 "2024-08-20T13:49:52Z")

</div>

Thankyou… this is the best post I’ve found on this subject, but I (a newbie) am still getting errors.  
My wiring is exactly the same as shown in JamesD pic above.

What is NOT clear, is how the rest of the code (after the initialisation) should look… specificllly, is this line:  
distSensor = PiicoDev\_VL53L1X(0, 400000, 20, 21, 0x41)  
still good… or do the 20, 21 args also need to be Pin(20), Pin(21) ?  
I’ve tried both… and am still getting Errno5 EIO.  
My entire code & output is:

```auto

from PiicoDev_VL53L1X import PiicoDev_VL53L1X
from time import sleep
from machine import I2C, Pin # Import Pin

i2c = machine.I2C(0, scl=Pin(21), sda=Pin(20), freq=400000)
output = i2c.scan()
print("I2C Scan addresses:")
print(output)
print("Starting Distance Sensor:")
distSensor = PiicoDev_VL53L1X(0, 400000, 20, 21, 0x41)

while True:
    dist = distSensor.read() # read the distance in millimetres
    print(str(dist) + " mm") # convert the number to a string and print
    sleep(0.2)

```

I2C Scan addresses:  
[41]  
Starting Distance Sensor:  
Using supplied bus, sda, and scl to create machine.I2C() with freq: 400000 Hz  
Traceback (most recent call last):  
File “”, line 10, in   
File “PiicoDev\_VL53L1X.py”, line 112, in **init**  
File “PiicoDev\_VL53L1X.py”, line 136, in reset  
File “PiicoDev\_VL53L1X.py”, line 125, in writeReg  
OSError: [Errno 5] EIO

---

<div class="post-metadata">

**Author:** ![JamesD](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jamesd/32/8448_2.png) [@JamesD](https://forum.core-electronics.com.au/u/JamesD)\
**Post date:** [20 August 2024 14:14 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/6 "2024-08-20T14:14:04Z")

</div>

Hi Trevor,

Looking back at my code, the distance sensor is part of the PiicoDev library which is what requires the pins to be defined as Pins, so my line of code that you’re referring to is:

```auto
print("Starting Distance Sensor:")
distSensor = PiicoDev_VL53L1X(0, 400000, Pin(20), Pin(21))

```

Which should hopefully then let you call `distSensor.read()`. I no longer make use of `i2c.scan()` and removed those lines, leaving just the `distSensor` definition.

Let us know how you go!

---

<div class="post-metadata">

**Author:** ![Trevor277988](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Trevor277988](https://forum.core-electronics.com.au/u/Trevor277988)\
**Post date:** [20 August 2024 16:22 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/7 "2024-08-20T16:22:22Z")

</div>

Still getting EIO error.  
To be precise, my earlier post implied I had wired axactly as shown… not sure if it matters, but I have my Pico soldered onto an expansion board, with wires connecting to the sensor board (soldered). Also, my board is actually a Pico W… but I don’t believe that matters… same pinout - right?

I note you also dropped the 0x41 arg… which I believe was wrong anyway… 0x29 is decimal 41. I’v tried both with and without this arg… makes no difference.

Could it be my wires need to be soldered directly to the Pico? I read lots of posts suggesting the EIO error may be related to poor connections?

BTW… I resorted to these wires as the tiny I2C cables I bought were complete failures… could not even power the onboard LED, with the 3.3V dropping to 1.8V on the sensor. That was never going to work!. Whatever… I am still stuck with no data

Thankyou for your help…

---

<div class="post-metadata">

**Author:** ![Trevor277988](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Trevor277988](https://forum.core-electronics.com.au/u/Trevor277988)\
**Post date:** [21 August 2024 00:17 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/8 "2024-08-21T00:17:15Z")

</div>

Finally… I got this working!  
The solution was to wind back to an earlier build of MicroPython… which I found referenced in this post: [Raspberry Pi Pico W I2C/Piicodev Bug [Solved]](https://forum.core-electronics.com.au/t/raspberry-pi-pico-w-i2c-piicodev-bug-solved/15206)

Worked first time… then didn’t, unsure why, but I nuked the flash, reinstalled 20220831 uf2, and it’s been just fine.

So it seems that whatever broke after that 2022 build has stayed broke…  
Some of the technical details in that other thread go over my head…but it does surprise me that this apparently has not been fixed in two years!

Anyway… I have something working, now need to test it, and maybe tweak FOV parameters. I saw a post somewhere that talked about that…

Cheers,

---

<div class="post-metadata">

**Author:** ![JamesD](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jamesd/32/8448_2.png) [@JamesD](https://forum.core-electronics.com.au/u/JamesD)\
**Post date:** [21 August 2024 01:30 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/9 "2024-08-21T01:30:24Z")

</div>

Sorry my response wasn’t helpful, but glad you figured it out and thank you for coming back to the thread to post what worked! I’ll have to also keep that build for the Pico firmware in mind, appreciate that.

Good luck!

---

<div class="post-metadata">

**Author:** ![Trevor277988](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Trevor277988](https://forum.core-electronics.com.au/u/Trevor277988)\
**Post date:** [21 August 2024 02:10 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/10 "2024-08-21T02:10:34Z")

</div>

Don’t be so hard on yourself… it was helpful!

I am quite new to all of this stuff… many years in IT, but not with microcontrollers etc. I am still finding my feet. Arduino, Rasberry… micropython… all new, so I really appreciate folk like you who responded so quickly. Many thanks.

---

<div class="post-metadata">

**Author:** ![Trevor277988](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Trevor277988](https://forum.core-electronics.com.au/u/Trevor277988)\
**Post date:** [21 August 2024 11:08 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/11 "2024-08-21T11:08:40Z")

</div>

I clearly have more to learn about the MicroPython ecosystem…

Code runs… great. But somehow, I sometimes get things messed up… by which I mean, even if I power off, restart Thonny, I get the old error msg again - until and unless I reload the August 2022 uf2 file, ie, basically wipe everything and start over.

Can you please point me to somewhere I can learn what the heck is happening?

I can try to replicate if it helps, it has happened few times now, it is not clear to me what sequence of things cause the fail mode. Grrr… Suffice to say, I know I have the “good” build of micropython on the SRAM, but hitting the Thonny red Stop button, and rerunning the known-to-working code… breaks But not always. Hrumph.

Sigh…

---

<div class="post-metadata">

**Author:** ![Liam120347](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/liam120347/32/5003_2.png) [@Liam120347](https://forum.core-electronics.com.au/u/Liam120347)\
**Post date:** [21 August 2024 11:37 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/12 "2024-08-21T11:37:17Z")

</div>

Hi Trevor,

Getting everything setup now, would it be possible to send through the code and the name of the uf2 file you are using?

---

<div class="post-metadata">

**Author:** ![Liam120347](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/liam120347/32/5003_2.png) [@Liam120347](https://forum.core-electronics.com.au/u/Liam120347)\
**Post date:** [21 August 2024 11:44 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/13 "2024-08-21T11:44:15Z")

</div>

Hi Trevor,

I’ve copied everything straight from the guide with success - and with the latest version of micropython.

Would it be possible to send through a photo of your setup?

Source:

> **[PiicoDev Distance Sensor VL53L1X - Raspberry Pi Pico Guide - Tutorial Australia](https://core-electronics.com.au/guides/piicodev-distance-sensor-vl53l1x-raspberry-pi-pico-guide/)**
>
> Introduction
> This guide will help you read distance data in millimeters from your PiicoDev® Distance Sensor and a Raspberry Pi Pico
> To follow along, it's best to have:
> 
> A Raspberry Pi Pico with pins soldered (pointing down)
> A PiicoDev Distance Sensor...

Proof:

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/7/79fba8794eeac8d5fe504418c9002d4e04c3e762.png)

---

<div class="post-metadata">

**Author:** ![James46717](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@James46717](https://forum.core-electronics.com.au/u/James46717)\
**Post date:** [21 August 2024 23:52 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/14 "2024-08-21T23:52:46Z")

</div>

There is a definite problem here.  
I loaded Micropython version 1.23.0 through Thonny, downloaded the library and test files from Core Electronics PiicoDev VL53L1X web page, and it all ran ok.

But …  
When I tried to use the I2C alternate ports (pins 20 & 21) it failed. After trying a few things to try and find out why I did not come to any conclusive conclusion, after a couple of hours.

So I went back to using the standard pins 8 & 9, that now failed with the same error.  
I then removed all files and loaded the library and test file again. Still failed.

Went back to square one and reloaded the Micropython version and it now all works again.  
This would be very confusing to any one.  
My investigation will continue …

Regards  
Jim

---

<div class="post-metadata">

**Author:** ![James46717](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@James46717](https://forum.core-electronics.com.au/u/James46717)\
**Post date:** [22 August 2024 01:11 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/15 "2024-08-22T01:11:20Z")

</div>

OK … so an old ‘gotcha’ got me.  
I restarted the test from scratch with a reload of micropython. I used the following code to change the I2C pins. A slight modification of `main.py` from the Core Electronics web site. Adds machine library imports and parameter definitions.

```auto
from PiicoDev_VL53L1X import PiicoDev_VL53L1X
from time import sleep
from machine import I2C, Pin

bus=0
freq=400000
sda=Pin(8)
scl=Pin(9)

#distSensor = PiicoDev_VL53L1X()
distSensor = PiicoDev_VL53L1X(bus=bus, scl=scl, sda=sda, freq=freq)

while True:
    dist = distSensor.read() # read the distance in millimetres
    print(str(dist) + " mm") # convert the number to a string and print
    sleep(1)

```

If I changed the pin numbers and ran the code again, it did NOT work no matter what pins I swapped to. If an error occurred the Pico no longer worked even with the right pin numbers.  
ie. right pins - works, change to wrong pins - fails, change back to right pins - fails

**But … after a power cycle of the Pico it works. If you change the pin numbers in the code, power cycle the Pico.**

Note: because the code is named, `main.py` it runs when the Pico powers up. This confuses the issue because once an error occurs it does not work anymore. My policy is to only change the name to `main.py` when the code has been adequately tested and works. If for any reason the sensor does not work and the code errors the Pico will need a power cycle to work again. it might only need a reset but the Raspberry Pi Pico boards do not come with a reset button Frustrating !!

Regards  
Jim

PS I dont know if this will fix the problem @Trevor277988 is experiencing.

---

<div class="post-metadata">

**Author:** ![Trevor277988](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Trevor277988](https://forum.core-electronics.com.au/u/Trevor277988)\
**Post date:** [22 August 2024 15:04 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/16 "2024-08-22T15:04:30Z")

</div>

Thanks… my internet was out most of Thursday… and it’s now 12:30 AM Friday. I WILL send code and photos… tomorrow! I notice James46717 has added some useful info…I’ll digest this all after I have got some sleep…

PS Having a ball learning Python, PicoDev… while also building my hardware where this little gadget will be installed. Lots to learn…

Cheers,

---

<div class="post-metadata">

**Author:** ![James46717](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@James46717](https://forum.core-electronics.com.au/u/James46717)\
**Post date:** [22 August 2024 22:44 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/17 "2024-08-22T22:44:18Z")

</div>

Without going into the depths of Micropython my guess is when the program first sets any pin definitions it can only be changed with a reset or power cycle. It would be how the RP-2040 is programmed to determine the function of its pins.

Normally you would not swap pin definitions without a reset. Speaking of reset …  
Taking `pin 30` of the Pico low provides a hardware reset (labelled `Run`); using `machine.reset()` in software does the same thing.

I used the software reset in a project to reboot the processor at midnight, because sometimes the program crashed during the day. The project turned on a water valve for an hour in the morning so it was not a very active project spending most of the time asleep.

Regards  
Jim

---

<div class="post-metadata">

**Author:** ![Trevor277988](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Trevor277988](https://forum.core-electronics.com.au/u/Trevor277988)\
**Post date:** [23 August 2024 04:51 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/18 "2024-08-23T04:51:51Z")

</div>

OK, pics attached.

The problem seems to be solved… not sure if it is because I’ve renamed main.py to test.py… anyway, as the pics show, now working on both old and new versions of microPython, using pins 20&21. I have not tried changing pins (again) now… I have more pressing things to do!

Jim’s summary above seems to cover this. And yes… confusing. Specially for new players who have no clue what they are doing !!

Thanks all for helping out.  
T

 ![VL53L1X](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/a/a8ab160a7a2373df52bb55c035bafe71dd2f5a4e.jpeg)  
 ![Screenshot 2024-08-23 135858](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/f/f121ccfea5057cc53ce1f6809df53330d0e94cf6.png)  
 ![Screenshot 2024-08-23 140537](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/3/3cc4000e302c2884de0e143b75e76823686069e8.png)

---

<div class="post-metadata">

**Author:** ![Jack](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jack/32/9287_2.png) [@Jack](https://forum.core-electronics.com.au/u/Jack)\
**Post date:** [23 August 2024 06:09 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/19 "2024-08-23T06:09:19Z")

</div>

Hi Trevor,

I’m not the biggest fan of the wiring here. While the board is getting powered it doesn’t look like there is much solder on those pads. You could possibly be losing connection with the Pico while running the script.

---

<div class="post-metadata">

**Author:** ![Liam](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/liam/32/3562_2.png) [@Liam](https://forum.core-electronics.com.au/u/Liam)\
**Post date:** [23 August 2024 06:21 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885/20 "2024-08-23T06:21:27Z")

</div>

Hi Trevor,

> [@Trevor277988](#):
>
> BTW… I resorted to these wires as the tiny I2C cables I bought were complete failures… could not even power the onboard LED, with the 3.3V dropping to 1.8V on the sensor. That was never going to work!. Whatever… I am still stuck with no data

That’s definitely concerning! Each of the PiicoDev boards are tested post-manufacture so hardware errors should be rarer, would it be possible to send through some photos?  
Unfortunately, the style of connector doesn’t have a satisfying click like one with a latch to prove its engaged. The front of the connector and flange of the cable should be around 1mm apart.

And thanks heaps for jumping in and helping @James46717, @JamesD and @Liam120347

We’re keen to see what you make!  
Liam

[Next page](https://forum.core-electronics.com.au/t/raspberry-pi-pico-with-piicodev-vl53l1x-failing-to-connect/19885.md?page=2)
