In this build, we'll explore how to use the CodeCell's onboard proximity sensor to detect objects.
The CodeCell is equipped with a VCNL4040 sensor that can measure proximity up to 20 cm. This sensor uses I2C communication and is automatically initialized through the CodeCell library, allowing for seamless integration into your projects. Whether you're looking to add simple gesture depth control or detect nearby objects, the VCNL4040 makes it easy to add proximity sensing into your builds.
The VCNL4040 proximity sensor 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.
In this example, the CodeCell continuously monitors proximity data and turns on a red LED when an object is detected. You can expand on this basic functionality to create more complex interactions, such as varying the LED color or brightness based on distance, or triggering different actions based on proximity.
Below is the example code to get you started. 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()) {
// 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.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 make it your own!
Be the first to know about new projects and get exciting deals!
© 2024 Microbots.