Projects / Sleep movement tracker

Sleep movement tracker

Intermediate4 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Test the sensor

    Print the movement while you wave.

  2. ESP32

    Sum movement per minute

    Compute the average.

  3. ESP32

    Label the minutes

    Still, restless, awake.

  4. ESP32MICROS

    Log to the card

    One line per minute.

  5. ESP32+

    Add the battery

    LiPo and the charging pads.

  6. ESP32

    Build the band

    Soft strap and a small case.

  7. ESP32

    Wear it for a night

    Plot the CSV in a spreadsheet.

In short

How do you build a sleep movement tracker?

A wristband logs movement through the night and shows a sleep chart with restless and still periods. You need Seeed XIAO ESP32S3, LIS3DH accelerometer, Li-Po battery 3.7 V 500 mAh and MicroSD Card Module. It takes 7 steps and about 240 minutes.

Level
Intermediate
Time
About 240 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
LIS3DH VCC and GNDXIAO 3V3 and GNDPower
LIS3DH SDA and SCLXIAO D4 and D5I2C
SD module CSXIAO D2Chip select
SD module SCK, MISO, MOSIXIAO D8, D9, D10SPI
SD module VCC and GNDXIAO 5V (USB) or 3V3 (check the module) and GNDPower
LiPoXIAO BAT padsPower and charging

Code

A wrist accelerometer cannot see sleep stages, only movement, so this logger records how much the wrist moves each minute and labels the minute still, restless or awake using two thresholds you can adjust. The file has one line a minute and opens in a spreadsheet for a chart of the night. It does not measure sleep quality, heart rate or breathing, and it is not a medical device. If you are worried about your sleep, speak to a doctor. Wear the band snugly but not tightly, with a soft strap and no sharp edges, and keep the LiPo in a protected pouch away from the skin on the side not facing you; never wear it while charging. A 500 mAh cell runs this overnight. The sleep chart on your phone is a follow-up: copy the CSV and open it in any spreadsheet app. 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).

// Sleep movement logger: activity per minute to SD. Libraries: Adafruit LIS3DH, SD. Board: XIAO ESP32S3.
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_LIS3DH.h>

Adafruit_LIS3DH lis;
const int SD_CS = D2;
unsigned long minuteStart = 0, started = 0, lastSample = 0; float activity = 0; int samples = 0, minutes = 0;
float prev = 9.8;

void setup() {
  Wire.begin(); lis.begin(0x18); lis.setRange(LIS3DH_RANGE_2_G);
  SD.begin(SD_CS);
  File f = SD.open("/SLEEP.CSV", FILE_APPEND);
  if (f && f.size() == 0) f.println("minute,activity,state");
  if (f) f.close();
  started = minuteStart = millis();
}

void loop() {
  if (millis() - lastSample >= 100) {                         // ten samples a second
    lastSample = millis();
    sensors_event_t e; lis.getEvent(&e);
    float mag = sqrt(e.acceleration.x * e.acceleration.x + e.acceleration.y * e.acceleration.y + e.acceleration.z * e.acceleration.z);
    activity += fabs(mag - prev); prev = mag; samples++;      // sum of changes means how much the wrist moved
  }
  if (millis() - minuteStart >= 60000) {
    float score = activity / max(samples, 1);                  // average movement in this minute
    const char* state = score < 0.15 ? "still" : (score < 0.6 ? "restless" : "awake");
    File f = SD.open("/SLEEP.CSV", FILE_APPEND);
    if (f) { f.print(minutes); f.print(","); f.print(score, 3); f.print(","); f.println(state); f.close(); }
    minutes++; minuteStart = millis(); activity = 0; samples = 0;
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Lie stillMinutes marked still
Roll overA restless minute
Walk to the kitchenAwake minutes
MorningFile has about 480 lines for 8 hours

If it does not work

All still

Lower the thresholds or check the sensor is working.

All awake

Raise the thresholds, check the wrist strap is firm.

SD errors

Format FAT32 and check the chip select.

Battery drains early

Use a bigger cell or log less often.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the sleep movement tracker?

Seeed XIAO ESP32S3, LIS3DH accelerometer, Li-Po battery 3.7 V 500 mAh and MicroSD Card Module.

How long does the sleep movement tracker take to build?

About 240 minutes for a intermediate maker, in 7 steps.

How do I know the sleep movement tracker works?

Lie still: Minutes marked still. Roll over: A restless minute. Walk to the kitchen: Awake minutes.

All still: what should I check?

Lower the thresholds or check the sensor is working.

All awake: what should I check?

Raise the thresholds, check the wrist strap is firm.

SD errors: what should I check?

Format FAT32 and check the chip select.

Battery drains early: what should I check?

Use a bigger cell or log less often.