ATtiny-Compatible Library for Time of Flight Sensor

Hi everyone. I’ve been trying to get the Pololu VL53L0X carrier to work correctly with my ATtiny85. The problem with Pololu’s existing library is that it uses Wire, which isn’t ATtiny compatible. To get around this I tried replacing Wire with TinyWireM, a drop-in ATtiny compatible library. My library and code is attached below.

However, the ATtiny clearly isn’t reading the correct values. It floats around, and doesn’t seem to have any dependence upon how far away objects are from the sensor I bought. I know that the sensor works, because I hooked it up to an Arduino Uno to verify that it was functioning properly.

If someone could take a look at my code and see if there’s any more modifications that need to be made that would be awesome. Could it be a timing problem with i2c? Thanks!
VL53L0X-ATtiny-3.ino (449 Bytes)
TinyWireM.cpp (4.1 KB)
TinyWireM.h (3.8 KB)
USI_TWI_Master.cpp (13.3 KB)
VL53L0X.h (6.2 KB)
USI_TWI_Master.h (5.1 KB)
VL53L0X.cpp (29.6 KB)

Do you have access to a logic analyzer or oscilloscope to probe what is occuring on the data lines? What frequency are you running your ATtiny85 at?


I only have a really cheap oscilloscope. It appears that the data lines are indeed active. The photo of the uniform square wave below is the SCL line, and the other photo is the SDA line. I tried running the ATtiny85 at both 1MHz and 8MHz with neither of them working.

You might try comparing the signals between your Uno program and the ATtiny program to see if they are similar. Also, it is possible that you are running out of SRAM on the ATtiny. There is some information about determining the free SRAM in this tutorial on Adafruit’s site.


Did you end up getting this to work? I am trying with the ATtiny841 and no luck so far.

Unfortunately I never did :frowning: I think it’s due to a lack of “real” I2C on the Tiny

I messed around with the lv53l0x library files and the TinyWireM files. I also made some changes to allow it to work on attiny84. It works great on both the attiny85 and attiny84.
However, I ended up with issues having the jeelib loaded to use low power for the attiny’s. It takes up around 64% just with the lv53l0x library and TinyWireM.TinyWireM.h (3.0 KB)
<a class=“attachment” href="/uploads/default/originattiny84_VL53L0X_test.ino (682 Bytes)
VL53L0X.h (6.2 KB)
VL53L0X.cpp (30.9 KB)
al/2X/d/d8009e4f5284b30fa2f80e92e572fb487865cd6d.h">USI_TWI_Master.h (5.2 KB)
USI_TWI_Master.cpp (13.4 KB)
TinyWireM.cpp (3.6 KB)

1 Like

Looks like the upload didn’t work in the previous email for the USI_TWI_Master.h file:
USI_TWI_Master.h (5.2 KB)

Thanks rxpc your example helped me to get my Attiny84 work correctly with 1MHz internal clock I2C communication. I am still learning how to configure it to work with internal 8MHz clock and with external 4MHz XTAL.

I have tried to change C_CPU which actually is effective. However, with 1MHz internal clock I need to keep it F_CPU 8000000UL in order to work. With 8Mhz it is working but making errors around 1%.

With external XTAL I cannot get any I2C communication starting at all. I assume there must be set some USI registers, but cannot find suitable bits from Attiny84 specification USI chapter.

Any advice how to proceed or try to get this working?

Thanks, Jarkko