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!
In this guide, we'll explore how to use the CodeCell to sense white light and automatically adjust the brightness of an LED or create other responsive lighting effects to adapt to light conditions.
In its tiny package, CodeCell packs a VCNL4040 that can precisely measure ambient light levels using its built-in photodiode. This allows it to detect changes in brightness, enabling automatic adjustments for energy-saving lighting systems, or ambient-aware interactions.
The onboard sensor offers high-resolution light sensing across a wide dynamic range and communicates via I2C, which is seamlessly managed by our CodeCell library.
The VCNL4040 sensor on the CodeCell is capable of both ambient light sensing and white light sensing, each serving distinct purposes:
In this example, we're using the white light sensing to directly influence the brightness of the CodeCell's on-board LED. This is based on the detected white light levels, creating a more targeted response compared to general ambient light sensing. As the room gets darker, the LED will dim, providing a smooth transition that you can tweak and customize for your own lighting projects.
Make sure your CodeCell is connected properly, 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.
}
void loop() {
delay(100); // Small delay - You can adjust it accordingly
// Read white light from the sensor and adjust brightness for 8-bit
uint16_t brightness = (myCodeCell.Light_WhiteRead()) >> 3;
Serial.println(brightness); // Print the brightness value to the serial monitor for debugging.
// Limit the sensor values to the LED's brightness range (1 to 254)
if (brightness == 0U) {
brightness = 1U; // Set a minimum brightness to avoid turning off the LED completely
} else if (brightness > 254U) {
brightness = 254U; // Cap the brightness to the LED's maximum level
}
brightness = 255U - brightness; // Invert the brightness so the LED dims as it gets brighter
myCodeCell.LED(0, 0, brightness); // Shine the onboard blue RGB LED with the new adjusted brightness
}
myCodeCell.LED()
function allows you to specify RGB values. Try experimenting with different colors based on light levels.This example is just the starting point for utilizing the CodeCell's light-sensing capabilities. Dive into the code, make it your own, 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.