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

CodeCell: Proximity Sensing

CodeCell includes an onboard VCNL4040 proximity sensor for touchless object detection and depth-based gestures. You can easily read proximity values using the CodeCell library’s built-in function:

uint16_t proximity = myCodeCell.Light_ProximityRead();

This function returns a numerical value representing how close an object is to the sensor. Higher values indicate a closer object. You can use this reading to trigger LEDs, motors, or interactive effects in real-time.

How It Detects Proximity

CodeCell Proximity Sensing

The VCNL4040 emits infrared light and measures its reflection to estimate distance (up to ~20 cm). It communicates over I²C and is fully handled by the CodeCell Library, allowing you to focus on interaction logic instead of low-level sensor control.

Example 1 – Proximity Trigger

Turns the LED red when an object is close, and green when clear. Adjust the sensitivity using the DISTANCE_RANGE value.


#include <CodeCell.h>

CodeCell myCodeCell;

#define DISTANCE_RANGE 100U  // Proximity trigger threshold (higher = requires closer object)

void setup() {
  Serial.begin(115200);              // Start USB serial at 115200 baud
  myCodeCell.Init(LIGHT);            // Enable light + proximity sensing
  myCodeCell.LED_SetBrightness(0);   // Turn off default LED breathing animation
}

void loop() {
  if (myCodeCell.Run(1)) {           // Run the CodeCell service loop at 1 Hz

    // Read proximity; values above DISTANCE_RANGE mean "object detected / close"
    if (myCodeCell.Light_ProximityRead() > DISTANCE_RANGE) {
      myCodeCell.LED(0xFF, 0x00, 0x00);  // Red: proximity detected
    } else {
      myCodeCell.LED(0x00, 0xFF, 0x00);  // Green: no proximity detected
    }
  }
}

Example 2 – Depth Gestures (Near / Mid / Far)

Maps proximity readings into gesture zones (NEAR, MID, FAR, NONE) and sets LED colors for each state. It also prints live data to the Serial Monitor for debugging or visualization.


#include <CodeCell.h>

CodeCell myCodeCell;

// Thresholds for gesture zones (tweak as needed)
const uint16_t NEAR_THRESHOLD = 700;
const uint16_t MID_THRESHOLD  = 100;
const uint16_t FAR_THRESHOLD  = 10;

void setup() {
  Serial.begin(115200);    // Start USB serial at 115200 baud
  myCodeCell.Init(LIGHT);  // Enable onboard light + proximity sensing

  myCodeCell.LED_SetBrightness(0);  // Turn off default LED breathing animation

  Serial.println(">> Depth Gesture Demo Started");
}

void loop() {
  if (myCodeCell.Run(2)) {  // Run at 2 Hz
    uint16_t proximity = myCodeCell.Light_ProximityRead();

    // Print raw sensor value
    Serial.print("Proximity: ");
    Serial.print(proximity);
    Serial.print("  | Gesture: ");

    // Classify depth gesture
    if (proximity > NEAR_THRESHOLD) {
      Serial.println("NEAR");
      myCodeCell.LED(0xFF, 0x00, 0x00);  // Red LED for near
    } else if (proximity > MID_THRESHOLD) {
      Serial.println("MID");
      myCodeCell.LED(0xFF, 0xA5, 0x00);  // Orange LED for mid
    } else if (proximity > FAR_THRESHOLD) {
      Serial.println("FAR");
      myCodeCell.LED(0x00, 0x00, 0xFF);  // Blue LED for far
    } else {
      Serial.println("NONE");
      myCodeCell.LED(0x00, 0x00, 0x00);  // LED off for no hand
    }
  }
}

Customization Tips

  • Tune thresholds: Adjust DISTANCE_RANGE, NEAR_THRESHOLD, MID_THRESHOLD, and FAR_THRESHOLD to fit your setup.
  • Change feedback: Swap LED colors or use motors, servos, or buzzers for alternate cues.
  • Combine sensors: Pair proximity data with light or IMU readings for gesture-controlled lighting, robots, or smart interfaces.
  • 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

87 reviews
Write a review
87%
(76)
5%
(4)
2%
(2)
2%
(2)
3%
(3)
81
21
A
CodeCell C3
Anonymous

Perfect product!

A
ProtoBot
Anonymous

This is a great product for beginners who want hands-on experience with Arduino and 3D printing. The Protobot has a sleek, solid design, and the software is just as polished. I’ve bought several educational robotics kits, and this is the best one so far. Looking forward to more awesome products!

Thank you so much for your kind review! 😊

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

Thank you so much for your thoughtful review! Glad you’re enjoying ProtoBot 😊 A localisation sensor is a great suggestion! The I²C connection lets you add compatible sensors, so there’s room to explore this. Thanks for your support!

G
Experiment in Progress Mug
Gerald Kendrick

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

Hi Gerald, we’re so glad you’re enjoying it! Thanks for sharing your kind review.

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

Very useful fun little screwdriver for the all tiny stuff

Hi Klaas, thanks for the great review! We’re glad you’re enjoying your screwdriver!