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: 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

50 reviews
Write a review
84%
(42)
4%
(2)
2%
(1)
4%
(2)
6%
(3)
21
46
B
CodeCell C6 Drive
Brandon

Awesome product with great tutorials and example code

G
MotorCell
Gerald Kendrick

Very happy with my MotorCell. I'm incorporating it into a prototype project that will hopefully result in me needing a few more!

User picture
A
CodeCell C6 Drive
Anonymous

Great product! Having the IMU, motor driver, and battery management directly on the board is incredibly handy for quick prototyping. Love it!

Improvement ideas:
- using an ESP32 other than the C6 to get more cores. On a single-core chip, WiFi tasks often interfere with real-time applications.
- adding two more motor drivers (with a slightly higher current rating) would be awesome for drone projects!
- I know the compactness of the board is a huge selling point and really optimised, but exposing a few more pins would be great. With the motor drivers already occupying 4 pins, having only 4 GPIOs left can be tight for complex projects (though I’m nitpicking, I’m really pushing this board to its limits!).

A
CodeCell C6
Anonymous

I use CodeCell C6, like all the Features, and compactness. The remote Link to the iPhone with some Controls for 2 Motors is just perfect for the job.

O
CodeCell C6 Drive
Odd_Jayy

This is one of my Favorite finds, this board has everything you need to make a quick and small robot, easy to set up and install. Perfect for beginners or a person who needs to save room in their build.

User picture
123