In this guide, we'll explore how to use the CodeCell's onboard motion sensor to detect taps. This project demonstrates how to use tap detection for interactive controls, making it perfect for creating responsive actions with a simple tap on the device.
CodeCell is is equipped with a BNO085 motion sensor that fuses the data from an accelerometer, gyroscope and magnetometer, to track specific movement patterns and determine if a tap was made, making it ideal for interactive controls like toggling lights, triggering sound effects, or other actions based on a simple tap gesture.
In the first example, we setup our CodeCell to continuously check for a tap. When a tap is detected, the onboard LED lights up in yellow for one second. You can expand on this basic functionality to create more complex interactions, such as controlling multiple LEDs, motors, or other connected devices based on tap inputs.
Make sure 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(MOTION_TAP_DETECTOR); // Initializes tap detection sensing
}
void loop() {
if (myCodeCell.Run(10)) {
// Runs every 100ms to check for taps
if (myCodeCell.Motion_TapRead()) {
// If a tap is detected, shine the LED yellow for 1 second
myCodeCell.LED(0xA0, 0x60, 0x00); // Set LED to yellow
delay(1000); // Keep the LED on for 1 second
}
}
}
In this next example, we use a CoilCell to flip its polarity, and actuate a flip-dot. This expands the interactivity by using tap detection to control external devices, creating a more dynamic response.
#include <CoilCell.h>
#include <CodeCell.h>
#define IN1_pin1 5
#define IN1_pin2 6
CoilCell myCoilCell(IN1_pin1, IN1_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(MOTION_TAP_DETECTOR); // Initializes tap detection sensing.
myCoilCell.Init(); // Initializes the CoilCell.
myCoilCell.Tone(); // Plays a tone to confirm initialization.
}
void loop() {
if (myCodeCell.Run(10)) {
// Runs every 100ms to check for taps.
if (myCodeCell.Motion_TapRead()) {
// If a tap is detected, shine the LED yellow and flip the CoilCell's polarity.
myCodeCell.LED(0xA0, 0x60, 0x00); // Set LED to yellow.
myCoilCell.Toggle(100); // Toggle the CoilCell's polarity.
delay(1000); // Delay to keep the LED on and polarity flipped for 1 second.
}
}
}
Experiment with the code, customize the responses, 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.