Heart rate variability (HRV) is the small variation in time between consecutive heartbeats. A healthy heart at rest does not beat like a metronome: one interval might be 910 ms, the next 940 ms, the next 925 ms. Those few milliseconds say a lot about the autonomic nervous system, which is why HRV shows up in everything from sleep tracking to clinical research.

The standard way to measure it is an electrocardiogram (ECG). Electrodes pick up the electrical signal of each beat, and the sharp R peak of each QRS complex gives you a very precise timestamp.

There is another way. Every time the heart contracts, the chest wall moves by a tiny amount. An accelerometer pressed against the sternum can pick that up. This is called a seismocardiogram (SCG), and one of its waves marks the moment the aortic valve opens (AO). If you can find that point on every beat, you get a second set of beat-to-beat intervals without any electrodes.

The question behind Health Monitor is whether those intervals are good enough for HRV. To answer it, I built a chest-worn board that records both signals at the same time, detects beats in both on-device, and computes HRV from each one separately. The ECG is used as the reference. The SCG is what is being tested.

The Health Monitor Rev2 board, a green PCB with an STM32 microcontroller, a USB-C port, and a 3.5 mm jack for the ECG leads
Health Monitor Rev2.

This is not a medical device. It is a research and learning project. While electrodes are attached, it should only be powered from a battery (an unplugged laptop works) or through a USB isolator. Never from a mains-powered PC.

Two boards

Health Monitor was my second PCB project. The first was an Arduino shield with a GNSS module and an IMU, so going from that to a custom microcontroller board with an analog ECG front end was a big jump.

The first revision never made it to testing. Before I powered it up, I found that the AD8232 ECG front end was wired incorrectly: a capacitor was missing between two of its pins, and a resistor and capacitor were connected wrong on its reference input. The filtering would most likely not have worked, so I abandoned the board.

Rev2 fixed the AD8232 circuit, added USB-C, and moved components to the bottom layer. It has three main parts:

  • An STM32U595 microcontroller running bare metal C.
  • An AD8232 for the ECG.
  • An ST1VAFE6AX 6-axis IMU, whose accelerometer records the SCG.

In hindsight, the AD8232 wasn’t necessary. The ST1VAFE6AX has its own analog front end for biopotential signals (ST calls it vAFE), sampled on the same chip as the accelerometer. I went with the AD8232 because its datasheet showed much more clearly which passive components it needs and where they go, which mattered a lot for my second ever PCB. That choice comes back later.

One clock for both signals

Comparing beat timing between two signals only works if both are sampled on the same clock. Otherwise, a drift of a few milliseconds would look like a disagreement between the signals.

The IMU’s data-ready interrupt triggers the DMA transfers for both the IMU (over SPI) and the ECG (through the ADC). So every ECG sample and every SCG sample is taken at the same instant, at a nominal 480 Hz (about 471 Hz measured). Each sample also gets a timestamp from a timer clocked by the board’s crystal, and every beat time and HRV metric is computed from those timestamps instead of the nominal rate.

Finding the beats

Three pipelines run on the device.

ECG. Beats are detected with Pan-Tompkins++, a refined version of the classic Pan-Tompkins algorithm: bandpass, derivative, squaring, smoothing, a moving window integrator, and adaptive thresholds with search-back for missed beats. Each detected beat is then timed at the apex of the R peak on the raw ECG, refined between samples.

SCG. The accelerometer’s Z axis (front to back through the chest) is filtered between 7 and 30 Hz. During the first 10 seconds, the firmware builds a template of a single heartbeat. After that, it slides the template along the signal using normalized cross-correlation (NCC) and marks the AO wherever the match peaks. This is based on the template matching method by Parlato et al. The template is refreshed every 60 seconds to follow slow changes in the signal, and replaced if it stops matching beats.

Respiration. The same accelerometer signal, low-passed and decimated to 10 Hz, goes through a 30 s autocorrelation to estimate breathing rate. This one does not work reliably yet.

From each stream’s beats, the firmware computes RMSSD, SDNN, pNN50, SD1, and SD2 over 60 s windows, after removing intervals that are more than 25% off the local median (missed, extra, or premature beats). Everything is streamed to a laptop over USB with CRC-32 checks, where a Python script plots and logs it.

Getting it wrong

In April, I recorded a 30 minute session at rest and compared the two. The result looked like this:

Metric Bias (SCG - ECG) 95% limits of agreement r
SDNN -2.0 ms [-6.0, +1.9] 0.988
RMSSD -6.3 ms [-16.2, +3.5] 0.378

