I'm working on a power-saving feature for my B-SoM project using a VL53L1 ToF distance sensor. My goal is to reduce power consumption by entering Stop Sleep mode every 5 seconds. The process involves taking a measurement, entering Stop Sleep for 5 seconds, clearing any pending interrupts, and checking the interrupt state before going back to sleep. Here's the current approach:
distanceSensor.begin();
distanceSensor.init();
distanceSensor.setInterruptPolarityLow();
distanceSensor.setDistanceModeLong();
distanceSensor.startRanging();
distanceSensor.setDistanceThreshold(50, 1000, 3);
const long SLEEP_TIMEOUT = 5000; // 5 seconds
long lastWakeTime = 0;
bool sleepFlag = true;
void loop() {
// Poll sensor every 5s and handle sleep
if (sleepFlag) {
if (distanceSensor.checkForDataReady()) {
distanceSensor.clearInterrupt(); // Clear before entering sleep
}
// Prepare for sleep
Log.info("Going to sleep...");
SystemSleepConfiguration config;
config.mode(SystemSleepMode::STOP)
.network(NETWORK_INTERFACE_CELLULAR)
.duration(5000ms);
SystemSleepResult result = System.sleep(config);
sleepFlag = true; // Set flag after waking up
}
// Handle interrupt events - this will wake the device
if (TOF_INTERRUPT_FLAG) {
setTemporaryLEDColor("g");
TOF_INTERRUPT_FLAG = false;
sleepFlag = false;
int distance = distanceSensor.getDistance();
byte rangeStatus = distanceSensor.getRangeStatus();
Log.info("Interrupt Distance: %d mm, Status: %d", distance, rangeStatus);
distanceSensor.startRanging(); // Restart ranging
distanceSensor.setDistanceThreshold(50, 1000, 3);
lastWakeTime = millis(); // Reset wake time after handling interrupt
delay(100); // Small delay for stability
}
}
Does this approach for Stop Sleep every 5 seconds with the B-SoM seem effective for power savings while maintaining sensor functionality? Would going back into Stop mode this frequently cause any issues? Any insights on improving this pattern, particularly with interrupts and power efficiency, would be appreciated!