Skip to content

Instantly share code, notes, and snippets.

@SimedruF
Created January 18, 2025 10:51
Show Gist options
  • Save SimedruF/61b2cfd7df8d7f0b49c870f6dcc248d9 to your computer and use it in GitHub Desktop.
Save SimedruF/61b2cfd7df8d7f0b49c870f6dcc248d9 to your computer and use it in GitHub Desktop.
ESP32 C3 supermini connected to VL6180
#include <Wire.h>
#include <Adafruit_VL6180X.h>
// Object for the sensor
Adafruit_VL6180X vl6180;
// Pin configuration
#define SDA_PIN 8 // Pin for SDA
#define SCL_PIN 9 // Pin for SCL
#define SHDN_PIN 10 // Pin for SHDN
void setup() {
Serial.begin(115200); // Initialize the serial monitor
// Configure SHDN pin
pinMode(SHDN_PIN, OUTPUT);
digitalWrite(SHDN_PIN, HIGH); // Turn on the sensor
// Initialize I²C
Wire.begin(SDA_PIN, SCL_PIN);
// Initialize the sensor
if (!vl6180.begin()) {
Serial.println("Error: VL6180 sensor not detected!");
while (1); // Stop execution if the sensor is not detected
}
Serial.println("VL6180 sensor successfully detected!");
}
void loop() {
// Read the distance in millimeters
uint8_t range = vl6180.readRange();
uint8_t status = vl6180.readRangeStatus();
// Check if the measurement is valid
if (status == VL6180X_ERROR_NONE) {
Serial.print("Measured distance: ");
Serial.print(range);
Serial.println(" mm");
} else {
Serial.print("Error reading distance: ");
Serial.println(status);
}
// Read ambient light in lux
float lux = vl6180.readLux(VL6180X_ALS_GAIN_1);
Serial.print("Ambient light: ");
Serial.print(lux);
Serial.println(" lux");
delay(500); // Pause before the next reading
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment