Built-in project · Arduino Uno R3

Heartbeat monitor on an Arduino Uno

A Pulse Sensor on a fingertip, PulseSensor Playground finding the beats, and an OLED drawing the wave scrolling by with the heart rate over it. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

The parts retail for about $37. On Mokxi they're free.

Step by step, with the wiring and the common mistakes: Arduino Pulse Sensor: Heart Rate on an OLED

Heartbeat monitor · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Heartbeat monitor: a Pulse Sensor and an SSD1306 OLED.
//
//   A0       the Pulse Sensor's S (purple); VCC and GND from the rails
//   A4, A5   the OLED's SDA and SCL (address 0x3C)
//   pin 13   the Uno's own LED, blinking with each beat
//
// PulseSensor Playground reads A0 every 2 ms on Timer2 and finds the beats,
// so loop() only has to draw. Every 20 ms it takes the latest sample and
// plots it one column further along the bottom of the screen, scaled from
// the 0 to 1023 of the converter to the 48 rows under the heart rate; at the
// right edge it starts again from the left. The screen is sent every fifth
// column, since a whole frame over I2C takes a while.

#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const int PULSE_PIN = A0;
const int LED = 13;
const int THRESHOLD = 550;

Adafruit_SSD1306 display(128, 64, &Wire, -1);
PulseSensorPlayground pulseSensor;

int column = 0;
int lastY = 40;
int bpm = 0;

void header() {
  display.fillRect(0, 0, 128, 16, SSD1306_BLACK);
  display.setCursor(0, 0);
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  if (bpm > 0) {
    display.print(bpm);
    display.print(" BPM");
  } else {
    display.print("-- BPM");
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: heartbeat monitor");
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("no OLED: check SDA, SCL and power");
  }
  display.clearDisplay();
  header();
  display.display();
  pulseSensor.analogInput(PULSE_PIN);
  pulseSensor.blinkOnPulse(LED);
  pulseSensor.setThreshold(THRESHOLD);
  pulseSensor.begin();
}

void loop() {
  if (pulseSensor.sawStartOfBeat()) {
    bpm = pulseSensor.getBeatsPerMinute();
    if (bpm > 0) {
      Serial.print("beat: ");
      Serial.print(bpm);
      Serial.print(" BPM ibi ");
      Serial.println(pulseSensor.getInterBeatIntervalMs());
    }
    header();
  }

  // One column of the wave: 0..1023 onto rows 63 (low) to 16 (high).
  int sample = pulseSensor.getLatestSample();
  int y = 63 - (int)((long)sample * 47 / 1023);
  display.drawFastVLine(column, 16, 48, SSD1306_BLACK);
  if (column > 0) display.drawLine(column - 1, lastY, column, y, SSD1306_WHITE);
  lastY = y;
  column++;
  if (column >= 128) column = 0;
  if (column % 5 == 0) display.display();
  delay(20);
}

Parts list

4 parts, plus the jumper wires. They retail for about $37; on Mokxi they're free.

How it is wired

5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 5V; Pulse sensor pin VCC; OLED display, 128x64 pin VCC
  • Ground: Arduino Uno R3 pin GND; Pulse sensor pin GND; OLED display, 128x64 pin GND
  • Arduino Uno R3 pin A0; Pulse sensor pin S
  • Arduino Uno R3 pin A4; OLED display, 128x64 pin SDA
  • Arduino Uno R3 pin A5; OLED display, 128x64 pin SCL

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.