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); // Start USB serial at 115200 baud
myCodeCell.Init(LIGHT); // Enable onboard light/proximity sensing
myCodeCell.LED_SetBrightness(0); // Disable the default LED breathing annimation
}
void loop() {
if (myCodeCell.Run(10)) { // Run loop at 10 Hz
uint16_t brightness = (myCodeCell.Light_WhiteRead()) >> 3; // Map the raw white-light reading to an 8-bit-like range
// Clamp to a usable LED range (1–254)
if (brightness == 0U) {
brightness = 1U; // Prevent the LED from fully turning off
} else if (brightness > 254U) {
brightness = 254U; // Cap to avoid over-driving
}
// Invert: uncomment to make darker room -> brighter LED
// brightness = 255U - brightness; // Invert
Serial.println(brightness); // Debug: scaled LED value
myCodeCell.LED(0, 0, brightness); // Drive onboard LED (blue channel)
}
}
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.