In this guide, we'll explore how to use the CodeCell to sense white light and automatically adjust the brightness of an LED or create other responsive lighting effects to adapt to light conditions.
In its tiny package, CodeCell packs a VCNL4040 that can precisely measure ambient light levels using its built-in photodiode. This allows it to detect changes in brightness, enabling automatic adjustments for energy-saving lighting systems, or ambient-aware interactions.
The onboard sensor offers high-resolution light sensing across a wide dynamic range and communicates via I2C, which is seamlessly managed by our CodeCell library.
The VCNL4040 sensor on the CodeCell is capable of both ambient light sensing and white light sensing, each serving distinct purposes:
In this example, we're using the white light sensing to directly influence the brightness of the CodeCell's on-board LED. This is based on the detected white light levels, creating a more targeted response compared to general ambient light sensing. As the room gets darker, the LED will dim, providing a smooth transition that you can tweak and customize for your own lighting projects.
Make sure your CodeCell is connected properly, and follow the comments in the code to understand each step.
#include <CodeCell.h>
CodeCell myCodeCell;
void setup() {
Serial.begin(115200); // Set Serial baud rate to 115200. Ensure Tools/USB_CDC_On_Boot is enabled if using Serial.
myCodeCell.Init(LIGHT); // Initializes light sensing.
}
void loop() {
delay(100); // Small delay - You can adjust it accordingly
// Read white light from the sensor and adjust brightness for 8-bit
uint16_t brightness = (myCodeCell.Light_WhiteRead()) >> 3;
Serial.println(brightness); // Print the brightness value to the serial monitor for debugging.
// Limit the sensor values to the LED's brightness range (1 to 254)
if (brightness == 0U) {
brightness = 1U; // Set a minimum brightness to avoid turning off the LED completely
} else if (brightness > 254U) {
brightness = 254U; // Cap the brightness to the LED's maximum level
}
brightness = 255U - brightness; // Invert the brightness so the LED dims as it gets brighter
myCodeCell.LED(0, 0, brightness); // Shine the onboard blue RGB LED with the new adjusted brightness
}
myCodeCell.LED()
function allows you to specify RGB values. Try experimenting with different colors based on light levels.This example is just the starting point for utilizing the CodeCell's light-sensing capabilities. Dive into the code, make it your own, and check out the CodeCell GitHub Repository for more code examples and technical documentation!
Join our Community ~ Be the first to know about new products and get exciting deals!
© 2025 Microbots.