Question Regarding VL53L4CD i2C Errors, Address issue, xshut issue, ReadNonStop issue?

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
nh1628
 
Posts: 18
Joined: Thu Apr 27, 2023 12:09 pm

Question Regarding VL53L4CD i2C Errors, Address issue, xshut issue, ReadNonStop issue?

Post by nh1628 »

Hello,

Trying to see if I can get help with figuring out this error on the VL53L4CD sensor as I am using multiple sensors.

I'm not 100% sure if this is an address setting issue, or if it's an xshut issue as it seems like I am not reading the sensors properly. It seems like it gets one reading (not sure how much I buy that result), but then gives me an i2C issue where it says i2cWriteReadNonStop returned error -1. Was hoping to see if I could get help with figuring out what the error is and how to use the i2c properly.

I am using this library per Adafruit's website: https://github.com/adafruit/VL53L4CD/bl ... cd_class.h.


Currently I am doing this to set the address and I'm not sure if this is actually setting the address or not.

Code: Select all

//init sensors
void initSensor(VL53L4CD sensorX, uint8_t sensoradd)
{
  // Init sensor1
  //Set sensor address.
  sensorX.VL53L4CD_SetI2CAddress(sensoradd);
  // Configure VL53L4CD satellite component.
  sensorX.begin();
  // Switch off VL53L4CD satellite component.
  sensorX.VL53L4CD_Off();
  //Initialize VL53L4CD satellite component.
  sensorX.InitSensor();
  // Program the highest possible TimingBudget, without enabling the
  // low power mode. This should give the best accuracy
  sensorX.VL53L4CD_SetRangeTiming(200, 0);
  sensorX.VL53L4CD_SetOffset(-10);
  // Start Measurements
  sensorX.VL53L4CD_StartRanging();
}

But the error I am getting is this, which leads me to find that it is an issue with Wire.h and the i2c TwoWire function and it seems like there is a readnonstop error.

Code: Select all

Starting...
Sensor 1 is on
This is Sensor1 0.00mm
Sensor 2 is on
This is Sensor2 28.00mm
Secpp:499] requestFrom(): i2cWriteReadNonStop returned Error -1
[  5272][E][Wire.cpp:499] requestFrom(): i2cWriteReadNonStop returned Error -1


Here's my full code:

Code: Select all

/* Includes ------------------------------------------------------------------*/
#include <Arduino.h>
#include <Wire.h>
#include <vl53l4cd_class.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <WiFi.h>
//#include <ESPAsyncWebServer.h>

#define DEV_I2C Wire
#define SerialPort Serial
char report[64];

//address we will assign if dual sensor is present
uint8_t sensor1add = 0x30;
uint8_t sensor2add = 0x31;

// set the pins to shutdown
#define xshut1 A0
#define xshut2 A1
#define GFX_BL DF_GFX_BL

//this holds the measurement
VL53L4CD_Result_t results1;
VL53L4CD_Result_t results2;
float sensor1, sensor2;

//Components.
VL53L4CD sensor1_vl53l4cd_sat(&DEV_I2C, xshut1);
VL53L4CD sensor2_vl53l4cd_sat(&DEV_I2C, xshut2);

//init sensors
void initSensor(VL53L4CD sensorX, uint8_t sensoradd)
{
  // Init sensor1
  //Set sensor address.
  sensorX.VL53L4CD_SetI2CAddress(sensoradd);
  // Configure VL53L4CD satellite component.
  sensorX.begin();
  // Switch off VL53L4CD satellite component.
  sensorX.VL53L4CD_Off();
  //Initialize VL53L4CD satellite component.
  sensorX.InitSensor();
  // Program the highest possible TimingBudget, without enabling the
  // low power mode. This should give the best accuracy
  sensorX.VL53L4CD_SetRangeTiming(200, 0);
  sensorX.VL53L4CD_SetOffset(-10);
  // Start Measurements
  sensorX.VL53L4CD_StartRanging();
}

//getSensor1Reading
void getSensor1Reading(uint8_t status, uint8_t NewDataReady)
{
  digitalWrite(xshut1, HIGH);
  Serial.println("Sensor 1 is on");
  if ((!status) && (NewDataReady != 0)) {
    // (Mandatory) Clear HW interrupt to restart measurements
    sensor1_vl53l4cd_sat.VL53L4CD_ClearInterrupt();
    // Read measured distance. RangeStatus = 0 means valid data
    sensor1_vl53l4cd_sat.VL53L4CD_GetResult(&results1);
    snprintf(report, sizeof(report), "Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\r\n",
             results1.range_status,
             results1.distance_mm,
             results1.signal_per_spad_kcps);
    sensor1 = results1.distance_mm;
    Serial.println((String)"This is Sensor1 " + (float)sensor1 + (String)"mm");
  }
  digitalWrite(xshut1, LOW);
}

