From quite some time I had been busy trying to perfect my real time ECG monitoring system. I was able to look at my ECG after a tedious task of recording it via sound card and then processing it in MATLAB. If you have closely read my previous post, then you will know that I was having an analog front-end based on INA128 instrumentation amplifier and then I was sending it over to the microphone input of my PC. I recorded this signal at a sampling rate of 1000 Hz and then processed this audio file in MATLAB to get a signal which looked just like a real ECG.
The first image on the top shows the zoomed section of the recording. From this image, it is absolutely clear, the PQRST points on the ECG graph. This being a 3-lead ECG, we can only see the heart from one perspective, but it is enough for preliminary diagnosis.
In order to reach to this stage, I have to do process the signal, filter it, smoothen it and then display it. Pretty long process if you are doing it all manually.
Bring in the automation
Automating the entire process was my goal back then. I wanted to eliminate the need of using a sound card to view my heart function. The device was barely portable because of two reasons. One, the circuit was poorly designed and therefore, there was a lot of motion artifact. I have to hold my breath in order to have a clean ECG. The circuit also did not have a good input impedance due to which the amplifier often saturated giving me a flat line!
Now the second reason is, I had to sit near my PC to record using the microphone input cable.
From this introduction, the goals should be pretty clear.
1. to have a portable system
2. To redesign the front end for obtaining good quality signal
3. To have multiple sensors (Still working on it)
Over all system block diagram would look somewhat like this.
How I managed to automate the process
For any mildly experienced person, the answer is to incorporate a microcontroller in the system. By looking at the system plan, there is a microprocessor at the heart and all the analog front ends on the left and a wireless transceiver (Bluetooth) on the right.
At the very beginning I tried my luck with Arduino board. All I had to do is feed the analog signal into the ADC channel of Arduino and have the analog to digital conversion task be handled by the controller.
To have a portable system, you always need a PC for testing. It was all good while I was still using a PC to view my graphs. Processing was a quite handy tool to view the real time ECG signal coming from my heart.
I could have gone ahead and built an application to run on the PC, but that was not what I wanted. What I wanted was to be able to view this graph on an ANDROID PHONE!
But I was not confident to start working on that part unless and until I had a good quality ECG signal on the PC. If you do not have a proper working analog front end, then there was no point in proceeding further.
Designing a good ECG circuit
We start with the INA128 instrumentation amplifier on the left hand side. Look carefully those capacitors C10 and C9. For those who do not know much about PCB designing, here is the explanation. The power rails often pick up noise or inherently have some noise coming from the circuit. If we allow this noise to enter our circuit through the power rails, I dont have to explain what would happen. Since, the noise can be picked up over the rails, we try to place these decoupling capacitors as close to the device as possible. This decouples the noise and lets the device have clean DC power. It is important because we want a clean ECG signal over which we do not want to do much signal processing. Any noise in the circuit will show up at the final output.
Opamps U1A and U1D form the band pass filter circuit. Again look closely at the numbering. I have used U1A and U1D, I could have used U1A and U1B, but that could have changed my PCB layout. You can choose any opamp in the IC which makes your life easier while routing the board. (I have indeed used the opamps U1B and U1C further down the circuit and not have let them go waste)
Opamp U3A is an amplifier with gain of 10. I have made this amplifier drive the notch filter which is ahead of it.
The notch filter is an optional components. INA128 works pretty well at cancelling the common mode noise at the input. But in my case it still had some 50Hz power line noise. In my past versions of the circuit, I had the amplifier U3A at the very end of the circuit, but that did not seem to work. The notch filter was overloading the band pass filter causing the voltage to drop to zero and hence my decision to keep this amplifier right before notch filter so that this amplifier would rather drive the notch. Opamp U3D working as a buffer again to prevent any kind of overloading.
The circuit does not end here. We also need the right leg drive which is quite useful in improving the SNR.
Please note that the black lines represent ground. And now its time for the full circuit.
Now that we have a nicely working ECG circuit we are ready to move one step ahead.
Please note that this circuit is available for sale. Contact me at [email protected] for the circuit.
The microcontroller and Bluetooth interfacing
Like I said, I had been using Arduino at the beginning. Problem began to arise when I interfaced the HC-06 bluetooth module with it. It was quite annoying to program Arduino while the bluetooth was connected to it because it uses the same Rx, Tx ports as the one that are connected to USB chip. I could have used the Software Serial functionality. But considering the future expansion of my project, I thought that it was time to let go of Arduino and starting hunting for a new candidate.
I found Stellaris Launchpad, 32 bit, 80MHz, nuff said! It had just too many Serial ports, SPI ports, internal RTC, dedicated features for DSP and the list goes on.
Now anyone with some experience in microcontrollers would say that having a new controller is tedious, because you have to port your software over to the new one. New controller would have a different architecture and other differences. There was Energia IDE which allowed me to port my Arduino code over to Stellaris with minimal modifications!!!
I would highly suggest you looking into Energia IDE. It has coding similar to that of Arduino and you also have the pleasure of incorporating functions from Stellaris API (which are just too powerful). All of this just for $12.99! There was a discount going on back in August and which is why I bought it for $7.99 😛
I was quickly able to interface the bluetooth module. I took help of the Amarino MeetAndroid library which enables easy interfacing and does all the hard stuff for you. The biggest program came when the voltage levels mismatched!
Stellaris can only accept power through the USB. If you try to bypass the voltage supervisor, it just shuts down. There, an obstacle in the path of portability! Not to worry, there was an additional USB host port on Stellaris through which I was able to give power using a battery.
Our Li-ion batteries only give out upto 3.8 V and USB works at 5V. Also, our ECG circuit requires nearly 5V to operate properly. Now how do I give power to these devices?
Answer was simple. A step up boost converter did the job for me. It boosted the voltage from a minimum of 2.2 V up to 5.1V. Enough for powering up the entire system. You can check out the boost converter from ICStation.com. Not to forget that we also have a bluetooth module sitting in our system which also needs power.
The Android application
Android is where we want to display our ECg signal, temperature sensor and the glucometer (which I still have not talked about).
Amarino library provides a pretty functional app called SensorGraph which takes serial data from the bluetooth and plots a graph on the phone. It worked pretty well, but viewing the graph was not all I wanted to do!
If you know Android programming, you can easily work around and add in some interesting features to that app. I am a newb, and I took quite a while to learn it.
Having a medical device monitoring yourself, why not let others view it as well? I decided that this app must upload this data to the internet and have others open a website to view it. Perhaps even a doctor could login and view the ECG and other medical records of you. May be if the doctor sees something wrong he can alert me? (Just in case he happens to open my records)
This is how our android application is looking so far.
When it comes to storing records, the first thing that came to my mind was to use MySQL database. It is quite easy to use and serves the purpose quite well.
Android does not have an API for directly accessing the MySQL database (WHAT?!)
So, we have to "POST" our ECG values on to a PHP page and the PHP page would store the data onto the database. Sounds simple, but its not (atleast for a newb like me).
By POST, I actually mean POST. Here goes a code snippet.
$patient_name = $_POST['patient_name'];
$gluco = $_POST['gluco'];
$temp = $_POST['temperature'];
$ECG = $_POST['ECG'];
On the Android side, we first need to take the data, which is in string format and then form a Basic Name Value pair which is basically the name of the field and the corresponding value that we are going to "POST".
nameValuePairs.add(new BasicNameValuePair("gluco", dataToBePosted));
Now, we need to "POST" it...
HttpResponse response = httpclient.execute(httppost);
Once we get a proper response from the web server, we continue graphing.
The android application works excellent even with EDGE / GPRS connectivity. This proves that our device can also work in remote rural areas with poor internet quality.
What have we done until now
1. We have the ECG, we feed it to the ADC of the microcontroller.
2. We send it over bluetooth using MeetAndroid library.
3. On the android application -> We are plotting the graph continuously. When a user enters his name and presses "Record and upload", 1500 samples of data are recorded and uploaded to the server as shown above. In short, we have an Android based wireless ECG monitoring system so far.
4. Store the POSTED data onto the MySQL server.
5. When a user accesses the webpage "patients.php" he can look at all the records.
This is how the "patients.php" looks like.
Most of the entries are mine which were stored in the process of testing. But you get the point!
And now let us have a look at how the ECG looks online!
If you look closely, you would be able to see that the PHP script also calculates the Heart rate of the given ECG signal. We have tested it and it is pretty darn accurate.
Some technical details
The battery operated device works for 2 to 3 hours on a single charge. We are planning to use Bluetooth Low energy in future revisions so that the battery life elongates even more. Additionally, if we replace LM324 Opamps with OPA333, we can achieve significant power cutting.
We are also working with turtle's pace on pulse oximeter and blood pressure.
Our final goal is to put this all on a single circuit board and have a seamless working medical device.
We are accepting all the possible help you can provide to us, including help in development and financial support.
This is how our prototype looks right now.
I just created a ECG front end that works great with all the microcontroller ADCs. Here are some pics. Nothing great, I simply got it fabricated by professional services.
You can buy the circuit for $85 + shipping. All you have to do is leave a comment below with your email address and I will contact you.