Build it step by step
0 of 7 doneTest the IMU
Print the acceleration while you move.
Plot a rep
See the up and down shape in the serial plotter.
Count with hysteresis
Two thresholds and a minimum time.
Add the set buzz
Buzz after 10 reps.
Add BLE
Notify the counts to a scanner app.
Build the strap
Fit the board snugly on the wrist.
Tune
Adjust the thresholds for each exercise.
How do you build a gym rep counter?
An IMU on your wrist counts reps of curls, presses and squats and shows sets on your phone. You need Seeed XIAO ESP32S3, LSM6DS3 6-axis IMU, Li-Po battery 3.7 V 500 mAh, Vibration motor (coin type), BC547 NPN transistor, Resistor 1 kΩ and Signal Diode 1N4148. It takes 7 steps and about 240 minutes.
- Level
- Intermediate
- Time
- About 240 min
- Parts
- 7
Wiring
| From | To | Why |
|---|---|---|
| IMU VCC and GND | XIAO 3V3 and GND | Power |
| IMU SDA | XIAO D4 | I2C data |
| IMU SCL | XIAO D5 | I2C clock |
| XIAO D2 | 1 kΩ to BC547 base | Motor switch |
| BC547 collector | Motor minus | Switched return |
| BC547 emitter | XIAO GND | Ground |
| Motor plus | XIAO 3V3 | Supply |
| 1N4148 | Across the motor, stripe to 3V3 | Flyback protection |
| LiPo | XIAO BAT pads | Power and charging |
Code
Every rep of a curl or press makes a rise and fall in the acceleration on the wrist. The sketch subtracts a slow baseline so gravity and posture do not matter, then counts a rep each time the movement rises above an upper threshold and has fallen below a lower one since the last rep; that gap is called hysteresis and stops one rep from counting twice. A minimum of 0.7 seconds between reps ignores jitter. Record a few reps of each exercise on the serial plotter and adjust the two thresholds to suit; a slow squat has a smaller swing than a fast curl. A short buzz marks the end of a set and a BLE scanner app can read the counts. This is a training aid and not a medical device; use proper form and a sensible weight, and stop if you feel pain. Wear it on a snug strap, not a tight one, and use a protected LiPo in a sewn pocket; never train with it while charging. Arduino IDE with the ESP32 boards package, board "XIAO_ESP32S3".
Runs on a virtual Seeed XIAO ESP32-S3 in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Rep counter with BLE. Libraries: Adafruit LSM6DS, ESP32 BLE Arduino (built in). Board: XIAO ESP32S3.
// Change the class if your chip differs: Adafruit_LSM6DS3, Adafruit_LSM6DS3TRC or Adafruit_LSM6DS33.
#include <Wire.h>
#include <Adafruit_LSM6DS3.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define SERVICE_UUID "6e400011-b5a3-f393-e0a9-e50e24dcca9e" // example UUIDs, make your own for real projects
#define COUNT_UUID "6e400012-b5a3-f393-e0a9-e50e24dcca9e" // notify: reps and sets as two bytes
Adafruit_LSM6DS3 imu;
BLECharacteristic* countChar;
bool connected = false;
const int MOTOR = D2;
const int REPS_PER_SET = 10;
int reps = 0, sets = 0;
float filtered = 0, level = 0; // smoothed movement and its slow average
bool above = false; unsigned long lastRep = 0;
class Cb : public BLEServerCallbacks {
void onConnect(BLEServer*) { connected = true; }
void onDisconnect(BLEServer* s) { connected = false; s->startAdvertising(); }
};
void buzz(int ms) { digitalWrite(MOTOR, HIGH); delay(ms); digitalWrite(MOTOR, LOW); }
void setup() {
pinMode(MOTOR, OUTPUT);
Wire.begin();
imu.begin_I2C();
BLEDevice::init("ElectroLark-Reps");
BLEServer* srv = BLEDevice::createServer(); srv->setCallbacks(new Cb());
BLEService* svc = srv->createService(SERVICE_UUID);
countChar = svc->createCharacteristic(COUNT_UUID, BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_READ);
countChar->addDescriptor(new BLE2902());
svc->start();
BLEDevice::getAdvertising()->addServiceUUID(SERVICE_UUID);
BLEDevice::getAdvertising()->start();
}
void loop() {
sensors_event_t a, g, t; imu.getEvent(&a, &g, &t);
float mag = sqrt(a.acceleration.x * a.acceleration.x + a.acceleration.y * a.acceleration.y + a.acceleration.z * a.acceleration.z);
filtered += (mag - filtered) * 0.2; // smooth quick jitter
level += (filtered - level) * 0.01; // slow baseline (gravity and posture)
float swing = filtered - level; // movement above or below the baseline
// hysteresis: count when the swing rises above the upper threshold, re-arm only after it falls below the lower one
if (!above && swing > 2.0 && millis() - lastRep > 700) { // 2.0 m/s^2 up, at least 0.7 s between reps
above = true; lastRep = millis(); reps++;
if (reps >= REPS_PER_SET) { reps = 0; sets++; buzz(300); } // buzz at the end of each set
uint8_t d[2] = { (uint8_t)reps, (uint8_t)sets };
countChar->setValue(d, 2); if (connected) countChar->notify();
}
if (above && swing < 0.8) above = false;
delay(10);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Do 10 slow curls | Counts 10, buzzes once |
| Shake the wrist lightly | Does not count |
| Rest | Count stays still |
| Phone scanner | Shows reps and sets update |
If it does not work
Counts twice per rep
Raise the lower threshold or lengthen the minimum time.
Misses slow reps
Lower the upper threshold.
No BLE
Check the board package and Bluetooth is on.
Motor always on
Check the transistor pins and the base resistor.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the gym rep counter?
Seeed XIAO ESP32S3, LSM6DS3 6-axis IMU, Li-Po battery 3.7 V 500 mAh, Vibration motor (coin type), BC547 NPN transistor, Resistor 1 kΩ and Signal Diode 1N4148.
How long does the gym rep counter take to build?
About 240 minutes for a intermediate maker, in 7 steps.
How do I know the gym rep counter works?
Do 10 slow curls: Counts 10, buzzes once. Shake the wrist lightly: Does not count. Rest: Count stays still.
Counts twice per rep: what should I check?
Raise the lower threshold or lengthen the minimum time.
Misses slow reps: what should I check?
Lower the upper threshold.
No BLE: what should I check?
Check the board package and Bluetooth is on.
Motor always on: what should I check?
Check the transistor pins and the base resistor.
