IntermediateWearables

Smart Posture Tracker

Monitor your posture and get a vibration reminder when you slouch for too long.

Time
2 h
Estimated cost
~£28
Hardware
Seeed Studio XIAO ESP32-C3

Prototype — not yet validated (wearable + battery)

Try it in the browser

What you will build

A small clip-on device worn at the upper back or collar. Press the button while sitting up straight to set your "good posture". If you lean more than 20° away from it for 30 seconds, it gives a short buzz. Sit up and it resets.

How it works

The MPU6050 measures the direction of gravity. At calibration the device stores that direction as a reference. Every 100 ms it compares the current direction with the reference and calculates the angle between them. A timer only starts the reminder after 30 s of continuous slouching, so normal movement (reaching, turning) does not trigger it. The vibration motor is switched through a transistor because it needs more current than a GPIO can give.

Parts

Part Qty Purpose Required
Seeed Studio XIAO ESP32-C3 1 Small controller with LiPo charging Yes
MPU6050 breakout (GY-521) 1 Posture angle Yes
10 mm coin vibration motor (3 V) 1 Reminder Yes
2N2222 / BC337 NPN transistor + 1 kΩ resistor + 1N4148 diode 1 each Motor driver Yes
Tactile button 1 Calibrate Yes
3.7 V LiPo, 150–300 mAh, with protection circuit (PCM) 1 Battery For wearing
Small enclosure or 3D-printed case + clip 1 Wearing For wearing

Build steps

  1. Breadboard first, USB powered, no battery.
  2. MPU6050: VCC → 3V3, GND → GND, SDA → D4, SCL → D5.
  3. Motor driver: D2 → 1 kΩ → transistor base; emitter → GND; motor between 3V3 and collector; diode across the motor (stripe to 3V3).
  4. Button between D1 and GND.
  5. In Arduino IDE select XIAO_ESP32C3. Install Adafruit MPU6050. Upload.
  6. Sit straight, press the button: one short buzz confirms calibration.
  7. Lean forward and hold for 30 s: two buzzes. Sit up: Serial Monitor shows OK.
  8. Only after it works on USB: solder the protected LiPo to the XIAO's battery pads (red +, black –), check polarity twice, and fit everything in the case.

Try it in the browser

Interactive Preview. A side-view figure on the project page with a posture slider and a 30 s timer shows when the reminder would fire. Optional Live mode reads the device's Serial output over Web Serial when plugged in.

Code

Download smart-posture-tracker.ino

// Smart Posture Tracker — XBOTNOW
// Board: Seeed XIAO ESP32-C3 · Core: esp32 v3.x · Library: Adafruit MPU6050
#include <Adafruit_MPU6050.h>

const int MOTOR_PIN = D2, BUTTON_PIN = D1;
const float SLOUCH_DEG = 20;              // allowed deviation
const unsigned long SLOUCH_MS = 30000;    // must last this long
const unsigned long REPEAT_MS = 60000;    // remind again after 1 min if still slouching

Adafruit_MPU6050 mpu;
float ref[3] = {0, 0, 1};
unsigned long slouchSince = 0, lastBuzz = 0;

void buzz(int times) {
  for (int i = 0; i < times; i++) { digitalWrite(MOTOR_PIN, HIGH); delay(200); digitalWrite(MOTOR_PIN, LOW); delay(150); }
}

void readGravity(float v[3]) {
  float sx = 0, sy = 0, sz = 0; sensors_event_t a, g, t;
  for (int i = 0; i < 10; i++) { mpu.getEvent(&a, &g, &t); sx += a.acceleration.x; sy += a.acceleration.y; sz += a.acceleration.z; delay(5); }
  float n = sqrt(sx * sx + sy * sy + sz * sz);
  v[0] = sx / n; v[1] = sy / n; v[2] = sz / n;
}

void calibrate() { readGravity(ref); buzz(1); Serial.println("Calibrated"); }

void setup() {
  Serial.begin(115200);
  pinMode(MOTOR_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  if (!mpu.begin()) { Serial.println("MPU6050 not found"); while (1) delay(500); }
  calibrate();
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) { calibrate(); delay(500); }
  float v[3]; readGravity(v);
  float dot = constrain(v[0] * ref[0] + v[1] * ref[1] + v[2] * ref[2], -1, 1);
  float angle = acos(dot) * 57.2958;

  if (angle > SLOUCH_DEG) {
    if (slouchSince == 0) slouchSince = millis();
    if (millis() - slouchSince > SLOUCH_MS && millis() - lastBuzz > REPEAT_MS) {
      buzz(2); lastBuzz = millis();
    }
  } else {
    slouchSince = 0; lastBuzz = 0;
  }
  Serial.printf("{\"angle\":%.1f,\"slouch\":%d}\n", angle, slouchSince != 0);
  delay(100);
}

Battery-life improvement for the page's "Next steps": use esp_sleep_enable_timer_wakeup and light sleep between readings.

Troubleshooting

Symptom Likely cause Check
Buzzes when sitting straight Calibrated while leaning Re-calibrate sitting upright
Motor doesn't vibrate Transistor pins swapped Check E-B-C order for your part
Resets when motor runs Missing diode or weak battery Fit the diode; charge the LiPo
Too many reminders Threshold too tight Raise SLOUCH_DEG to 25–30

Safety and limitations

Use only a LiPo with a built-in protection circuit. Never puncture, bend or short it; do not charge it unattended or while worn; stop using it if it swells or gets warm. Keep the device off skin with broken skin or under tight clothing for long periods. This is a habit reminder, not a medical or physiotherapy device, and it does not diagnose or treat back problems.

Checklist

Next project

Smart Gesture Ring: shrink motion control into something you wear on a finger.

Next project: Smart Gesture Ring