VL53L1X and AtTiny85 - Run library - help

Can you help me to run this library under AtTiny85

https://github.com/pololu/vl53l1x-arduino

Please read the forum guidelines to get some ideas of the information that we need in order to be of assistance.

Have you tried any of the example code that comes from the library?

What have you tried so far?
How are you reading out the distance, if at all?

Please post a schematic. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies. Photos of the wiring are often useful.

What core are you using to program the tiny85? I like the ATTinyCore core. The linked page includes installation instructions. With ARTTinyCore the default I2C pins are pin 0 is SDA and pin 2 is SCK.

ATTinyCore pins.

1 Like
// Select Wire library fitting to your platform
/*
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) \
    || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny25__) \
    || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) \
    || defined(__AVR_ATtiny2313__) || defined(__AVR_ATtiny4313__) \
    || defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__) \
    || defined( __AVR_ATtiny261__ ) || defined( __AVR_ATtiny461__ ) \
    || defined( __AVR_ATtiny861__ ) || defined(__AVR_ATtiny1634__)
#include <USIWire.h>
#else
#include <Wire.h>
#endif

// USIWire - doesn't work with VL53L1X library
*/

#include <Wire.h>
#include <VL53L1X.h>
#include <SoftwareSerial.h>

VL53L1X sensor;
SoftwareSerial mySerial (4, 3); // RX, TX

/*
 * AtTiny85  -  VL53L1X
 * --------     -------
 *   PB0     -   SDA
 *   PB2     -   SCL
 *   VCC     -   VCC
 *   GND     -   GND
 * 
 * 
 * AtTiny85  -  TTL-RS232
 * --------     ---------
 *   PB4     -   TX
 *   PB3     -   RX
 *   GND     -   GND
 */

void setup() {
  mySerial.begin(9600);
  mySerial.println();
  mySerial.println("Start...");
  Wire.begin();
  Wire.setClock(400000); // use 400 kHz I2C

  sensor.setTimeout(500);
  if (!sensor.init()) {
    mySerial.println("Failed to detect and initialize sensor!");
    while (200);
  }
  sensor.setDistanceMode(VL53L1X::Long);
  sensor.setMeasurementTimingBudget(50000);
  sensor.startContinuous(50);
  mySerial.println("OK...");
}

void loop() {
  mySerial.print(sensor.read());
  if (sensor.timeoutOccurred()) { mySerial.print(" TIMEOUT"); }

  mySerial.println();
}

I use elements like this:

  • AtTiny85
  • TOF400C
  • ATTinyCore

I tried libraries like:

  • USIWire
  • TintWireM
    Unfortunately, nothing worked.

P.S.
The above example I tested on Wemos D1 mini ESP8266 works fine!

The version of Wire.h that comes with the ATTinyCore core works fine. I just tested it with a VL53L0X and the Pololu VL53L0x library installed with the IDE library manger. From what I understand the VL53L0X is not a lot different from the VL53L1X. I use am I2C enabled LCD to read the distance instead of serial. I don't think that the code will directly work with a VL53L1X, but maybe of some use.

#include <Wire.h>
#include <VL53L0X.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h>

VL53L0X sensor;
hd44780_I2Cexp lcd;

//#define LONG_RANGE
// Uncomment ONE of these two lines to get
// - higher speed at the cost of lower accuracy OR
// - higher accuracy at the cost of lower speed
//#define HIGH_SPEED
#define HIGH_ACCURACY

void setup()
{
   Wire.begin();
   lcd.begin(16, 2);
   lcd.print("VL53 Demo");
   delay(3000);

   sensor.setTimeout(500);

   if (!sensor.init())
   {
      lcd.clear();
      lcd.print("sensor fail!");
      while (1) {}
   }
   else
   {
      lcd.clear();
      lcd.print("sensor up");
   }

#if defined LONG_RANGE
   // lower the return signal rate limit (default is 0.25 MCPS)
   sensor.setSignalRateLimit(0.1);
   // increase laser pulse periods (defaults are 14 and 10 PCLKs)
   sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
   sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
#endif

#if defined HIGH_SPEED
   // reduce timing budget to 20 ms (default is about 33 ms)
   sensor.setMeasurementTimingBudget(20000);
#elif defined HIGH_ACCURACY
   // increase timing budget to 200 ms
   sensor.setMeasurementTimingBudget(200000);
#endif
   lcd.clear();
   lcd.print("Distance ");
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      int distance = (sensor.readRangeSingleMillimeters());
      lcd.setCursor(10, 0);
      lcd.print("     ");  // overwrite old data
      lcd.setCursor(10, 0); // reset cursor
      lcd.print(distance); // print new data
      if (sensor.timeoutOccurred())
      {
         lcd.clear();
         lcd.print(" TIMEOUT");
      }
   }
}

1 Like

I changed the code under SoftwareSerial to see what's going on with the program.
He stops at Step #1
I read that the Wire library does not work on AtTiny85, but how does it work for you because you use ATTinyCore - where it supposedly changes itself and selects the library.
Well, now I have a problem because I don't know why it works for you, it stops at sensor.init() for me.

