DIY ECG with 1 op-amp

I made surprisingly good ECG from a single op-amp and 5 resistors! An ECG (electrocardiograph, sometimes called EKG) is a graph of the electrical potential your heart produces as it beats. Seven years ago I posted DIY ECG Machine on the Cheap which showed a discernible ECG I obtained using an op-amp, two resistors, and a capacitor outputting to a PC sound card’s microphone input. It didn’t work well, but the fact that it worked at all was impressive! It has been one of the most popular posts of my website ever since, and I get 1-2 emails a month from people trying to recreate these results (some of them are during the last week of a college design course and sound pretty desperate). Sometimes people get good results with that old circuit, but more often than not the output isn’t what people expected. I decided to revisit this project (with more patience and experience under my belt) and see if I could improve it. My goal was not to create the highest quality ECG machine I could, but rather to create the simplest one I could with emphasis on predictable and reproducible results. The finished project is a blend of improved hardware and custom cross-platform open-source software (which runs on Windows, Linux, and MacOS), and an impressively good ECG considering the circuit is so simple and runs on a breadboard! Furthermore, the schematics and custom software are all open-sourced on my github!

ECG_1470609065
my heartbeat recorded while filming the YouTube video shown below

Here’s a video demonstrating how the output is shown in real time with custom Python software. The video is quite long, but you can see the device in action immediately, so even if you only watch the first few seconds you will see this circuit in action with the custom software. In short, the amplifier circuit (described in detail below) outputs to the computer’s microphone and a Python script I wrote analyzes the audio data, performs low-pass filtering, and graphs the output in real time. The result is a live electrocardiograph!

The circuit is simple, but a lot of time and thought and experimentation went into it. I settled on this design because it produced the best and most reliable results, and it has a few nuances which might not be obvious at first. Although I discuss it in detail in the video, here are the highlights:

circuit

  • The output goes to the microphone jack of your computer.
  • There’s nothing special about the op-amp I used (LM741). A single unit of an LM324 (or any general purpose op-amp) should work just as well.
  • Resistor values were chosen because I had them on hand. You can probably change them a lot as long as they’re in the same ballpark of the values shown here. Just make sure R1 and R2 are matched, and R3 should be at least 10MOhm.
  • Do not use a bench power supply! “BAT+” and “BAT-” are the leads of a single 9V battery.
  • Note that the leg electrode is ground (same ground as the computer’s microphone ground)
  • R5 and R4 form a traditional voltage divider like you’d expect for an op-amp with a gain of about 50.
    • You’d expect R4 to connect to ground, but since your body is grounded, chest 2 is essentially the same
    • R3 must be extremely high value, but it pulls your body potential near the optimal input voltage for amplification by the op-amp.
    • R1 and R2 split the 9V battery’s voltage in half and center it at ground, creating -4.5V and +4.5V.
  • altogether, your body stays grounded, and the op-amp becomes powered by -4.5V and +4.5V, and your body is conveniently near the middle and ready to have small signals from CHEST1 amplified. Amplification is with respect to CHEST2 (roughly ground), rather than actual ground, so that a lot of noise (with respect to ground) is eliminated.
DIY ECG made from 1 op-amp, 5 resistors, a 9V battery, and 3 penny electrodes

For those of you who would rather see a picture than a schematic, here’s a diagram of how to assemble it graphically. This should be very easy to reproduce. Although breadboards are typically not recommended for small signal amplification projects, there is so much noise already in these signals that it doesn’t really matter much either way. Check out how good the signals look in my video, and consider that I use a breadboard the entire time.

design

The most comfortable electrodes I used were made for muscle simulators. A friend of mine showed me some muscle stimulator pads he got for a back pain relief device he uses. As soon as I saw those pads, I immediately thought they would be perfect for building an ECG! They’re a little bit expensive, but very comfortable, reusable, last a long time, and produce brilliant results. They also have 3.5 mm (headphone jack) connectors which is perfect for DIY projects. On Amazon.com you can get 16 pads for $11 with free shipping. I decided not to include links, because sometimes the pads and cords are sold separately, and sometimes they have barrel connectors and sometimes they have snap connectors. Just get any adhesive reusable electrodes intended for transcutaneous electrical nerve stimulation (TENS) that you can find! They should all work fine.

