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!