//getSensor2Reading
void getSensor2Reading(uint8_t status, uint8_t NewDataReady)
{
  digitalWrite(xshut2, HIGH);
  Serial.println("Sensor 2 is on");
  if ((!status) && (NewDataReady != 0)) {
    // (Mandatory) Clear HW interrupt to restart measurements
    sensor2_vl53l4cd_sat.VL53L4CD_ClearInterrupt();
    // Read measured distance. RangeStatus = 0 means valid data
    sensor2_vl53l4cd_sat.VL53L4CD_GetResult(&results2);
    snprintf(report, sizeof(report), "Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\r\n",
             results2.range_status,
             results2.distance_mm,
             results2.signal_per_spad_kcps);
    sensor2 = results2.distance_mm;
    Serial.println((String)"This is Sensor2 " + (float)sensor2 + (String)"mm");
  }
  digitalWrite(xshut2, LOW);
}

/* Setup ---------------------------------------------------------------------*/
void setup()
{
  // Initialize serial for output.
  SerialPort.begin(115200);
  SerialPort.println("Starting...");
  // Initialize I2C bus.
  DEV_I2C.begin();
  
  initSensor(sensor1_vl53l4cd_sat, sensor1add);
  initSensor(sensor2_vl53l4cd_sat, sensor2add);
}

void loop()
{
  uint8_t NewDataReady1 = 0;
  uint8_t NewDataReady2 = 0;
  uint8_t status1;
  uint8_t status2;
  do {
    status1 = sensor1_vl53l4cd_sat.VL53L4CD_CheckForDataReady(&NewDataReady1);
    status2 = sensor2_vl53l4cd_sat.VL53L4CD_CheckForDataReady(&NewDataReady2);
  } while (!NewDataReady1 || !NewDataReady2);
  getSensor1Reading(status1, NewDataReady1);
  getSensor2Reading(status2, NewDataReady2);
  
  delay(100);
}

When using 1x VL53L4CD with the helloworld example from the library it works flawlessly (even when both sensors are connected), so I don't think there is an issue with the i2C bus or SDA/SCL wires.
Thanks!

User avatar
nh1628
 
Posts: 18
Joined: Thu Apr 27, 2023 12:09 pm

Re: Question Regarding VL53L4CD i2C Errors, Address issue, xshut issue, ReadNonStop issue?

Post by nh1628 »

Is there a way that I should be wiring it differently if I am using the pins?
Based on this article, it seems like it is only using the stemma QT to pass it through, but is there a way to pass it through via wire pins? https://learn.adafruit.com/working-with ... te-address

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Question Regarding VL53L4CD i2C Errors, Address issue, xshut issue, ReadNonStop issue?

Post by adafruit_support_carter »

The STEMMA QT connector is just a convenience. More info here:
https://learn.adafruit.com/introducing- ... -stemma-qt

Using the STEMMA QT connector is not required.

The exact same pins (SCL, SDA, VIN, GND) are available on the header pins.
https://learn.adafruit.com/adafruit-vl5 ... or/pinouts

User avatar
robcranfill
 
Posts: 142
Joined: Wed Feb 13, 2013 4:14 pm

Re: Question Regarding VL53L4CD i2C Errors, Address issue, xshut issue, ReadNonStop issue?

Post by robcranfill »

nh1628 wrote: Wed May 31, 2023 6:27 pm Is there a way that I should be wiring it differently if I am using the pins?
Based on this article, it seems like it is only using the stemma QT to pass it through, but is there a way to pass it through via wire pins? https://learn.adafruit.com/working-with ... te-address
I'm a little confused about the whole

Code: Select all

i2c = board.I2C()
.vs.

Code: Select all

i2c = board.STEMMA_I2C()
issue, myself, but I do wonder if you are NOT using the Stemma connectors, do you have to provide pullup resistors for the I2C bus?

Now that I write that, I think the answer is "no" - the breakout board has pullups no matter which way you wire it. Maybe?

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Question Regarding VL53L4CD i2C Errors, Address issue, xshut issue, ReadNonStop issue?

Post by adafruit_support_carter »

Info on pull up resistors:
https://learn.adafruit.com/working-with ... -resistors
Adafruit breakouts will generally include the pull ups on the breakout.

Originally, setting up an I2C bus in CircuitPython code was more manual. The board module was imported to provide access to the pins. The busio module was imported to provide access to the I2C() interface. Then, a busio.I2C() instance was created using specified board.PINS:

Code: Select all

import board
import busio
i2c = busio.I2C(board.SCL, board.SDA)
This still works if you want to code that way. However, since most boards have a single set of I2C pins, called SCL and SDA, a convenience short cut was added:

Code: Select all

import board
i2c = board.I2C()
This is equivalent to the manual process above. But now there is no need to import busio.

Then came the STEMMA QT connectors. Also, some boards started providing multiple I2C ports on different pins - with the STEMMA QT pins possibly being different from the header pins. So the meaning of board.I2C() became confusing. Therefore, a new, more explicit shortcut was added:

Code: Select all

import board
i2c = board.STEMMA_I2C()
which will always be the I2C port on the STEMMA QT connector. On some boards (most actually), this will be the same as board.I2C(). The pinout page for a given board should provide these details.

The QT Py RP2040 is one example of a board where the header I2C is different from the STEMMA QT I2C:
https://learn.adafruit.com/adafruit-qt-py-2040/pinouts

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”