SDNN, which captures slower variation, agreed well. RMSSD, which depends on the difference between consecutive intervals, did not. The SCG consistently reported less beat-to-beat variation than the ECG. My conclusion, which went into the README, was that the SCG pipeline could follow long-term variability but not fast beat-to-beat changes.

That conclusion was wrong, and so was much of the analysis behind it. Consecutive reports overlapped, so many beats were counted in more than one window, and the pairs were not independent measurements. More importantly, the ECG, which I was treating as ground truth, had timing errors of its own.

Fixing the reference

Pan-Tompkins++ finds a beat at the peak of its moving window integrator, and the firmware used that point as the beat time. But the integrator peak trails the actual R peak by about 110 ms on a normal beat and up to about 200 ms on a wide one. That delay is not constant, so the ECG intervals carried errors that had nothing to do with the heart, and the SCG was being compared against them.

The fixes:

  • After Pan-Tompkins++ accepts a beat, the firmware goes back and finds the R apex on the raw ECG, refined between samples, and uses that as the beat time.
  • Intervals more than 25% off the local median are removed before computing HRV, so missed, extra, and ectopic beats don’t distort the metrics.
  • Reports use contiguous, non-overlapping 60 s windows, and the SCG window covers the same cardiac cycles as the ECG window: it starts at the AO that follows the ECG window’s first R peak.
  • Several SCG fixes for template refresh and replacement.

One more bug

A later fix was in the SCG template refresh. In a single beat, the AO wave and the wave right after it can be of similar height. The firmware placed the AO at the template’s highest peak, so after a refresh it could pick the other wave and move every AO by about 60 ms.

The fix keeps the AO on the same wave. After a refresh, the AO goes to the new template’s peak closest to the old AO position, within 21 ms. That window isn’t tuned: it is just under half the ~60 ms spacing between the waves.

The result

From a session of about an hour, lying down (single subject, 60 paired 60 s windows, with the first 40 s of electrode settling left out):

Metric ECG (mean ± SD) Bias (SCG - ECG) 95% limits of agreement ICC(2,1)
RMSSD 32.7 ± 7.8 ms +0.9 ms [+0.0, +1.8] 0.991
SDNN 39.4 ± 12.1 ms +0.4 ms [+0.1, +0.8] 0.999
pNN50 12.2 ± 9.2 % +1.5 % [-3.1, +6.1] 0.959
Bland-Altman plots of SCG versus ECG for RMSSD, SDNN, and beat-to-beat intervals, all with narrow limits of agreement
Bland-Altman plots for RMSSD, SDNN, and individual beat-to-beat intervals.

Over 3,954 heartbeats, the ECG detected every beat (I reviewed them manually) and the SCG missed 3, with no false detections in either. Beat by beat, the SCG intervals agree with the ECG intervals with a bias of 0.0 ms and 95% limits of agreement of ±2.8 ms.

The remaining RMSSD bias is small, but it is consistently positive, and it has a physical cause. The delay between the R peak and the AO (63.4 ± 1.9 ms) gets slightly longer after longer heartbeats. That stretches the SCG intervals a little and accounts for about 90% of the bias. The delay is related to the pre-ejection period (PEP), the time between the electrical signal and the heart actually pushing blood out, which likely drives it. This recording can’t confirm that, and this board can’t measure PEP reliably either.

That is where the AD8232 comes back. Its filters delay the ECG on its way to the ADC, so every R peak is recorded a little later than it actually happened. For intervals, this barely matters: the shift is about the same on every beat, so it cancels when you subtract one beat time from the next. For PEP, it doesn’t cancel. PEP is an absolute delay between the ECG and the SCG, and any delay the filters add is mixed into it. Recording the ECG through the ST1VAFE6AX’s own front end, on the same chip and clock as the accelerometer, might have made it possible to measure PEP as well.

What this doesn’t show

This is one person, in one session, lying still. That is the easiest possible case for an SCG. Movement, talking, other people’s chests, and irregular heart rhythms are all untested. The SCG also only reports HRV while the ECG is producing windows, so it hasn’t been evaluated on its own yet. And SDNN over 60 s is lower than the 5 minute SDNN that published norms use, so it shouldn’t be compared with them.

What’s next

  • Compute 5 minute SDNN and pNN50 on the device, so they’re comparable to published norms.
  • Get respiratory rate working reliably.
  • Combine more than one axis for respiration (PCA across the accelerometer axes, plus the gyroscope).

The schematics, firmware, and analysis scripts are all on GitHub.