Built-in project · Arduino Uno R3

ECG heart monitor plot on an Arduino Uno

An AD8232 heart monitor on A0, its lead-off pins on 10 and 11, and the trace drawn live in the Plot pane. The heartbeat is synthetic: drag its rate or take an electrode off. The whole build, an Arduino Uno and 1 more part, 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.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

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

The circuit runs here once the page has loaded.

What you'll learn

  • Reading an analog sensor with analogRead
  • Digital inputs that flag a fault
  • Printing numbers to the serial plotter
  • Powering a sensor from 3.3 V
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

The AD8232 heart monitor board turns tiny electrical signals from your body into a voltage on its OUTPUT pin. That pin goes to A0, and the board runs from the Uno's 3.3 V pin. In the simulator the heartbeat is synthetic, so you can change the rate.

The two lead-off pins, LO+ and LO-, go HIGH when an electrode is not attached. The sketch checks them first. If either is HIGH it prints a !. Otherwise it prints the A0 reading, and the Plot pane draws those numbers as a trace.

Parts list

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

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Heart monitor (ECG), AD8232 pin LO-Arduino Uno R3 pin 11
Heart monitor (ECG), AD8232 pin LO+Arduino Uno R3 pin 10
Heart monitor (ECG), AD8232 pin 3.3VArduino Uno R3 pin 3V3
Heart monitor (ECG), AD8232 pin GNDArduino Uno R3 pin GND
Heart monitor (ECG), AD8232 pin OUTPUTArduino Uno R3 pin A0

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
// ECG on the Serial Plotter: an AD8232 heart monitor on A0.
//
// The board's OUTPUT goes to A0, LO+ to pin 10 and LO- to pin 11, and it
// runs from the Uno's 3.3 V pin. Each loop prints one reading, or a '!'
// when an electrode is off, and the Plot pane draws the trace.
// The heartbeat in the simulator is synthetic: drag the heart rate, or
// press the board's leads key to take an electrode off.

const int LO_PLUS = 10;
const int LO_MINUS = 11;

void setup() {
  Serial.begin(9600);
  pinMode(LO_PLUS, INPUT);
  pinMode(LO_MINUS, INPUT);
}

void loop() {
  if (digitalRead(LO_PLUS) == HIGH || digitalRead(LO_MINUS) == HIGH) {
    Serial.println('!');
  } else {
    Serial.println(analogRead(A0));
  }
  // A little rest, so the serial line keeps up.
  delay(1);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Opens serial at 9600 baud so numbers can go to the Plot pane. It also sets the two lead-off pins, LO_PLUS (10) and LO_MINUS (11), as inputs, since the sensor board drives them.

void setup() {
  Serial.begin(9600);
  pinMode(LO_PLUS, INPUT);
  pinMode(LO_MINUS, INPUT);
}

loop sketch.ino

Each pass checks both lead-off pins with digitalRead. If either reads HIGH, an electrode is off, so it prints !. If both are fine, it prints analogRead(A0), one number per line. A 1 ms delay keeps the serial line from falling behind.

void loop() {
  if (digitalRead(LO_PLUS) == HIGH || digitalRead(LO_MINUS) == HIGH) {
    Serial.println('!');
  } else {
    Serial.println(analogRead(A0));
  }
  // A little rest, so the serial line keeps up.
  delay(1);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The plot shows only exclamation marks and no trace This project
    An electrode is off. Press the board's leads key in the simulator to put it back on.
  • The trace is flat or missing This project
    Check that OUTPUT goes to A0 and that the board has both 3.3 V and ground.
  • The signal is flat, saturated, or very noisy. Heart monitor (ECG)
    Place the electrodes firmly on skin: right arm, left arm, and right leg as reference. Stay still, since movement and mains noise cause large artifacts. Run it from a battery if you can.
  • Readings are wrong when the leads are off. Heart monitor (ECG)
    Read the LO+ and LO- pins. When either is high, an electrode is not connected, and the output is meaningless. Ignore the signal then.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

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