Menu
Microbots
0
  • Learn
    • Getting Started
    • Maker Builds
    • Education
  • Shop
    • ProtoBot
    • Modules & Parts
    • Tools & Gears
    • Coming Soon
  • About
    • Our Story
    • Reach Out
    • FAQs
  • Sign in
  • English
  • Your Cart is Empty
Microbots
  • Learn
    • Getting Started
    • Maker Builds
    • Education
  • Shop
    • ProtoBot
    • Modules & Parts
    • Tools & Gears
    • Coming Soon
  • 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!

© 2026 Microbots.

★ Reviews

Let customers speak for us

73 reviews
Write a review
85%
(62)
5%
(4)
3%
(2)
3%
(2)
4%
(3)
67
21
G
ProtoBot
Gennadii

Got a Pro version, it's very easy to put together, and it works great.
Would be great to have brushless motors and more GPIO access through the plastic shell in ProtoBot v2

A
ProtoBot
Armand

It is a compact, very well designed RC-like robotic car with endless possibilities for those with active imaginations. Inspirational and creative in a small form factor. Innovate and enjoy!

User picture
J
ProtoBot
Javier

I Recieved yesterday my ProtoBot and the quality and details are awesome!

User picture
C
CodeCell C3
Cloke74

Great piece of kit, had just what i needed to complete the project i had in mind. Shame shipping to the UK is so expensive, but appreciate this isn’t necessarily in the hands of MicroBots

A
CodeCell C6
Anonymous

I had an issue, got a red light, I used too much flux. Support said clean it, then the one sensor worked fine. I got the help and answer same day I provided a foto.

123