#include #include "driver/pcnt.h" #include #define SENSOR_TANK_SDA GPIO_NUM_16 /**< GPIO 16 - echo feedback of water sensor */ #define SENSOR_TANK_SCL GPIO_NUM_17 /**< GPIO 17 - trigger for water sensor */ #define OUTPUT_SENSOR 14 #define SENSOR_PLANT 17 VL53L0X tankSensor; void setup() { Serial.begin(115200); pinMode(OUTPUT_SENSOR, OUTPUT); tankSensor.setTimeout(500); digitalWrite(OUTPUT_SENSOR, HIGH); Serial.println("Nodemcu ESP32 Start done"); tankSensor.setTimeout(500); Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL, 100000UL /* 100kHz */); tankSensor.setTimeout(500); tankSensor.setBus(&Wire); delay(20); long start = millis(); bool distanceReady = false; while (start + 500 > millis()) { if (tankSensor.init()) { distanceReady = true; break; } else { delay(20); } } if (distanceReady) { Serial.println("Sensor init done"); tankSensor.setSignalRateLimit(0.1); // increase laser pulse periods (defaults are 14 and 10 PCLKs) tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18); tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14); tankSensor.setMeasurementTimingBudget(200000); } else { Serial.println("Sensor init failed"); } } void loop() { delay(500); if (!tankSensor.timeoutOccurred()) { uint16_t distance = tankSensor.readRangeSingleMillimeters(); Serial.print("Distance"); Serial.println(distance); } else { Serial.println("Timeout"); } }