ECG and PIC Demo

    This little article aims to show how easy it is to use the ECG Front End by interfacing to a Microchip PIC16F1826 microcontroller. The microcontroller reads the Heart Rate in two ways. Firstly, it uses the AD converter to send the data as an 8 bit stream over the UART for a PC to display. Second, it uses the built in comparator to detect the pulse and flash an LED on every beat. This article won't go into all the characteristics of the ECG waveform but that information can easily be obtained by a simple google search or on Wikipedia. As built, the demo circuit was drawing about 2mA of current not including the LED current which is about 7mA. But the LED is only on for milliseconds and should not draw too much power over time.
    You can download the source code for the demo circuit at the end of the article. Also the .NET sourcecode for the PC can be downloaded here.

Disclaimer: This product is for educational purposes ONLY.
It is NOT to be used for diagnostic or for life saving operations of any sort.
Always consult a qualified medical professional for any medical concerns.

    Here is an idealized image of the ECG waveform.
The part that is of the most interest is the QRS portion of the waveform.
It's this peak that is used to determine heartrate.


Below is the schematic of the demo.

    The ECG Front End is connected to the PIC through a simple high pass filter consisting of the 22uF cap, 100K, 10K, and 10K pot. The 10K pot is used to set the center of the output. Since the PIC's Vref is set to 1.024V, the center is set to approx. 0.405v. This is slightly below the middle of the Vref so that only the peak of the signal is above the 1/2 Vref point. The signal is then sent into pins 1 and 2, analog in and comparator in respectively. The 10K, and 10K pot that feed into pin 17 is to set the comparator negative input. When the signal is greater than approx. 0.745v, the comparator output is high and the PIC turns the LED on. If this circuit is to be run from battery power, a voltage reference of about 2.5 volts should be used for the VCC that feeds the two potentiometers to maintain the proper reference voltages as the battery discharges.

Above is an image of the demo circuit as built with the ECG front end on the left, the PIC
in the middle, USB to Serial next, and finally, a CR2032 coin cell battery.

    The number one issue with this ECG measurement is line noise. 60Hz can swamp the signal and make it unreadable. The quality of the electrodes can make all the difference between a clean looking signal and a noisy mess. Access to medical grade electrodes would be ideal, but this isn't really an option for the DIY'er. A few suggestions though are:

    - Use shielded wire. I have used RCA cables and clipped off the ends with decent results.
    - Make your cables as short as possible. The longer the wire, the more noise that it can pick up.
    - Make your contact pads as large as possible. The more contact with skin, the greater the signal voltage picked up.

    The software for the PIC is rather straight forward. After initializing all the peripherals, it loops through waiting for the UART to be empty, then it reads a sample and checks the comparator. If the comparator is high, it lights the LED. It also sends the AD result over the UART. Since the AD converter is 10 bits, the PIC right shifts the result 2 bits to send the data as 8 bit.
    The UART has its baud rate set to 600 which is 60 bytes per second. Since the software waits for the UART buffer to be empty before sampling, it has the effect of setting the sample rate to 60. This is a great way to reduce and possible eliminate the 60Hz noise.
This won't get you a perfect looking ECG waveform, but for heart rate calculation, it is perfect.

    I hope that this will help you in your design of a simple low cost heart rate monitor, and with the addition of an external EEPROM, this could easily be setup to record heart rate over time (eg. exercise monitor).

Copyright © 2013 by 100RandomTasks. All rights reserved.

Site Admin,
Feb 2, 2013, 4:00 PM