I have been following the Battle of the DreamBoards Championship with interest and I enjoyed the variation of functionality features put together in each of the dreamboards. I especially had my attention attracted by the combination of audio inputs and outputs, analog to digital and digital to analog converters, and the microcontroller digital processing of signals that I saw in PC64, which was also the winning dreamboard. This combination of features inspired me to build one of the old ideas that has been waiting "dormant" somewhere in my mind for a while. Here is a description of my project:
Why I built this project:
Electrocardiographs (ECG) measure electric signals generated by the heart and display them in a graphical format on a screen or on a hard copy electrocardiogram. Most of the times when I go to a doctor for check-up I have one of these electrocardiograms measured on my heart. More extensive monitoring of the heart activities uses 24 hours ECG or even two weeks ECG. The recorded data is then processed in medical offices to determine if there was any ECG waveform aberration. So in my case once in a while I may have an irregular heart beat or a premature ventricular contraction (PVC) which typically happen in normal heart activity but if there are too many then they need medical attention.
What am I trying to do:
ECG measurement and analysis is a powerful tool for detecting problems with the heart activity when it is recorded, but how about the rest of the time? Can I have a device that continuously monitors the heart activity every day through ECG measurements? The technology is available and ECG heart monitors are inexpensive, but the problem I see is how I can actually use them to continuously monitor my heart activity? For how long I can keep my eyes on the screen and watch the ECG waveform in real time? I also need to do other things during the day so I am not able to continuously watch that screen. If I record the ECG data for a period of time I will then need to go and analyze it, which takes time and thus I find inconvenient.
I kept thinking about how I could monitor my heart activity without disturbing my normal activities. So I decided to build an ECG device that can be attached to an audio system and when it detects aberrations in ECG waveform it distorts the music or voice that plays through the audio system (CD player, radio, TV, or any appliance that outputs an audio signal). This way I can monitor my heart activity while doing my regular daily activities without spending specific time to watch or analyze he recorded the ECG waveforms.
How does it work:
The ECG unit continuously measures the ECG and when it detects an aberration in heart activity it sends a command to the audio unit which then either increases the pitch of the music or voice played by the speaker.
How I implemented it:
I have chosen to implement this project on a Freescale FRDM-KL05Z development kit that has a MKL05Z32VFM4 microcontroller. This is a very inexpensive microcontroller and development kit that can operate up to 48MHz, has 32kB of flash non volatile memory, various analog and digital peripherals, and what I planned to use in my application it has a 12-bit analog-to-digital converter. This development kit supports the open standard embedded serial and debug adapter OpenSDA. What I also liked on this FRDM-KL05Z development kit was that it has Arduino type GPIO headers, that allows me to easily connect Arduino type shield modules. Thus, I could use a shield module for the front end ECG analog path.
Here is a picture of the Freescale FRDM-KL05Z development kit:
Setting up the design environment for the Freescale FRDM-KL05Z board
The first step after I received the FRDM-KL05Z board was to setup the design environment. In this process I found very useful a quick start guide file, FRDM-KL05Z Quick Start Guide (Rev 1.1), available to download on the Freescale website part of a quick start package with precompiled examples and OpenSDA applications.
This document has guided me step by step through installing the required drivers, the OpenSDA programming and debug environment, and loading and running an application. I found quite convenient the MSD flash programmer where I only need to drag and drop the file and it gets automatically programmed into the microcontroller.
After getting the FRDM-KL05Z board up and running I installed Keil micro vision (uvision) programming environment. This tool provides source code editing and program debugging, so I have chosen to use this platform for the rest of my work on this project.
I also want to mention that during this setup process I bumped into various issues for which I either found solutions on Freescale support webpage or I got help from Freescale support team. I was impressed on how fast the support engineers responded to my requests, like for example I sent a request on July 6 at 9PM and a support representative responded to me in 5 hours, at 2AM. I also contacted the support team on a Saturday with a design question about configuring GPIO pins, and I was surprised to get an answer during that weekend on Sunday; I thought I will need to wait until the week starts, but the response came during that weekend.
The Electrocardiogram Circuit:
For the front end analog amplification of heart electrical signals I chose to use an Olimex Shield-EKG/EMG module from Newark store.
This ECG shield works with multiple types of electrodes, and I chose this set, also from Newark store, that attaches to the shield module through a connector:
The ECG shield board connectors plug directly into the headers that I soldered on the Freescale FRDM-KL05Z board:
Before I describe the rest of the components of this project I would like to show a block diagram.
Block Diagram of the Entire System
This system has three main parts: ECG front end module, FRDM-KL05Z data processing, and an audio processing module.
The ECG shield module measures continuously the ECG waveform, which is then sampled by the analog to digital converter inside the FRDM-KL05Z development kit. The sampled values are processed to extract the peak R-waves and store the samples of a R-wave to R-wave cycle, which represent the time between two consecutive heart beats. These samples become the expected values for the following cycle. So in each R-wave triggered cycle the FRDM-KL05Z code compares the real time measured ECG samples with the expected values and if it finds discrepancies it generates specific flags. When a flag is detected the code generates control signals that are sent to the audio processing module to either increase or decrease the pitch of the audio signal that passes through the module. For example if the person monitored by this system listens to music on the radio, CD or mp3 player, the music will change the pitch higher or lower depending on what aberration has been detected on the ECG waveform. Similar sound pitch changes happen if the person watches a show on the TV or DVD player. This way the heart activity is monitored while doing other activities without the need to spend dedicated time for this monitoring.
Schematic of the System
Here is the schematic diagram of this project.
and here is a picture of the prototype on my test bench:
The main unit is the FRDM-KL05Z evaluation kit, which provides power to the ECG shield and provides the control signals to the audio processing board. The ECG shield amplifies and filters the electrode signals and then sends the output to PTB13 GPIO port of the MKL05Z32VFM4 microcontroller on the FRDM-KL05Z board. This microcontroller analyzes the analog ECG waveform and when it detects heart activity problems it sends two digital pulse signals to the audio processing module, one that increases the pitch and one that decreases it. The microcontroller also controls four LEDs that display what was wrong with the ECG waveform that generated a flag. The four ECG aberrations signaled are: LED1 = Early R-wave peak (Irregular heart rate too short), LED2 = late R-wave (Irregular heart rate too long), LED3=P-wave and/or T-wave magnitude too large, LED4=P-wave and/or T-wave magnitude too small.
Here is a drawing of these elements of a ECG waveform:
The audio processing module is a voice changer kit available to purchase from various places, like for example on amazon.com:
Here is a picture of my audio processing board:
I have modified this kit by adding an audio line-in and an audio line-out connectors instead of soldering a microphone and a speaker to the board.
In order to control this kit with TTL pulses instead of pressing the buttons I had to solder two wires on the high pitch and low pitch switches and one on the ground. These signals are normally pulled-up and they need to be driven low to get the same effect as pressing the buttons, but because FRDM-KL05Z board operates at 3.3V and the voice changer kit at 5V I couldn’t connect directly these wires to the FRDM-KL05Z outputs. To solve this issue without adding level shifters, I inserted a diode in series with the wire. The diode allows pulling down the node but blocks the 5V coming into FRDM-KL05Z when the signal is not asserted.
Developing the Code
I started from two example codes and I expanded them into the complete code. One of the example code was LED_Blink that comes in the KL05-SC package that I downloaded from Freescale website. There is a guide document in this package that describes how to install and run the code examples, FRDM_KL05ZSCG_KEIL_rev1, which I found very informative and easy to follow. This one is specific for Keil tools, and there are two other similar documents for IAR and Code Warrior tools.
So I used the LED_Blink to learn how to generate and send digital signals to output ports, which I needed for controlling the audio processing module.
The other example code that I used was AdcDemoWithDMA. This code configures, calibrates, and performs analog-to-digital conversions using the 12-bit ADC inside the MKL05Z32VFM4 microcontroller. I found this example code very useful for implementing my ECG signal sampling function, and I decided to actually start form this example code and build the rest of the project on top of it.
What my code does
At power up it initializes and calibrates the ADC (this part was already in the example code, which saved me a lot of time in developing this project). Next the code configures the GPIO pins. To be able to later toggle the state of GPIO pins, the pins need to be configured in the GPIO multiplexer:
And configured as outputs:
After the power up initialization and ADC calibration the code enters in a continuous loop. At the first cycle of the loop memorizes the samples corresponding to one R-wave to next R-wave recording of the ECG signal. To do this, the code first looks for an R-wave peak:
If there is no ECG cycle memorized (first reading after power-up) the code memorizes the entire samples after the first R-wave peak and until the second R-wave peak.
These samples become the expected values for next heart rate cycle, and the timing interval between the two R-wave peaks becomes the expected heart rate period for the next cycle.
After memorizing the first cycle, on each heart rate cycle the code compares the current ECG samples with the expected values:
and decides if there is any aberration from previous cycle (including a compare window margin to account for noise and other perturbation factors).
When the code detects an aberration in the ECG waveform it evaluates what type of aberration is and it prepares and sends the corresponding control signals to the audio processing module and to the LEDs. There are four types of EGC aberrations detected, early R-wave peak, late R-wave peak, P-wave and/or T-wave magnitude too large, and P-wave and/or T-wave magnitude too small. Here is the code that is executed for the first case:
Similar codes are executed for the other three cases.
Some Experimental Results
To be able to see aberrations in the ECG during these experiments I intentionally lowered the compare window margins in the code, so that the project became more sensitive to small perturbations or variations of heart activity. Here are some pictures of the four types of ECG aberrations detected by this system:
We notice in this picture the ECG signal displayed on the Tektronix 1202B-EDU oscilloscope by the blue trace. There is no aberration detected and the yellow signal that shows what is sent to the audio processing module has no pulses. Also all four LEDs are OFF.
We notice in the picture that the timing between the two R-wave peaks marked with pink in the figure is slightly shorter than the previous heart rate cycle. The program code running in the FRDM-KL05Z module detect this small difference, turns ON LED1, and sends three digital pulses to increase of pitch of the audio signal followed by another three pulses that would decrease back the pitch to normal.
These pulses are shown by the yellow trace on the TBS1202B-EDU oscilloscope. The yellow signal actually represents two TTL type digital signals encoded here this way since I did not have a third measuring channel on this oscilloscope. So to visualize the moments when the high pitch and low pitch pulses are sent with reference to the ECG analog signal I built this simple circuit structure:
so the high pitch control pulses are attenuated by a factor of 1/3 and the low pitch pulses are attenuated by a factor of 2/3, and both these attenuated signals are mixed in the same output signal. So on the yellow waveform, the first group of three pulses represents the TTL pulses sent to increase the sound pitch and the second group of pulses with larger amplitude represents the TTL pulses sent to decrease back the sound pitch.
The following picture represents a case when a lower heart rate irregularity has been detected.
The timing between the two R-waves marked with pink on the picture is slightly larger than the previous cycle. The program code detects this aberration, turns on LED2, and sends a group of pulses to lower the sound pitch, (the large amplitude pulses on the yellow trace), followed by a group of pulses to increase the pitch back to normal, (the lower amplitude pulses on the yellow trace).
The following picture shows an example of increased magnitude of T-wave section of the ECG waveform.
The region encircled with pink line shows the increased magnitude T-wave. The program code detects it, turns ON LED3, and sends a sequence of increase pitch control to the audio processing module followed by a sequence of pulses that decrease the pitch back to normal.
The following picture shows an example of measured lower magnitude T-wave.
The region marked with pink line shows the decreased magnitude T-wave compared to the previous cycle. The program code detects this aberration, turns ON LED4, and sends a sequence of decrease pitch control pulses to the audio processing module followed by a sequence of pulses that increase the pitch back to normal.
And here is a short video that demonstrates the functionality of this system. The real-time measured ECG analog signal is shown by the blue trace on the oscilloscope screen, the increasing and decreasing pitch control pulses are shown by the yellow trace. The blue LEDs light up one at a time signaling the type of ECG waveform aberration detected.
This is how my “musical” ECG project functions. In a future version I am planning to add a wireless radio interface between the Freescale FRDM-KL05Z board and the audio processing module. This way the person wearing the ECG electrodes is physically disconnected from the audio processing module, allowing this person to move around in the house while listening to music or other voice/sound based home appliance (TV set, CD player, DVD player, radio…)
Here is the entire code: