From 27b170e328f8be186f3da4b087c3605399f2b50b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Abonnel?= Date: Sun, 15 Feb 2026 17:09:00 +0000 Subject: [PATCH] Ajouter notes/ VL6180X - catpeur de distance - code.c --- notes/ VL6180X - catpeur de distance - code.c | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 notes/ VL6180X - catpeur de distance - code.c diff --git a/notes/ VL6180X - catpeur de distance - code.c b/notes/ VL6180X - catpeur de distance - code.c new file mode 100644 index 0000000..7310eab --- /dev/null +++ b/notes/ VL6180X - catpeur de distance - code.c @@ -0,0 +1,53 @@ +#include +#include + +Adafruit_VL6180X vl = Adafruit_VL6180X(); + +// I2C ESP32 (valeurs par défaut fréquentes) +static const int I2C_SDA = 21; +static const int I2C_SCL = 22; + +void setup() { + Serial.begin(115200); + delay(200); + + // Init I2C (ESP32 permet de choisir les pins) + Wire.begin(I2C_SDA, I2C_SCL); + Wire.setClock(400000); // 400 kHz + + Serial.println("Init VL6180X..."); + + if (!vl.begin()) { + Serial.println("Erreur: VL6180X non detecte sur le bus I2C."); + Serial.println("Verifier: cablage, adresse I2C, alimentation, pull-ups."); + while (true) delay(1000); + } + + Serial.println("VL6180X OK."); +} + +void loop() { + // Distance en mm + uint8_t range = vl.readRange(); + + // Code de statut (0 = OK) + uint8_t status = vl.readRangeStatus(); + + // ALS (luminosite). Paramètre = gain (ex: VL6180X_ALS_GAIN_5) + // Tu peux ajuster le gain suivant ton environnement. + float lux = vl.readLux(VL6180X_ALS_GAIN_5); + + if (status == VL6180X_ERROR_NONE) { + Serial.print("Distance: "); + Serial.print(range); + Serial.print(" mm"); + } else { + Serial.print("Distance: ERR status="); + Serial.print(status); + } + + Serial.print(" | Lux: "); + Serial.println(lux, 2); + + delay(200); +}