Vl53l1x GPIO1 Interrupt

Hi there,
I’m trying to trigger an interrupt though the GPIO pin from this sensor to wake the atmega328p from deep sleep.
If you could give me some support with the library as I could not locate such implementation.
I tried SparkFun library which had similar, but yet not much help as there is no documentation.
Would greatly help if anyone can help me on this.
Btw, I’m using your module VL53L1X bought form Little Bird Electronics
Thanks!

Hello.

We do not have any specific code for using the GPIO pin like that, but you might look at ST’s datasheet and API User Manual for more information about how that might work. You can find links to more ST resources like that on the “Resources” tab of our product page for the board. Additionally, you might try posting to ST’s community site.

-Jon

Okay I managed to get some helpful pointers from ST community forum and was able to make this work.
Only thing I should point here is that if anyone using the Sparkfun library, make sure to set

setInterruptPolarityLow();

if your interrupt is on a LOW signal

attachInterrupt(0, wakeUp, LOW);

My test example code below;

#include <avr/sleep.h>
#include <Wire.h>
#include "SparkFun_VL53L1X.h"

#define SHUTDOWN_PIN 10
#define INTERRUPT_PIN 2

long heartBeatTime = 0, count = 0, pollingInterval = 0;;
int heartBeatLedState = LOW; 
int status;

SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);

void setup() 
{
  pinMode(INTERRUPT_PIN, INPUT_PULLUP);
  pinMode(SHUTDOWN_PIN, OUTPUT);
  digitalWrite(SHUTDOWN_PIN, HIGH);

  ADCSRA &= ~(1 << 7);

  Wire.begin();

  Serial.begin(9600);
  Serial.println(F("VL53L1X Qwiic Test"));

  if (distanceSensor.begin() == 0) //Begin returns 0 on a good init
  {
    Serial.println(F("Sensor online!"));
  }

  distanceSensor.setInterruptPolarityLow();
  distanceSensor.setDistanceModeLong();
  distanceSensor.setDistanceThreshold(50, 1000, 3);
  distanceSensor.startRanging(); 
}

void loop(void)
{
  if ((millis() - heartBeatTime) >= 1000) 
  {
    heartBeatTime = millis();

    if (heartBeatLedState == LOW) {
      heartBeatLedState = HIGH;
    } else {
      heartBeatLedState = LOW;
    }
  
    digitalWrite(LED_BUILTIN, heartBeatLedState);
  }

  if ((millis() - pollingInterval) >= 10) 
  {
    pollingInterval = millis();
    
    static uint16_t startMs = millis();
  
    // non-blocking check for data ready
    status = distanceSensor.checkForDataReady();
    if(status)
    {
      int distance = distanceSensor.getDistance();

      Serial.print(F("count: "));
      Serial.print(count);
      Serial.print(F(" distance: "));
      Serial.println(distance);

      distanceSensor.clearInterrupt();
      startMs = millis();
    }
    else if((uint16_t)(millis() - startMs) > 10000) //Goes to sleep if no object in range for 10 seconds
    {
      Serial.println(F("Entering CPU low power sleep."));

      distanceSensor.clearInterrupt();
      startMs = millis();

      Going_To_Sleep();

      Serial.println(count);
    }
  }
}

void Going_To_Sleep(){
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  MCUCR = bit (BODS) | bit (BODSE);
  MCUCR = bit (BODS);
  noInterrupts();
  attachInterrupt(0, wakeUp, LOW);
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println(F("I'm going to bed!"));
  Serial.flush();
  interrupts();
  sleep_cpu();

  Serial.println(F("I just now woke up!"));
  digitalWrite(LED_BUILTIN, HIGH);
}

void wakeUp(){
  count++;

  sleep_disable();
  detachInterrupt(0);
}
1 Like