In this guide, we'll explore how to use the CodeCell's onboard proximity sensor to detect objects. and also depth gestures.
In its tiny package CodeCell packs a VCNL4040 that uses infrared light to detect objects within its range. It measures the reflection of emitted IR light to approximate how close an object is, allowing you to create responsive behaviors based on proximity. This feature is particularly useful for creating interactive lighting, robotics, touchless switches, or other proximity-based actions.
The onboard sensor can measure proximity up to 20 cm away, and uses I2C communication which is automatically handled by our CodeCell library.
In this example, the CodeCell continuously monitors proximity data and turns on a red LED when an object is detected within its proximity threshold.
Ensure your CodeCell is properly connected via USB-C, and follow the comments in the code to understand each step.
#include <CodeCell.h>
CodeCell myCodeCell;
void setup() {
Serial.begin(115200); // Set Serial baud rate to 115200. Ensure Tools/USB_CDC_On_Boot is enabled if using Serial
myCodeCell.Init(LIGHT); // Initializes light sensing, including proximity
}
void loop() {
if (myCodeCell.Run(10)) {
// Runs every 100ms to check proximity
uint16_t proximity = myCodeCell.Light_ProximityRead();
// Check if an object is within range
if (proximity > 100) {
myCodeCell.LED(0xFF, 0, 0); // Set LED to Red when proximity is detected
delay(1000); // Keep the LED on for 1 second
} else {
// No action if the object is out of range
}
}
}
100
in the example) to adjust the sensitivity of proximity detection based on your application.myCodeCell.LED()
function to create multi-colored responses to proximity.By moving your hand or other objects closer or further from the sensor, you can create dynamic inputs that drive various actions. In this example, the proximity data is used to control the angle of two FlatFlaps, which are connected to two DriveCells (H-bridge drivers).
The Pulse width modulation (PWM) duty cycle of two FlatFlaps is therefore adjusted based on how close the object is. As the object moves closer or further, the angle of the FlatFlaps changes, demonstrating a simple yet effective method for depth gesture-based control. Follow the comments in the code to understand each step:
#include <CodeCell.h>
#include <DriveCell.h>
#define IN1_pin1 2
#define IN1_pin2 3
#define IN2_pin1 5
#define IN2_pin2 6
DriveCell FlatFlap1(IN1_pin1, IN1_pin2);
DriveCell FlatFlap2(IN2_pin1, IN2_pin2);
CodeCell myCodeCell;
void setup() {
Serial.begin(115200); // Set Serial baud rate to 115200. Ensure Tools/USB_CDC_On_Boot is enabled if using Serial.
myCodeCell.Init(LIGHT); // Initializes Light Sensing
FlatFlap1.Init();
FlatFlap2.Init();
FlatFlap1.Tone();
FlatFlap2.Tone();
}
void loop() {
if (myCodeCell.Run(10)) {
// Runs every 100ms
uint16_t proximity = myCodeCell.Light_ProximityRead();
Serial.println(proximity);
if (proximity < 100) {
// If proximity is not detected, the FlatFlaps flap
FlatFlap1.Run(1, 100, 400);
FlatFlap2.Run(1, 100, 400);
} else {
// Adjust FlatFlap angle based on proximity
proximity = proximity - 100;
proximity = proximity / 10;
if (proximity > 100) {
proximity = 100;
}
FlatFlap1.Drive(0, proximity);
FlatFlap2.Drive(0, proximity);
}
}
}
This project introduces the basics of using proximity sensing with CodeCell, opening up a range of interactive possibilities. Experiment with the code, tweak the settings, and check out the CodeCell GitHub Repository for more code examples and technical documentation!
Join our Community ~ Be the first to know about new products and get exciting deals!
© 2025 Microbots.