DIY Ambient Light Indicator
Installing the Required Libraries and Sketch
- Install the SparkFun_Qwiic_Button and SparkFun_VEML7700_Arduino_Library: Open the Arduino IDE and navigate to Sketch > Include Library > Manage Libraries. In the Library Manager, search for "SparkFun Qwiic Button and Sparkfun VEML7700" and install the latest versions of each from SparkFun.
You can now copy and paste the code below into a fresh Arduino sketch:
#include <Wire.h>
#include <SparkFun_Qwiic_Button.h>
#include <SparkFun_VEML7700_Arduino_Library.h>
QwiicButton button;
VEML7700 lightSensor;
void setup() {
Serial.begin(115200);
Wire.begin();
// Initialize the button
if (!button.begin()) {
Serial.println("Button not found!");
while (1);
}
Serial.println("Button found.");
// Initialize the ambient light sensor
if (!lightSensor.begin()) {
Serial.println("Light sensor not found!");
while (1);
}
Serial.println("Light sensor found.");
button.LEDon(0); // Start with LED on, but low brightness
}
void loop() {
// Check if button is pressed
if (button.isPressed()) {
// If pressed, turn off LED regardless of light
button.LEDoff();
Serial.println("Button pressed, LED off.");
// Wait until released
while (button.isPressed()) {
delay(10);
}
Serial.println("Button released.");
} else {
// Read light level
float lux = lightSensor.getLux();
// Clamp lux to a usable range (0 to 400 for mapping)
if (lux > 400) lux = 400;
if (lux < 0) lux = 0;
// Invert lux to brightness: lower light → higher brightness (0–255)
uint8_t brightness = map(lux, 0, 400, 255, 0);
// Apply brightness to LED
button.LEDon(brightness);
Serial.print("Lux: ");
Serial.print(lux);
Once you have the sketch and libraries installed, you can upload the code to your development board. After uploading, the Qwiic button will be fully lit when the room is dark and will turn off when there is enough light. To adjust how sensitive the brightness control is, you can change the value 400 to any number between 100 and 1000. This number sets the light threshold needed to turn the button on or off.
Need Help?
If you need technical assistance or more information on a product that is not working as you expected, we recommend heading over to the SparkFun Technical Assistance page for some initial troubleshooting.
If you can't find what you need there, the SparkFun Forums is a great place to search product forums and ask questions.
Account Registration Required: If this is your first visit to our forum, you'll need to create a Forum Account to post questions.