Projects / Heart rate breathing coach

Heart rate breathing coach

Intermediate2.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANOHEART91 bpm

    Wire the pulse sensor

    Print raw values.

  2. NANO

    Find the beat

    Adaptive threshold.

  3. NANO

    Print the gaps

    Check the bpm.

  4. NANO

    Add the variability number

    RMSSD over 20 beats.

  5. NANO19.7 mA

    Wire the ring

    Test a colour.

  6. NANO

    Add the breathing light

    One breath every 11 s.

  7. NANO

    Try a 5 minute session

    Compare start and end.

In short

How do you build a heart rate breathing coach?

A pulse sensor measures heart rate variability and a light guides your breathing to calm it. You need Arduino Nano, Heart rate sensor (pulse sensor), WS2812B ring 16 LEDs and Resistor 330 Ω. It takes 7 steps and about 150 minutes.

Level
Intermediate
Time
About 150 min
Parts
4

Parts list

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

Wiring

FromToWhy
Pulse sensor signalNano A0Analog input
Pulse sensor plus and minusNano 5V and GNDPower
LED ring DIN330 ohm resistor to Nano D6Data
LED ring 5V and GNDNano 5V and GNDPower (keep brightness low)
Nano USBComputer or a power bankPower

Code

Between heartbeats the time gap is never perfectly steady, and how much it varies is called heart rate variability. Slow, even breathing at about five to six breaths a minute tends to make the heartbeat rise and fall with the breath, and many people find that calming. The light ring swells and fades once every eleven seconds: breathe in as it fills, breathe out as it empties. The pulse sensor on a fingertip gives a rough beat time; the sketch keeps the last twenty gaps and prints a simple variability number (RMSSD), so you can watch it over a few minutes. A finger sensor is not accurate and movement spoils it, so use it for curiosity and relaxation. This is not a medical device: it cannot diagnose or monitor any condition, and anyone with a heart condition or worrying symptoms should talk to a doctor, not rely on this. Keep the ring dim, and do not use it if flashing or pulsing lights bother you. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).

Runs on a virtual Arduino Nano 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).

// Breathing coach: a slow light guides the breath (about 5.5 breaths a minute) while the pulse sensor measures beat-to-beat variability.
// Library: Adafruit NeoPixel. Board: Arduino Nano. For relaxation and curiosity only: it is not a medical device.
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel ring(16, 6, NEO_GRB + NEO_KHZ800);
const int PULSE_PIN = A0;
const unsigned long BREATH_MS = 11000UL;       // 5.5 s in, 5.5 s out
const int N = 20;
unsigned long ibi[N]; int ibiCount = 0;
int sigMin = 1023, sigMax = 0, thresh = 512;
unsigned long lastBeat = 0, lastSample = 0, lastDraw = 0, lastDecay = 0;
bool above = false;
float rmssd() {
  if (ibiCount < 5) return 0;
  float sum = 0; int n = 0;
  for (int i = 1; i < ibiCount; i++) { long d = (long)ibi[i] - (long)ibi[i - 1]; sum += (float)d * d; n++; }
  return sqrt(sum / n);
}
void setup() {
  Serial.begin(115200);
  ring.begin(); ring.setBrightness(40); ring.show();
}
void loop() {
  if (millis() - lastSample >= 4) {                          // 250 samples a second
    lastSample = millis();
    int s = analogRead(PULSE_PIN);
    if (s < sigMin) sigMin = s;
    if (s > sigMax) sigMax = s;
    thresh = (sigMin + sigMax) / 2;
    if (!above && s > thresh + 20 && millis() - lastBeat > 300) {
      above = true;
      unsigned long now = millis();
      if (lastBeat > 0) {
        unsigned long gap = now - lastBeat;
        if (gap > 400 && gap < 1500) {                       // keep believable beats only (40 to 150 bpm)
          if (ibiCount == N) { for (int i = 1; i < N; i++) ibi[i - 1] = ibi[i]; ibiCount--; }
          ibi[ibiCount++] = gap;
          Serial.print("bpm "); Serial.print(60000UL / gap); Serial.print("  rmssd ms "); Serial.println(rmssd(), 0);
        }
      }
      lastBeat = now;
    }
    if (s < thresh - 20) above = false;
  }
  if (millis() - lastDecay > 2000) { lastDecay = millis(); sigMin += 2; sigMax -= 2; if (sigMin > sigMax) { sigMin = 1023; sigMax = 0; } }
  if (millis() - lastDraw > 40) {                            // draw the breathing light
    lastDraw = millis();
    float phase = (millis() % BREATH_MS) / (float)BREATH_MS;
    float level = 0.5 - 0.5 * cos(phase * 2 * 3.14159);      // rises then falls once per breath
    int lit = 1 + (int)(level * 15);
    for (int i = 0; i < 16; i++) ring.setPixelColor(i, i < lit ? ring.Color(0, 40 + level * 120, 120) : 0);
    ring.show();
  }
}

Test checklist

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

CheckExpected
Finger on the sensorSteady bpm on Serial
Resting rateWithin 5 bpm of a manual count
Breath with the lightVariability number tends to rise
Finger movedReadings briefly jump

If it does not work

No beats found

Press the finger gently and keep still.

Wild bpm

Shield the sensor from room light.

Ring too bright

Lower the brightness value.

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

Ask Lark AI

Common questions

What parts do I need for the heart rate breathing coach?

Arduino Nano, Heart rate sensor (pulse sensor), WS2812B ring 16 LEDs and Resistor 330 Ω.

How long does the heart rate breathing coach take to build?

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

How do I know the heart rate breathing coach works?

Finger on the sensor: Steady bpm on Serial. Resting rate: Within 5 bpm of a manual count. Breath with the light: Variability number tends to rise.

No beats found: what should I check?

Press the finger gently and keep still.

Wild bpm: what should I check?

Shield the sensor from room light.

Ring too bright: what should I check?

Lower the brightness value.