Page 1 of 1

Interruption function for vl6180x didnot work for esp8266 nodemcu v3?

Posted: Mon Feb 26, 2024 10:59 am
by engkhaledeisa
Hi…how are you ….final question and I am sorry for bothering you but I need your advice….the interruption function of proximity sensor vl6180x didnot work on esp8266 nodemcu v3 ….I connect the sensor pins (vin -> 3.3v , gnd -> gnd , gpio -> D6 , sda ->D2 , scl ->D1 )…..but the results are nonstop interruptions(255) but it measure the distance to the sensor….

So what do you think should I change the connection pins or change a specific registery in your library

That is the test code so what do you think ?


```

Code: Select all

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <VL6180X_WE.h>
#define VL6180X_ADDRESS 0x29
#define LED 2
#define LEDNR 16
#define TOF_INT 12
#define SCL_PIN 5
#define SDA_PIN 4

VL6180xIdentification identification;
VL6180x TOFsensor(VL6180X_ADDRESS);

Adafruit_NeoPixel pixels(LEDNR, LED, NEO_GRB + NEO_KHZ800);
int i = 0;

volatile bool interruptReceived = false;

ICACHE_RAM_ATTR void handleInterrupt() {
  interruptReceived = true;
}



void setup() {
  Serial.begin(115200);
  Wire.begin(SDA_PIN, SCL_PIN);  //Initialize I2C for VL6180x (TOF Sensor)
  pixels.begin();                // Initialize NeoPixel ring

  pixels.clear();
  pixels.show();

  pinMode(TOF_INT, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(TOF_INT), handleInterrupt, FALLING);

  delay(1000);

  while(TOFsensor.VL6180xInit() == VL6180x_FAILURE_RESET){
    Serial.println("FAILED TO INITALIZE"); //Initialize device and check for errors

  }
  TOFsensor.VL6180xDefautSettings(); //Load default settings to get started.
  delay(1000);
    TOFsensor.VL6180xSetDistInt(10 ,10); 
    TOFsensor.getDistanceContinously();
}

void loop() {
  if (interruptReceived) {
    Serial.println("Intrerupt received");
    Serial.println(TOFsensor.getLastDistanceFromHistory());
    for (int i = 0; i < LEDNR; i++) {
      pixels.setPixelColor(i, pixels.Color(50, 0, 0));
      pixels.show();
      delay(100);
    }
    Serial.println("Light Set");
    interruptReceived = false;
    TOFsensor.VL6180xClearInterrupt();
    Serial.println("Intrerupt cleared");
  }

  pixels.clear();
  pixels.show();
    
}
```

Note:(from VL6180X_WE.h library I get the function names and specific parts that contian getregister and setregister values so you can have a look at the register values so maybe you can suggest I can modify the register value so the intteruption function can work properly
VL6180X_WE.h

```

Code: Select all

#define VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO         0x0014
#define VL6180X_SYSTEM_INTERRUPT_CLEAR               0x0015
#define VL6180X_RESULT_INTERRUPT_STATUS_GPIO         0x004F
#define VL6180X_I2C_SLAVE_DEVICE_ADDRESS             0x0212

  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x00); // Configures interrupt on ‘New Sample Ready threshold event’ 

uint8_t VL6180x::getDistance()
{
  VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); //Start Single shot mode (=0x01)  //ACHTUNG!!!!!!!!!!!!! gewechselt von 0x01
  delay(10);
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);
  return VL6180x_getRegister(VL6180X_RESULT_RANGE_VAL);
  //    return distance;
}
float VL6180x::getAmbientLight(vl6180x_als_gain VL6180X_ALS_GAIN)
{
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);



void VL6180x::VL6180xSetDistInt(uint8_t lowThres, uint8_t highThres){
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x03);

void VL6180x::VL6180xSetALSInt(vl6180x_als_gain VL6180X_ALS_GAIN, uint16_t lowThres, uint16_t highThres){
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x18);

uint8_t VL6180x::getDistanceContinously()
{
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);

float VL6180x::getAmbientLightContinously(vl6180x_als_gain VL6180X_ALS_GAIN)
{
 VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);


void VL6180x::VL6180xClearInterrupt(void){
    VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);
}

uint8_t VL6180x::changeAddress(uint8_t old_address, uint8_t new_address){
  
  //NOTICE:  IT APPEARS THAT CHANGING THE ADDRESS IS NOT STORED IN NON-VOLATILE MEMORY
  // POWER CYCLING THE DEVICE REVERTS ADDRESS BACK TO 0X29
 
  if( old_address == new_address) return old_address;
  if( new_address > 127) return old_address;
   
   VL6180x_setRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS, new_address);
   _i2caddress = new_address;


uint8_t VL6180x::VL6180x_getRegister(uint16_t registerAddr)
{
  uint8_t data;

  Wire.beginTransmission( _i2caddress ); // Address set on class instantiation
  Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address
  Wire.write(registerAddr & 0xFF); //LSB of register address
  Wire.endTransmission(false); //Send address and register address bytes
  Wire.requestFrom( _i2caddress , 1);
  data = Wire.read(); //Read Data from selected register

  return data;
}


uint16_t VL6180x::VL6180x_getRegister16bit(uint16_t registerAddr)
{
  uint8_t data_low;
  uint8_t data_high;
  uint16_t data;

  Wire.beginTransmission( _i2caddress ); // Address set on class instantiation
  Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address
  Wire.write(registerAddr & 0xFF); //LSB of register address
  Wire.endTransmission(false); //Send address and register address bytes

  Wire.requestFrom( _i2caddress, 2);
  data_high = Wire.read(); //Read Data from selected register
  data_low = Wire.read(); //Read Data from selected register
  data = (data_high << 8)|data_low;

  return data;
}


void VL6180x::VL6180x_setRegister(uint16_t registerAddr, uint8_t data)
{
  Wire.beginTransmission( _i2caddress ); // Address set on class instantiation
  Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address
  Wire.write(registerAddr & 0xFF); //LSB of register address
  Wire.write(data); // Data/setting to be sent to device.
  Wire.endTransmission(); //Send address and register address bytes
}

void VL6180x::VL6180x_setRegister16bit(uint16_t registerAddr, uint16_t data)
{
  Wire.beginTransmission( _i2caddress ); // Address set on class instantiation
  Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address
  Wire.write(registerAddr & 0xFF); //LSB of register address
  uint8_t temp;
  temp = (data >> 8) & 0xff;
  Wire.write(temp); // Data/setting to be sent to device
  temp = data & 0xff;
  Wire.write(temp); // Data/setting to be sent to device
  Wire.endTransmission(); //Send address and register address bytes
}

void VL6180x::VL6180xDefautSettings(void){
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x00); // Configures interrupt on ‘New Sample Ready threshold event’ 


uint8_t VL6180x::getDistance()
{
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);


float VL6180x::getAmbientLight(vl6180x_als_gain VL6180X_ALS_GAIN)
{
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);



void VL6180x::VL6180xSetDistInt(uint8_t lowThres, uint8_t highThres){
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x01);


void VL6180x::VL6180xSetALSInt(vl6180x_als_gain VL6180X_ALS_GAIN, uint16_t lowThres, uint16_t highThres){
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x18);


uint8_t VL6180x::getDistanceContinously()
{
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);


float VL6180x::getAmbientLightContinously(vl6180x_als_gain VL6180X_ALS_GAIN)
{
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);

void VL6180x::VL6180xClearInterrupt(void){
  VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);
}
```