🎉 Black Friday is Here! Enjoy Up to 35% Off ~ Offer Ends November 30th!
In this build, we'll explore how to use the CodeCell's onboard motion sensor to measure step counts and display these counts on an OLED display. This project demonstrates how to create a step counter, ideal for fitness trackers, pedometers, or any other DIY project that requires activity monitoring.
The CodeCell is equipped with a motion sensor that can track step counts by using its onboard sensors to detect specific movement patterns. This algorithm is performed inside the BNO085 sensor, and the CodeCell library help you easily reads these step counts.
In this example, the CodeCell continuously monitors for steps and updates the count. This count is then displayed on an OLED screen using the Adafruit SSD1306 library.
Below is the example code to get you started. Ensure your CodeCell is properly connected via USB-C and your OLED display is correctly wired to the CodeCell's lower side. There you can use its ground, 3V3 and I2C pins (SDA and SCL).
Follow the comments in the code to understand each step.
#include <CodeCell.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
CodeCell myCodeCell;
/* Configure the OLED Display */
#define SCREEN_WIDTH 64 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // Address of the OLED display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
uint16_t step_counter = 0;
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_STEP_COUNTER); // Initializes step counting and activity sensing.
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println("Display Error");
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.display();
delay(2000);
}
void loop() {
if (myCodeCell.Run()) {
// Read step count from the CodeCell motion sensor.
myCodeCell.Motion_StepCounterRead(step_counter);
// Clear the display and show the step count.
display.clearDisplay();
display.setCursor(32, 16); // Start at top-left corner
display.print(F("Steps: "));
display.print(step_counter);
display.display();
}
}
This project demonstrates how to use the CodeCell’s motion sensor to count steps and display the count on an OLED screen. Experiment with the code,to create your own wearable fitness device!
Be the first to know about new projects and get exciting deals!
© 2024 Microbots.