Intro: Simple ECG and Heart-Rate Detector
NOTICE: This is not a medical device. This is for educational purposes only using simulated signals. If using this circuit for real ECG measurements, please ensure the circuit and the circuit-to-instrument connections are utilizing proper isolation techniques.
Today, we'll walk through basic electrocardiography (ECG) circuit design and create a circuit to amplify and filter your heart's electrical signal. Then, we can measure heart rate using labVIEW software. Throughout the process, I'll give detailed instruction on elements of circuit design and why they occurred, as well as a little biology background. The title image is of my heart's electrical signal. By the end of this instructable, you will be able to measure yours too. Lets get started!
ECG is a useful diagnostic tool for medical professionals. It can be used to diagnose a multitude of heart conditions, from the basic heart attack (myocardial infarction), all the way to more advanced cardiac disorders, such as atrial fibrillation, that people may go a majority of their life without noticing. Every heartbeat, your autonomic nervous system is working hard to make your heart beat. It sends electrical signals to the heart, which travel from the SA node to the AV node, and then to the left and right ventricles synchronously, and finally from the endocardium to the epicardium and purkinje fibers, the hearts last line of defense. This complex biological circuit can have issues anywhere along its path, and the ECG can be used to diagnose these issues. I could talk biology all day, but there's already a book on the topic, so check out "ECG Diagnosis in Clinical Practice", by Nicholas Peters, Michael Gatzoulis, and Romeo Vecht. This book is extremely easy to read and demonstrates the amazing utility of an ECG.
To create the ECG, you will need the following components or acceptable substitutions.
- For Circuit Design:
- OP Amps x 5
- Alligator Clips, or other methods of stimulating and measuring
- BNC cables
- Function Generator
- DC Power Supply, or batteries if you're handy
- DAQ Board
- Alligator Clips, or electrode leads
*I put a warning note above, and I'll discuss the dangers of electrical components to the human body a little bit more. Do not connect this ECG to yourself unless you have ensured that you are using proper isolation techniques. Connecting power-main powered devices such as power supplies, oscilloscopes, and computers directly to the circuit can cause large currents to flow through the circuit in the case of a power surge. Please isolate the circuit from the power main by using battery power and other isolation techniques.
Next' I'll discuss the fun part; Circuit design elements!
Step 1: Circuit Design Specifications
Now I'll talk circuit design. I won't discuss circuit schematics, as those will be given after this section. This section is for people who want to understand why we chose the components we did.
The Image above, taken from my lab manual at Purdue University, gives us near-everything we need to know to design a basic ECG circuit. This is the frequency composition of an unfiltered ECG signal, with a generic "amplitude" (y axis) referring to a dimensionless number for comparative purposes. Now lets talk design!
A. Instrumentation Amplifier
The instrumentation amplifier will be the first stage in the circuit. This versatile tool buffers signal, reduces common mode noise, and amplifies signal.
We are taking a signal from the human body. Some circuits allow you to use your measurement source as a power supply, as there's adequate charge available with no risk of damage. However, we don't want to hurt our human subjects, so we need to buffer the signal we are interested in measuring. An instrumentation amplifiers allows you to buffer biological signals, since the Op Amp-Inputs have theoretically infinite impedance (this is not the case, in practice, but impedance is usually sufficiently high) which means that no current (theoretically) can flow into the input terminals.
The human body has noise. Signals from muscles can cause this noise to manifest itself in ECG signals. To reduce this noise, we can use a difference amplifier to reduce common-mode noise. Essentially, we want to subtract out the noise that is present in your forearm muscles at two electrode placements. An Instrumentation amplifier includes a difference amplifier.
Signals in the human body are small. We need to amplify these signals so they can be measured at an appropriate resolution using electrical measurement devices. An instrumentation amplifier provides the gain necessary to do this. See the attached link for more information on instrumentation amplifiers.
B. Notch Filter
Power lines in the U.S. produce a "mains hum" or "power line noise" at exactly 60 Hz. In other countries this occurs at 50 Hz. We can see this noise by looking at the image above. Since our ECG signal is still somewhat within the band of interest, we want to remove this noise. To remove this noise, a notch filter can be used, which reduces gain at frequencies within the notch. Some people may not be interested in the higher frequencies on the ECG spectrum, and may choose to create a low pass filter with a cutoff below 60 Hz. However, we wanted to err on the safe side and receive as much of the signal as possible, so a notch filter and low pass filter with a higher cutoff frequency was chosen instead.
See the attached link for more information on notch filters.
C. Second-Order Butterworth VCVS Low-Pass Filter
The frequency composition of an ECG signal only extends so far. We want to eliminate signals at higher frequencies, since for our purposes, they are simply noise. Signals from your cell phone, blue tooth device, or laptop are everywhere, and these signals would cause unacceptable noise in the ECG signal. They can be eliminated with a Butterworth Low-Pass filter. Our chosen cutoff frequency was 220 Hz, which in hindsight, was a little high. If i were to create this circuit again, I would choose a cutoff frequency much lower than that, and maybe even experiment with a cutoff frequency below 60 Hz and use a higher order filter instead!
This filter is second order. This means that gain "rolls off" at a rate of 40 db/decade instead of 20 db/decade like a first order filter would. This steeper roll off provides greater mitigation of high frequency signal.
A Butterworth filter was chosen since it is "maximally flat" in the pass band, meaning that there is no distortion within the pass band. If you're interested, this link contains awesome information for basic second-order filter design:
Now that we've talked circuit design, we can start construction.
Step 2: Construct the Instrumentation Amplifier
This circuit will buffer input, subtract common mode noise, and amplify signal at a gain of 100. The circuit schematic and accompanying design equations are shown above. This was created using OrCAD Pspice designer and simulated using Pspice. The schematic comes out a little blurry when copied from OrCAD, so I apologize for this. I've edited the image to hopefully make some of the resistor values a little clearer.
Remember that when creating circuits, reasonable resistance and capacitance values should be chosen such that the practical impedance of the voltage source, practical impedance of the voltage measurement device, and physical size of the resistors and capacitors are taken into account.
The design equations are listed above. Initially, we wanted the gain of the instrumentation amplifier to be x1000, and we created this circuit so that we could amplify simulated signals. However, when attaching it to our body, we wanted to reduce the gain to 100 for safety reasons, since breadboards aren't exactly the most stable circuitry interfaces. This was done by hot-swapping resistor 4 to be reduced by a factor of ten. Ideally, your gain out of each stage of the instrumentation amplifier would be the same, but instead our gain became 31.6 for stage 1 and 3.16 for stage 2, giving a gain of 100. I've attached the circuit schematic for a gain of 100 instead of 1000. You will still see simulated and biological signals perfectly fine with this level of gain, but it may not be ideal for digital components with a low resolution.
Note, in the circuit schematic, I have the words "ground input" and "positive input" drawn in orange text. I accidentally placed the function input where the ground is supposed to be. Please put ground where "ground input" is noted, and the function where "positive input" is noted.
- Stage 1 gain - 31.6
- Stage 2 gain - 3.16 for safety reasons
Step 3: Construct the Notch Filter
This notch filter eliminates 60 Hz noise from U.S. powerlines. Since we want this filter to notch at exactly 60 Hz, using the correct resistance values is critical.
The design equations are listed above. A quality factor of 8 was used, which results in a steeper peak at the attenuation frequency. A center frequency (f0) of 60 Hz was used, with a bandwidth (beta) of 2 rad/s to provide attenuation at frequencies slightly deviating from the center frequency. Recall that the greek letter omega (w) is in units of rad/s. To convert from Hz to rad/s, we must multiply our center frequency, 60 Hz, by 2*pi. Beta is also measured in rad/s.
- Values for Design equations
- w0 = 376.99 rad/s
- Beta (B) = 2 rad/s
- Q = 8
Step 4: Construct the Low-Pass Filter
A low-pass filter is used to eliminate high frequencies we are not interested in measuring, such as cell phone signals, bluetooth communication, and WiFi noise. An active second-order VCVS Butterworth filter provides a maximally flat (clean) signal in the band pass region with a roll off of -40 db/decade in the attenuation region.
The design equations are listed above. These equations are a bit long, so remember to check your math! Note that b and a values are carefully chosen to provide flat signal in the bass region and uniform attenuation in the roll off region. For more information on how these values come about, refer to the link in step 2, section C, "low pass filter".
The specification for C1 is pretty ambiguous, as it's simply less than a value based off C2. I calculated it to be less than or equal to 22 nF, so I chose 10 nF. The circuit worked fine, and the -3 db point was very close to 220 Hz, so I wouldn't worry about this too much. Again recall the angular frequency (wc) in rad/s is equal to cutoff frequency in Hz (fc) * 2pi.
- Design Constraints
- K (gain) = 1
- b = 1
- a = 1.4142
- Cut off frequency - 220 Hz
The cutoff frequency of 220 Hz seemed a little high. If i were to do this again, I'd likely make it closer to 100 Hz, or even mess around with a higher order low pass with a cutoff of 50 Hz. I encourage you to try out different values and Schematics!
Step 5: Connect the Instrumentation Amplifier, Notch Filter, and Low Pass Filter
Now, simply connect the output of the instrumentation amplifier to the input of the notch filter. Then connect the output of the notch filter to the input of the low pass filter.
I've also added bypass capacitors from the DC power supply to ground to eliminate some noise. These capacitors should be the same value for each Op-Amp and at least 0.1 uF, but other than that, feel free to use any reasonable value.
I tried to use a little envelope circuit to "smooth" the noisy signal, but it wasn't working as intended, and I was low on time, so I scrapped this idea and used digital processing instead. This would be a cool extra step if you're curious!
Step 6: Power Up the Circuit, Input a Waveform, and Measure
- Connect the function generator to the instrumentation amplifier.
- Positive Clip to the lower Op-Amp in the instrumentation amplifier diagram
- Negative clip to ground.
- Short the input of the upper Op-Amp in the instrumentation amplifier diagram to ground. This will provide a reference for the incoming signal. (In biological signals, this input will be an electrode with the intention of reducing common-mode noise.)
- Connect the positive clip of the oscilloscope to the output at the final stage (output of low pass filter).
- positive clip to output at final stage
- negative clip to ground
- Connect your DC power supply to the rails, ensuring that the each Op-Amp power input is shorted to the rail it corresponds to.
- Connect your DC power supply's earth ground to a the remaining bottom rail, providing a reference for you signal.
- short the bottom rail ground to the top rail ground, which should allow you to clean up the circuit
Start Inputting a wave and use the oscilloscope to take measurements! If your circuit is working as intended, you should be seeing a gain of 100. This would mean that the peak to peak voltage should be 2V for a 20 mV signal. If you're function generator as a fancy cardiac waveform, try inputting that.
Mess around with frequencies and inputs to ensure that your filter is working properly. Try Testing each stage individually, and then test the circuit as a whole. I've attached a sample experiment where I analyzed the function of the notch filter. I noticed sufficient attenuation from 59.5 Hz to 60.5 Hz, but I would have preferred to have a bit more attenuation at the 59.5 and 60.5 Hz points. Nevertheless, time was of the essence, so I moved on and figured I could remove the noise digitally later. Here are some questions you want to consider for your circuit:
- Is the gain 100?
- Check the gain at 220 Hz. Is it -3 db or close to that?
- Check the attenuation at 60 Hz. Is it sufficiently high? Does it still provide some attenuation at 60.5 and 59.5 Hz?
- How fast does your filter roll off from 220 Hz? Is it -40 db/decade?
- Is there any current going into either of the inputs? If so, this circuit is not suitable for human measurement, and something is likely wrong with your design or components.
If you're circuit is working as intended, then you're ready to move on! If not, you have some troubleshooting to do. Check the output of each stage individually. Ensure your Op-Amps are powered and functional. Examine the voltage at each node until you have found the issue with the circuit.
Step 7: LabVIEW Heart Rate Measurement
LabVIEW will allow us to measure heart-rate using a logic-block diagram. Given more time, I would have preferred to digitize the data myself and create code that would determine heart-rate, as it wouldn't require computers with labVIEW installed and a hefty DAQ board. Additionally, numerical values in labVIEW did not come intuitively. Nevertheless, learning labVIEW was a valuable experience, as using block diagram logic is much easier than having to hard-code your own logic.
There isn't much to say for this section. Connect the output of your circuit to the DAQ board, and connect the DAQ board to the computer. Create the circuit displayed in the following image, hit "run", and start collecting data! Make sure your circuit is receiving a waveform.
Some important settings in this are:
- a sampling rate of 500 Hz and a window size of 2500 units means that we are capturing 5 seconds worth of data inside the window. This should be sufficient to see 4-5 heartbeats at rest, and more during exercise.
- A peak detected of 0.9 was sufficient to detect heart rate. Although this looks like it checks out graphically, it actually took quite a bit of time to arrive upon this value. You should mess around with this until you are accurately calculating heartbeat.
- A width of "5" seemed to be sufficient. Again, this value was tinkered with and did not seem to make intuitive sense.
- The numeric input to calculate heart rate uses a value of 60. Every time a heartbeat is indicated, it goes through the lower level circuit and returns a 1 every time the heart beats. If we divide this number by 60, we are essentially saying "divide 60 by the number of beats calculated in the window". This will return your heart rate, in beats/min.
The attached image is of my own heartbeat in labVIEW. It determined that my heart was beating at 82 BPM. I was pretty excited to finally have this circuit working!
Step 8: Human Measurement
If you've proved to yourself that your circuit is safe and functional, then you can measure your own heartbeat. Using 3M measurement electrodes, place them in the following locations and connect them to the circuit. The wrist leads go on the inside of your wrist, preferably where there is little to no hair. The ground electrode goes on the bony part of your ankle.Using alligator clips, connect the positive lead to the positive input, negative lead to the negative input, and ground electrode to the ground rail (pay close attention that its not the negative power rail).
One Last Repeat Note: "This is not a medical device. This is for educational purposes only using simulated signals. If using this circuit for real ECG measurements, please ensure the circuit and the circuit-to-instrument connections are utilizing proper isolation techniques. You assume the risk of any damage incurred."
Ensure that your oscilloscope is properly connected. Ensure that no current is flowing into the op amp, and that the ground electrode is attached to ground. Ensure your oscilloscope window sizes are correct. I observed a QRS complex of roughly 60 mV and used a 5s window. Attach the alligator clips to their respective positive, negative, and ground electrodes. You should begin to see an ECG waveform after a couple of seconds. Relax; don't make any movements as the filter can still pick up muscle signals.
With proper circuit setup, you should be seeing something like that output in the previous step! This is your very own ECG signal. Next I'll touch on processing.
NOTE: You'll see different 3-electrode ECG setups online. These would work too, but they may give inverted wave forms. With the way the differential amplifier is setup in this circuit, this electrode configuration provides a traditional positive-QRS complex waveform.
Step 9: Signal Processing
So you've hooked yourself up to the oscilloscope, and you can see the QRS complex, but the signal still looks noisy. Probably something like the first image in this section. This is normal. We are using a circuit on an open breadboard, with a bunch of electrical components that basically act as small antennas. DC power supplies are notoriously noisy, and no RF shielding is present. Of course the signal will be noisy. I made a brief attempt at using an envelope tracing circuit, but ran out of time. It's easy to do this digitally, though! Simply take a moving average. The only difference between the grey/blue graph and the black/green graph is that the black/green graph uses a moving average of voltage in a 3 ms window. This is such a small window compared to the time between beats, but it makes the signal look so much smoother.
Step 10: Next Steps?
This project was cool, but something can always be done better. Here are some of my thoughts. Feel free to leave yours below!
- Use a lower cutoff frequency. This should eliminate some of the noise present in the circuit. Maybe even play around with using just a low pass filter with a steep roll off.
- Solder the components and create something permanent. This should reduce the noise, its cooler, and its safer.
- Digitize the signal and output it on your own, eliminating the need for a DAQ board and allowing you to write code that will determine heart beat for you instead of needing to use LabVIEW. This will allow the everyday user to detect heartbeat without requiring a powerful program.
- Create a device that will display the input directly on a screen (hmmmm raspberry pi and screen project?)
- Use components that will make the circuit smaller.
- Create an all-in-one portable ECG with display and heart rate detection.
This concludes the instructable! Thank you for reading. Please leave any thoughts or suggestions below.