🎉 Black Friday is Here! Enjoy Up to 35% Off ~ Offer Ends November 30th!
In this build, we'll explore how to use the CodeCell to sense white light and automatically adjust the brightness of LEDs. This project demonstrates the CodeCell's onboard light sensor, helping you create responsive lighting effects that adapt to changing light conditions.
The CodeCell features a built-in VCNL4040 sensor that can measure both light levels and proximity up to 20 cm. This sensor uses I2C communication, and talks to the ESP32 in the CodeCell library, where the sensor is automatically initialized to optimize its sensing resolution. This makes the setup straightforward, so you can focus on building your project.
The VCNL4040 sensor on the CodeCell is capable of both ambient light sensing and white light sensing, each serving distinct purposes:
In this project, we're using the white light sensing feature to directly influence LED brightness based on the detected white light levels, creating a more targeted response compared to general ambient light sensing.
In this example, the CodeCell continuously measures ambient white light and adjusts the brightness of an onboard LED based on the detected light level. As the room gets darker, the LED will dim, providing a smooth transition that you can tweak and customize for your own lighting projects.
Below is the example code to get you started. 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 project is just the starting point for utilizing the CodeCell's light-sensing capabilities. Dive into the code, make it your own, and light up your next project!
Be the first to know about new projects and get exciting deals!
© 2024 Microbots.