#include <Wire.h>
#include <VL53L0X.h>
#include <SoftwareSerial.h>

VL53L0X sensor;
SoftwareSerial mySerial (4, 3);


//#define LONG_RANGE
// Uncomment ONE of these two lines to get
// - higher speed at the cost of lower accuracy OR
// - higher accuracy at the cost of lower speed
//#define HIGH_SPEED
#define HIGH_ACCURACY

void setup(){
   mySerial.begin(9600);
   mySerial.println("Start...");
   Wire.begin();
   delay(100);

   sensor.setTimeout(500);
   mySerial.println("STEP #1");

   if (!sensor.init())
   {
      mySerial.println("STEP #2");
      mySerial.println("sensor fail!");
      //while (1) {}
   }
   else
   {
      mySerial.println("STEP #3");
      mySerial.println("sensor up");
   }

   mySerial.println("STEP #4");

#if defined LONG_RANGE
   // lower the return signal rate limit (default is 0.25 MCPS)
   sensor.setSignalRateLimit(0.1);
   // increase laser pulse periods (defaults are 14 and 10 PCLKs)
   sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
   sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
#endif

#if defined HIGH_SPEED
   // reduce timing budget to 20 ms (default is about 33 ms)
   sensor.setMeasurementTimingBudget(20000);
#elif defined HIGH_ACCURACY
   // increase timing budget to 200 ms
   sensor.setMeasurementTimingBudget(200000);
#endif
   //mySerial.println("Distance ");
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      int distance = (sensor.readRangeSingleMillimeters());
      mySerial.print("Distance ");
      mySerial.println(distance); // print new data
      if (sensor.timeoutOccurred())
      {
         mySerial.println(" TIMEOUT");
      }
   }
}

I added a USB to serial module and uploaded the latest code. Here is the output copied from serial monitor.

Start...
STEP #1
STEP #3
sensor up
STEP #4
Distance 149
Distance 91
Distance 59
Distance 196
Distance 203
Distance 109
Distance 325
Distance 329

There is a version of Wire that comes with the ATTinyCore core. You do not have to add any library, use Wire just like you would on an Uno.

Have you installed the ATTinyCore core? If not, what core do are you using?

1 Like

Maybe a problem with the I2C bus. Load and run this I2C scanner modified to work with software serial. The scanner finds my sensor at address 0x29. This will confirm the communication with the bus and the I2C address.

// I2C scanner by Nick Gammon.  Thanks Nick.
#include <SoftwareSerial.h>

SoftwareSerial mySerial(4, 3); // RX, TX
#include <Wire.h>

void setup()
{
   mySerial.begin (9600); //*****  make sure serial monitor baud matches *****
   mySerial.println ();
   mySerial.println ("I2C scanner. Scanning ...");
   byte count = 0;

   Wire.begin();
   for (byte i = 1; i < 120; i++)
   {
      Wire.beginTransmission (i);
      if (Wire.endTransmission () == 0)
      {
         mySerial.print ("Found address: ");
         mySerial.print (i, DEC);
         mySerial.print (" (0x");
         mySerial.print (i, HEX);
         mySerial.println (")");
         count++;
         delay (1);  // maybe unneeded?
      } // end of good response
   } // end of for loop
   mySerial.println ("Done.");
   mySerial.print ("Found ");
   mySerial.print (count, DEC);
   mySerial.println (" device(s).");
}  // end of setup

void loop() {}
1 Like

I tested the scanner on ESP8266 and it works fine.
But on AtTiny85 it shows no devices found.

So I have a problem with some configuration?

That points to a problem with the I2C bus. Is it wired tiny85 SCL (pin 2) to VL53 SCL and tiny85 SDA (pin 0) to VL53 SDA?

I2C scanner should see it either way, but what is the default I2C address of the VL53L1X? I don't know if it is the same as the VL53L0X.

What are you using to bootload and program the tiny85?

What is your clock rate and source set to?

1 Like

To check my compiler and setting, send me a scanner compiled on HEX - I will upload it via AVR Studio 4.1

For uploading, he used a 10pin ISP - and settings in the Arduino IDE 1.8.19 on
"Atmel AVR500"

I have AtTiny85 set to 8MHz and Serial to 9600, but when I set the timing to 1MHZ for tests, the Serial in the program was 9600 and on the monitor it was 74880 - that is, it did not recalculate during compilation - it puzzles me, I2C also has a problem to move because something is wrong in Arduino IDE or Core settings

I connected the SDA and SCL pins to PB0 and PB2 and tried to swap places if something went wrong by accident. That didn't help either

groundFungus thank you very much for your help. The program fired up and started. The problem was also with my programmer, which was connected to the board and AtTiny, which caused interference on the PB0 port.

I didn't think of that since my programmer requires removing the tiny85 from the circuit for programming.

Glad that it works now. Have fun!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.