Menu
Microbots
0
  • Learn
    • Getting Started
    • Maker Builds
    • Education
  • Shop
    • Modules & Parts
    • Tools & Gears
    • Robots & Displays
  • About
    • Our Story
    • Reach Out
    • FAQs
  • English
  • Your Cart is Empty
Microbots
  • Learn
    • Getting Started
    • Maker Builds
    • Education
  • Shop
    • Modules & Parts
    • Tools & Gears
    • Robots & Displays
  • 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);             // 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)
  }
}

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.