IMG_7576

You can make your own electrodes for $0.03! Okay that’s a terrible joke, but it’s true. I made not-awful electrodes by soldering wires to copper pennies, adding strength by super-gluing the wire to the penny, and using electrical tape to attach them to my chest. Unless you want a tattoo of an old guy’s face on your torso, wait until they cool sufficiently after soldering before proceeding to the adhesion step. I suspect that super gluing the penny to your chest would also work, but please do not do this. Ironically, because the adhesive pads of the TENS electrodes wear away over time, the penny solution is probably “more reusable” than the commercial electrode option.

I put pennies on wood to help them get hot as I solder to them.
I put pennies on wood to help them get hot as I solder to them.

 

penny electrodes match the minimalist style of this project
penny electrodes match the minimalist style of this project

 

this ECG was captures using penny electrodes
This ECG was captured using penny electrodes. It’s pretty darn good!

 

Notes on filtering: Why didn’t I just use a hardware low-pass filter?

  1. It would have required extra components, which goes against the theme of this project
  2. It would require specific value components, which is also undesirable for a junkbox project
  3. I’m partial to the Chebyshev filter, but getting an extremely sharp roll-off a few Hz shy of 50Hz would take multiple poles (of closely matched passive components) and not be as trivial as it sounds.

Notes on software: This a really cool use of Python! I lean on some of my favorite packages numpy, scipy, matplotlib, pyqrgraph, and PyQt4. I’ve recently made posts describing how to perform real-time data graphing in Python using these libraries, so I won’t go into that here. If you’re interested, check out my real-time audio monitor, notes on using PlotWidget, and notes on using MatPlotLib widget. I tried using PyInstaller to package this project into a single .EXE for all my windows readers who might want to recreate this project, but the resulting EXE was over 160MB! That’s crazy! It makes sense considering packagers like PyInstaller and Py2EXE work by building your entire python interpreter and all imported libraries. With all those fun libraries I listed above, it’s no wonder it came out so huge. It may be convenient for local quick-fixes, but not a good way to distribute code over the internet. To use this software, just run it in Python. It was tested to work with out-of-the-box WinPython-64bit-3.5.2.1 (not the Qt5 version), so if you want to run it yourself start there.

Notes on safety. How safe is this project? I’m conflicted on this subject. I want to be as conservative as I can (leaning on the side of caution), but I also want to be as realistic as possible. I’m going to play it safe and say “this may not be safe, so don’t build or use it”. As an exercise, let’s consider the pros and cons:

  • PROS:
    • It’s powered from a 9V battery which is safer than a bench power supply (but see the matching con).
    • The only connections to your body are:
      • leg – ground. you ground yourself all the time. using a wrist grounding strap is the same thing.
      • chest 1 – extremely high impedance. You’re attaching your chest to the high impedance input of an op-amp (which I feel fine with), and also to a floating battery through a 10MOhm resistor (which also I feel fine with)
      • chest 2 – raises an eyebrow. In addition to a high impedance input, you’re connected to an op-amp through a 100k resistor. Even if the op-amp were putting out a full 4.5V, that’s 0.045mA (which doesn’t concern me a whole lot).
    • I don’t know where to stick this, but I wonder what type of voltages / currents TENS actually provide.
  • CONS / WARNINGS:
    • It’s powered from a 9V battery. So are many stun guns.
    • If the op-amp oscillates, oscillations may enter your body. Personally I feel this may be the most concerning issue.
    • Small currents can kill. I found a curiously colored website that describes this. It seems like the most dangerous potential effect is induction of cardiac fibrillation, which can occur around 100mA.

Improving safety through optical isolation: The safety of this device may be improved (albeit with increased complexity) through the implementation of opto-isolators. I may consider a follow-up post demonstrating how I do this. Unlike digital signals which I’ve optically isolated before, I’ve never personally isolated analog signals. Although I’m sure there are fully analog means to do this, I suspect I’d accomplish it by turning it into a digital signal (with a voltage-to-frequency converter), pulsing the output across the optoisolator, and turning it back into voltage with a frequency-to-voltage converter or perhaps even a passive low-pass filter. Analog Devices has a good write-up about optical isolation techniques.

