Ajouter notes/ VL6180X - catpeur de distance - code.c
This commit is contained in:
53
notes/ VL6180X - catpeur de distance - code.c
Normal file
53
notes/ VL6180X - catpeur de distance - code.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_VL6180X.h>
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user