Menu
Microbots
0
  • Learn
  • Shop
    • Maker-Modules
    • Maker-Packs
    • Tools & Gears
    • Robots & Displays
    • All Products
  • Community
    • Education
    • Software
  • About
    • Our Story
    • Reach Out
    • FAQs
  • English
  • Your Cart is Empty
Microbots
  • Learn
  • Shop
    • Maker-Modules
    • Maker-Packs
    • Tools & Gears
    • Robots & Displays
    • All Products
  • Community
    • Education
    • Software
  • About
    • Our Story
    • Reach Out
    • FAQs
  • Language

  • 0 0

CodeCell: Brightness Detection

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.

How it detects light?

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.

Ambient Light Sensing vs. White Light Sensing

The VCNL4040 sensor on the CodeCell is capable of both ambient light sensing and white light sensing, each serving distinct purposes:

  • Ambient Light Sensing: This mode measures the overall light intensity from all sources in the environment, including natural and artificial light. It's ideal for applications that require a general understanding of the light levels in a space, such as adjusting screen brightness or activating night mode in devices.
  • White Light Sensing: This mode specifically measures the intensity of white light, which is particularly useful when the goal is to assess light sources that resemble daylight or LED lighting. White light sensing is beneficial in scenarios where you want to distinguish between different lighting conditions, such as separating white light from other colored lights or in situations where accurate color temperature is important.

Example Code

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
}

Tips for Customization 

  • Adjust Brightness Sensitivity: Adjust the brightness limits based on your room's light levels.
  • Change LED Colors: The myCodeCell.LED() function allows you to specify RGB values. Try experimenting with different colors based on light levels.
  • Add More LEDs: Connect more LEDs or even NeoPixels for more lighting effects and adjust their brightness with the same technique. 
  • Add Proximity Control: Add the proximity sensor to add more interactive effects, like depth gestures to act like a switching.

Conclusion

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!

  • Share:

Follow

Github

  • About
  • Software
  • Education
  • Contact
  • FAQs
  • Terms
  • Refund Policy
  • Privacy Policy

Join our Community ~ Be the first to know about new products and get exciting deals!

© 2025 Microbots.