Menu
Microbots
0
  • Learn
    • Getting Started
    • Maker Builds
    • Education
  • Shop
    • Modules & Parts
    • Tools & Gears
    • Robots & Displays
  • About
    • Our Story
    • Reach Out
    • FAQs
  • Sign in
  • 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

CodeCell Light Sensing

CodeCell includes a built-in VCNL4040 light sensor that can precisely measure both ambient and white light. This sensor enables automatic brightness adjustment, energy-efficient lighting, and environment-aware feedback effects.

How It Detects Light

The VCNL4040 integrates a high-sensitivity photodiode and ADC to detect brightness across a wide range. It communicates via I²C and is fully managed by the CodeCell Library, so you can begin reading light levels with a single line of code.

Ambient vs. White Light Sensing

  • Ambient Light: Measures total brightness from all light sources, ideal for auto-dimming displays or day/night detection. Read it using:
    uint16_t ambient = myCodeCell.Light_AmbientRead();
    This function returns the combined light intensity from the environment, including sunlight and artificial light.
  • White Light: Focuses on daylight or LED-type light sources, useful for lighting control or color-sensitive applications. Read it using:
    uint16_t white = myCodeCell.Light_WhiteRead();
    This function isolates white light levels, making it ideal for brightness calibration or adaptive illumination.

Example – Auto-Dimming LED

This example continuously measures white light and adjusts the onboard LED brightness. The LED becomes brighter in strong light and dims as the room darkens. You can invert or tweak the effect to your preference.


#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 animation
}

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)
  }
}

Customization Tips

  • Adjust Sensitivity: Modify scaling or mapping to fine-tune brightness response.
  • Change LED Colors: Use myCodeCell.LED(r, g, b) for different visual feedback.
  • Combine Sensors: Mix light and proximity sensing for gesture-based or adaptive lighting effects.
  • Expand Outputs: Add external LEDs or NeoPixels for ambient feedback control.
  • 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.