An AD8232 ECG on an Arduino: the heartbeat in the Serial Plotter
Beginner
Or see every lesson.
- 505parts on the bench
- 45boards running now
- 1.00xreal time, on every board
The AD8232 is a small red board that measures the electrical signal of your heart, the same signal a hospital ECG draws. Three sticky electrodes go on your chest or arms, the board amplifies the tiny signal 1100 times, and one wire carries it to an Arduino analog pin.
The circuit above is an Uno with the AD8232 on A0, its two lead-off pins on 10 and 11, and its power from the Uno’s 3.3 V pin. Press Run, open the Plot pane, and the trace draws itself: a small bump, a tall spike, a dip and a broad hump, again and again. The heartbeat in the simulator is synthetic, built from the shape of a healthy heartbeat, so you can drag its rate and take electrodes off without anyone being wired to anything.
What you need
- An Arduino Uno (or any 5 V or 3.3 V Arduino)
- An AD8232 heart monitor board (SparkFun SEN-12650 or a clone)
- The three-lead electrode cable and sticky electrodes
- Jumper wires
Wiring
| Part and pin | Goes to | Note |
|---|---|---|
| AD8232 3.3V | 3.3 V | Not 5 V: the chip is rated to 3.5 V |
| AD8232 GND | GND | |
| AD8232 OUTPUT | A0 | The ECG, centered on half of 3.3 V |
| AD8232 LO+ | Pin 10 | High when the LA electrode is off |
| AD8232 LO- | Pin 11 | High when the RA electrode is off |
| AD8232 SDN | Not connected | The board holds it high, so the chip runs |
What the trace shows
Each heartbeat is a wave of electricity across the heart muscle, and the electrodes pick up a millivolt or so of it at the skin. The AD8232 multiplies that by 1100 and adds half its supply, so OUTPUT sits around 1.65 V and swings about a volt above it on every beat.
An Uno reads A0 against its own 5 V, so the middle of the trace is about 338 on the 0 to 1023 scale, not 512, and the tall spike reaches about 560. The spike is the R wave. Before it is the small P wave, and after it the S dip and the broad T wave. At the starting 72 beats a minute the spikes come 833 ms apart.
The sketch
It is short. Every loop reads the two lead-off pins first. If either is HIGH an electrode is off, and the sketch prints a "!" instead of a number. Otherwise it prints analogRead(A0). One number per line is exactly what the Serial Plotter wants.
At 9600 baud a line like "338" takes about 5 ms to send, so the plot gets about 190 points a second: plenty for a heartbeat, which has nothing much faster than the 25 ms of the R wave.
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));
}
delay(1);
}Open this sketch in the editorThe code alone: the editor places the board and the parts it can read from the sketch.
Leads off
Press the leads key on the board. With the LA electrode off, LO+ goes HIGH and the sketch prints "!" lines. The amplifier saturates too: OUTPUT goes to the top of its range. Press again for RA off, which raises LO- instead and sends OUTPUT to the bottom. That is why the sketch checks both pins before it trusts a reading.
The Uno’s serial buffer holds about a dozen readings, so the first "!" shows up a moment after the electrode comes off.
Heart rate from the trace
To count beats, look for the R wave rising through a threshold well above the middle, like 480. Time from one crossing to the next is the beat interval, and 60000 divided by it is the heart rate. The P and T waves never get that high, so they do not count twice.
Drag the noise slider up and the trace gets fuzzy and wanders slowly, the way a real one does when the person moves or breathes. The R wave still stands well above it.
Common mistakes
Powering the board from 5 V. Use 3.3 V.
Expecting the middle at 512. It is half of the board’s own 3.3 V supply, about 338 on an Uno.
A big delay() in loop(). With delay(100) the plot gets ten points a second and the spikes vanish between them.
Moving while wearing the electrodes. Muscle signals are bigger than the heart’s; sit still.
Treating it as a medical device. It is not, and neither is the real board.
Open this circuit in the editor. Arduino Uno: an AD8232 ECG, wired as it runs at the top of this page. Change a part or a value and run it again.
Open the circuitQuestions
How do I connect an AD8232 to an Arduino?
OUTPUT to A0, LO+ to pin 10, LO- to pin 11, 3.3V to the 3.3 V pin and GND to GND. Leave SDN unconnected.
Do I need a library for the AD8232?
No. The board puts out an analog voltage, so analogRead(A0) and two digitalRead calls are the whole sketch, the one above.
What do LO+ and LO- do on the AD8232?
They are lead-off outputs. LO+ goes HIGH when the LA electrode is off and LO- when the RA electrode is off, so a sketch can skip readings that are not a heartbeat.
Why does my AD8232 reading sit around 338 and not 512?
The board centers its output on half of its 3.3 V supply, 1.65 V, and an Uno reads that against 5 V: about 338.
Is the heartbeat in the simulator real?
No. It is synthetic, built from the shape of a healthy ECG, with a rate and noise you can change.
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.