Menu
Microbots
0
  • Robots
    • ProtoBot
    • CubeBot
    • Robot Parts
  • Gear
    • Tools
    • Merch
  • Parts
    • CodeCell ESP32 Modules
    • Motors & Actuators
    • LiPo Batteries
  • Learn
    • User Guide & Tutorials
    • DIY Projects & Files
    • Education Discounts
  • About
    • Our Story
    • Reach Out
    • FAQs
  • Sign in
  • Your Cart is Empty
Microbots
  • Robots
    • ProtoBot
    • CubeBot
    • Robot Parts
  • Gear
    • Tools
    • Merch
  • Parts
    • CodeCell ESP32 Modules
    • Motors & Actuators
    • LiPo Batteries
  • Learn
    • User Guide & Tutorials
    • DIY Projects & Files
    • Education Discounts
  • About
    • Our Story
    • Reach Out
    • FAQs
  • 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!

© 2026 Microbots.

★ Reviews

Let customers speak for us

85 reviews
Write a review
87%
(74)
5%
(4)
2%
(2)
2%
(2)
4%
(3)
79
21
F
ProtoBot
Francis van Uyten

Really love this robot. I like that is simple to assemble and I like that you guys prepares it easy to start programming with examples and libraries. My opinion on what to improve is more like a add-on, can you guys develop a localisation sensor or function so it will know where the bot is. Expecting only great things coming from you guys. Succes

G
Experiment in Progress Mug
Gerald Kendrick

I am enjoying my Experiment in Progress coffee mug. It fits in well in the lab.

K
12-in-1 Screwdriver (Pen-Size)
Klaas

Very useful fun little screwdriver for the all tiny stuff

f
ProtoBot
frederic marty

I don't have much time right now to test the micro bot, but it looks good.

f
MotorCell
frederic marty

I don't have much time right now to test it but it looks good.

123