Do you have comments regarding the safety of this device? Write your thoughts concisely and send them to me in an email! I’d be happy to share your knowledge with everyone by posting it here.

Did you build this or a device similar to it? Send me some pictures! I’ll post them here.

Source code and project files: https://github.com/swharden/diyECG-1opAmp/

LEGAL: This website is for educational purposes only. Do not build or use any electrical devices shown. Attaching non-compliant electronic devices to your body may be dangerous. Consult a physician regarding proper usage of medical equipment.


     

25 thoughts on “DIY ECG with 1 op-amp

  1. Never thought a 741 could be enough, very well done, thanks a lot for publishing this!

    Regarding safety and the biggest cons:
    The schematic GND is connected to the computer GND by the audio cable. The computer GND does NOT have a galvanic isolation, so if you are unlucky enough, THIS SETUP MIGHT LITERALLY KILL YOU !!! No kidding, the battery instead of a power supply precaution is useless if the computer GND is not galvanic isolated.

    • YOUR COMPUTER MIGHT LITERALLY KILL YOU!!! If your computer chassis is charged instead of grounded, your computer is dangerous, regardless of whether or not this circuit is built. People commonly ground themselves to their computer chassis with a wrist-strap (I propose a penny on your leg, in my case), and if you expect to be grounded this way but the frame of your PC is actually charged and you go to ground yourself to something else (i.e., wall socket ground) a dangerous situation will occur. The danger is using your computer chassis for a ground source and not a component of this ECG circuit. I would advise everyone to ensure they don’t attempt to ground themselves to anything other than the shield of their microphone cable if they build this.

  2. Scott,

    Thanks for an awesome article. I am just getting started with electronic circuits and would tremendously appreciate your thoughts on the following.

    If I were to extend this circuit to send the captured analog audio signal from Op-Amp to an ADC (Analog to Digital Converter) and then use Bluetooth to send the digital output wirelessly, Any pointers on what components might I use to keep the cost low?

    I know that I could probably use an Arduino Nano for ADC & Micro controller + a Bluetooth AddOn module to keep the circuitry simple. But this will add to the cost. My goal is to progress my learning by figuring out the possible circuitry rather than use Packaged solutions like Arduino.

    Is MCP3008 (10bit 8-channel ADC) an option? If so what would the rest of the circuit look like. I understand that there are probably numerous solutions and this is a bit open ended. But I am inspired by your minimalistic thinking.

    Thanks for your help.

  3. Scott, Thanks for the quick response. That’s definitely an interesting idea. Two problems to consider. Data loss/signal degradation due to local FM Radio Frequency overlaps and recognizing/handling data continuity loss. I guess the data continuity loss part could be handled in the receiving end of the software. Let me think over it a bot more.

    • If you’re really interested in making a wireless ECG, step one would be to improve the amplifier to an instrumentation amplifier to dramatically decrease noise and eliminate a lot of the processing required to have a quality signal, but those chips are much more expensive than a simple op-amp. Google for AD620 ECG (some of the pages should be my youtube too). If you use that you can read it directly via the ADC of a microcontroller. You can then send the ADC values via serial (USART) using a bluetooth serial module (cheap on ebay). It’s a cool idea, and doesn’t add too much cost to the circuit. Now I’m tempted to build one…

  4. Just ordered some 741’s on Ebay to try this myself.

    There’s one thing that’s bugging me though, I read about the grounding issue with computers. Since this old dump does not have grounded wall outlets (outside of the kitchen) and since the case is definitely charged, I’ll have to do this some other way.

    I’m thinking of hooking the output up to an Adafruit ADS1115 or ADS1015* I’ve got lying around, which in turn is hooked up to an Arduino. To prevent grounding issues, I am gonna use one of those adc’s to do a differential measurement between the output pin and ground of the 741-circuit, this way I do not have to hook up the ground of the 741 circuit to the ground of the Arduino.

    This should eliminate the grounding issue.

    * The ADS1115 has a 16 bit resolution, the 1015 12 bit, but the 1015 has a much higher sample rate. See https://learn.adafruit.com/adafruit-4-channel-adc-breakouts?view=all for more info.

    How high is the sample rate in the picture?

    • Hi Bart,

      Personally I’m not particularly concerned about the computer grousing issue. If you just use the device as it is pictured here, there is no problem. The danger is only present if you go out of your way to connect additional wires to your body connected to different forms of ground (perhaps something like a wall socket). This is obviously a bad idea. If the circuit is simply built as shown, there is no issue.

      If you want to perform a differential measurement, that should work too. The sad thing is that the beauty of this project is its simplicity using the high sample rate provided by the sound card, and realtime software which filters and graphs data coming into the sound card. My sound card is set to record 16-bit resolution at 44100 Hz. Although you could use an extremely highspeed high resolution ADC IC to accomplish this, the complexity rapidly begins to increase. The 4 channel ADC link you provided is limited to <4kHz. If you had a clean ECG signal it would be fine, but to get such a clean ECG signal you need to use a different circuit. Again, the key to the simplicity of this circuit is the ability to use the sound card (a high resolution high speed ADC) and powerful software.

      Good luck!!

  5. Great, idea, will try! I’ll try a few possible improvements:
    1. Using artificial ground, to save some power. I’ll use much higher resistors to divide the battery voltage, and buffer this half point voltage using the second channel of a dual opamp. LM358 typical current is 1mA for both channels at 9V.
    2. Shield the two chest wires. The shield will be connected to the same “LEG” common point. Without the shield, I am worried about the noise pickup.
    3. A capacitor in parallel with the 100k resistor, to limit the bandwidth.
    As for the safety, I think battery powered laptop is my best bet.
    My other worry is the soundcard frequency response at the low end. I don’t think it goes down to 1Hz. Will see how it goes.

  6. Scott,

    Before I do anything with ECG analysis I wanted to check my laptop’s sound card quality to capture the signal. I generated a 1000Hz Sine wave from my Phone Mobile app and passed that as input to the laptop sound card mic in (4-pole audio pin).

    When I captured the incoming audio using Audacity and looked at the Frequency Spectrum here is waht it looked like.

    http://i.imgur.com/N77vEkF.png

    I captured the Audio signal in Audacity at 8000 Hz sample rate 16-bit mono. As you can see there is a Spike at 1000 Hz as expected but I see a lot of data at different frequencies. Is the rest noise from all other sources?

    With that kind of Noise levels, is there a way to filter the rest of the frequencies? I tried applying low pass and high pass filters. But I still see a lot of noise.

    I am worried that if a Simple sound signal has this much of noise, the captured ECG may not be good for further analysis.

    For kicks I captured the 1000Hz signal using python code and here is what it looked like.

    http://i.imgur.com/4xZMy5K.png

    I want to make sure the captured ECG is good quality Signal. Any pointers on reducing the noise levels?

    Thanks for you help.

  7. Scott,

    I posted previous message but it seems to have disappeared somehow. Anyway, I was testing my laptop sound card with a 1000Hz Sine wave and I see a lot of noise being introduced into the converted signal from sound card. See the image below.

    http://imgur.com/N77vEkF

    This is a 100Hz Sine wave generated from my Mobile phone, Captured by Laptop sound card audio jack (4-pole). Visualized in Audacity.

    Did you see similar noise levels in your sound card when you captured the ECG?
    Any ideas on how to remove the noise?

    Thanks.

  8. Hey Scott,

    Great work man, i loved it alot… I wish to make it work using raspberry pi instead of PC as it will become more portable, but for some reason Im not able to use the same code for raspberry pi ( PS i’m kinda new to python language so i dont know much in this matter). I tried using usb sound card to connect to audio jack, as we know there is no microphone in RPi, but still it is not working. Scott i request you pls help me in this matter it will be a great thing for me if u help me so that this project will work on RPi… sorry for the trouble

    • Hi AnujStark, what output are you actually getting? What are you using for electrodes? Are you careful not to ground yourself, other than how it is described in the video?

      • Hey Scott,

        I got an ECG simulator which borrowed from my institute, so I’m actually using it before trying it on myself. Also I’ll be purchasing the electrodes you mentioned about in this article.

        Okay, now talking about my error I’m receiving proper single from the simulator to the amplifier circuit to my audio jack. Firstly I’ll confess that I’m new to python language so i blindly tried to use your code as it is in my Rasp Pi 3, I’m using USB sound card to receive the output from the circuit to my Rpi. Also i made it clear that the USB sound card is made default in Raspberry Pi tweaking some variables in alsa.conf and in .arsound.

        As for now I found that my USB sound card is working just fine but still I’m not able to receive any output on the GUI. I’m attaching some links to my errors and output as it is seen on my display. Also I’m using Raspberry Pi 3, latest Rasbian build, Jessie with Pixel and a cheap USB sound card.

        The output i get:
        https://www.dropbox.com/s/abqyukcc5qyvmyq/ECG_1479147756.png?dl=0

        and the lx terminal shows:
        https://www.dropbox.com/s/tgtolpo8u43wwbw/output%20i%20get%20on%20lxterminal.txt?dl=0

        • After checking your output, it looks like you have 4 potential sound card inputs. The output you linked to says they are “[2, 3, 7, 9]”. The software tries to use the first one, and fails, so the first one is probably not your microphone. To fix this, try telling the software to use one of the other inputs.

          Edit this line of go.py:
          https://github.com/swharden/diyECG-1opAmp/blob/master/software/go.py#L30

          and change it from:
          self.ear = swhear.Ear(chunk=int(100))

          to:
          self.ear = swhear.Ear(chunk=int(100), device=3)

          Try making the device 3, 7, and 9, and I bet one of them will work…

          PS: I fixed your missed words in the original comment 😉

          • Hey Scott,
            Thanks alot for the solution, for some reason i had to return the simulator kit back to my college, and I’ll get it back in about a week or so. I want to ask if it is okay to use an earphone’s mic signal directly to USB sound card and then try to record/plot it using this code. Because i want to make sure if the code is running properly on my device.

            PS: Thanks for fixing them ??

          • Hey, I just found out that I fried my sound card somehow, I tried hooking it up to my PC just to check if it is working or not and i tried recording some random audio in Audacity and i received nothing so now I’m hoping that only my sound card was the issue and I’ll try the old code using a new sound card.

  9. Dear Scott,

    Thank you very much for sharing your knowledge and experiments. For a long time I wanted to experiment within the field of circuit and electric engineering and your post motivated me to finally start doing it :).

    To start with, I tried to design a circuit simulation of your circuit to see what currents and voltages are present in each stage. Somehow I am not so sure which circuit element would represent the leads (Chest1, Chest2, Leg); Most likely a Voltage Source connected to ground?

    You can find the simulated circuit in the pdf (link below). Simply have a look if it semantically matches your circuit:
    https://github.com/ireicht/circuit_experiments/blob/master/ECG/circuit_simulation.pdf

    Thanks in advance
    donpromillo

    • Hi Donpromillo,

      I do not think your circuit is accurate enough to perform a simulation. The main reason is that there are components necessary for a simulation which are NOT drawn on my schematic. The most important one is the conductance (very small, but very important) between the 3 electrodes. Consider https://www.swharden.com/wp/wp-content/uploads/2016/08/circuit.jpg and remember that your skin is slightly conductive. For this circuit to work, it is critically important that the leg electrode voltage clamp your body to 0V. This will be a very slow clamp (your body is many pF and the skin resistance is 100s of MOhm) but it is the most important thing that makes this ECG work in the first place.

      I suspect if you draw the circuit in your simulator exactly as I show it here, then connect every electrode to every other electrode through a 100M resistor, it will work. Remember that it may take 10s to “warm up” and reach steady state, so your simulation may not produce good data unless you let it run for a long time.

      Good luck!

  10. Hey Scott,
    Pretty nice DIY ECG project you got here and thanks for sharing it. I just wanted to inquire why haven’t you included the ECG analysis part like the one you built earlier. I am interested in reproducing this project but i am not good at computer programming and I would like this circuit to do a real time analysis of my ECG signal as well. any suggestion from you is welcomed.