The world of embedded technology is getting wider and wider by the year. More and more embedded systems are becoming a part of our everyday life, making it easier for us to do all the tasks that were once deemed impossible. One of the fields in which embedded technology is very useful is medicine. A lot of traditional and new devices have their core in digital electronics. These devices make diagnosing and curing patients easier and more efficient for doctors than before. Hardware engineers at MikroElektronika so far came up with two products that fall under the category of medical devices: Heart rate click and Heart rate 3 click. These two clicks gather heart rate data from the finger of the user. That data can be used to roughly calculate the heart rate of the user. But what if we need more precise data? What if we wanted to implement a full ECG scan? Now you can.
Enter ECG click
ECG (Electrocardiography) is a method used to gather data about the beating of a patients heart. An ECG scanner connects a number of electrodes to different parts of the patients body, and measures the electrical activity of the heart. This electrical activity is then plotted in a graph, and helps expose different heart diseases.
How the click works
The click contains a 3.5mm jack to which three electrodes are connected. The electrodes will detect the electrical activity of the heart and drive the signal to the 3.5mm jack. After that, the signal needs to be amplified and filtered, therefore the click also contains two amplifiers, two high-pass filters and one low-pass filter.
Once the data is acquired, amplified and filtered, it is sent to one analog pin. The MCU then reads this analog data, converts it to digital, this digital data presents the value of the electrical activity of the heart at that particular moment.
ECG click front and back
ECG click gathers data from three electrodes, so where do we attach these electrodes? The electrodes have a naming convention, and each has one of these abbreviations: LA, RA, LL - meaning Left Arm, Right Arm, Left Leg. You can already see where this is going, one electrode is placed on the left arm, one on the right arm, and one on the left leg. Usually, though, the LL electrode is not placed on the leg, but right beneath the heart.
Here's a simple illustration:
Electrode placement
Placing the right electrode at the right place is very important, therefore, the cables that come with the click are marked LA, RA and LL, so that our users don't misplace them.
ECG cables
Acquiring the data
Okay, so the click gives us the analog value of the electrical activity of the heart, what now? In order for us to have usable ECG data, we need two parameters: heart data and time.
Yes, time is always an important factor, especially in ECG. A person with 60 heart beats per minute is pretty much okay, but a person with 80 beats per minute has heart problems. Therefore, the MCU will gather ECG data, and also the time when that data is recorded. This way, we will have a time-based overview of the patient's heart beating.
The ECG click can be bought along in a bundle with the electrodes and cables which connect them:
ECG bundle
What to do with the data? - MikroPlot it!
MikroPlot
Now we have ADC and time values? What's next? How can we possibly determine the heart rate? We have an app for that: MikroPlot.
MikroPlot is a PC application that gathers whichever data you need to plot from the microcontroller (along with time data) and creates an interactive plot for analyzing the collected data. This way, the doctors can analyze the heart rate without any doubts, and determine if something is wrong with our patient.
MikroPlot data visualization tool
How to use the app
The mikroPlot app it's quite easy to use.
Serial ports
The MCU and the PC communicate using simple UART serial ports. Once the application is started, the serial ports combo box will list all the detected serial ports. If your MCU is connected, and ready to use, it's serial port should show up in the combo box. Select the serial port of your MCU and hit "Connect", if everything is right, the status label right underneath should say "Connected". If at any point you don't see your serial port in the combo box, hit "Reset" this will do a new search for serial ports and list all the ports the application finds.
Serial ports
NOTE: For the application to work correctly in real time, you will have to set up your serial port latency. This is done by going into Control Panel -> Device manager -> COM ports, once you find your COM port, right click -> properties then go Port settings -> Advanced. There you will find Latency timer, set the timer to 1 msec. This will enable the COM port to work fast enough for real time data plotting.
Setting the correct latency
The Reset button
The reset button
The reset button is pretty self-explanatory, but let's see what happens once you click it:
- Disconnects the serial port
- Clears the plot
- Scans all the available serial ports and updates the combo box with the results
So, if at any time you need to change the serial port, clear the plot for some new data, or search for new serial ports, the reset button is where it's at.
The plot
The plotting field
As you can see, the plot is settled at the center of the screen. Above the plot there's a status label. The status label will read "Ready" once your development board is connected to the MikroPlot app, otherwise, it'll read "Disconnected". There are two key features we need to point out about the plot:
- The plot is in real-time, which means that it will update with every sample of data that the MCU sends. Basically, every time your heart beats, MikroPlot will immediately show it.
- The plot is interactive, meaning that, once you have finished sending data, and the picture stands still, you can zoom into the part of data that interests you, or move the plot across x or y axis.
More -> about
The about sections shows additional information about the application, it can be closed by hitting the ESC key.
What if...
Problems may arise if the user disconnects the USB cable while the data is being sent. But worry not, the application will notify you. Once the application detects that you have disconnected the ECG device, it will immediately close the serial port. Setting up your working environment again is pretty easy:
- Reconnect your USB and reset your board by powering it down then up again.
- Hit "Reset" to list all the serial ports again. Choose your serial port and connect to it again
The scenario
Okay, now that we have seen how the click and the application work, let's go over a quick example, and see how everything works when put in motion.
The firmware
#include#include #include void InitTimer2(){ RCC_APB1ENR.TIM2EN = 1; TIM2_CR1.CEN = 0; TIM2_PSC = 5; TIM2_ARR = 54599; NVIC_IntEnable(IVT_INT_TIM2); TIM2_DIER.UIE = 1; TIM2_CR1.CEN = 1; } static bool read_flag = false; static volatile uint32_t interrupt_ctr = 0; static volatile uint32_t seconds_counter = 0; uint32_t temp_adc_read; double temp_timer_read; void main() { uint16_t adc_read = 0; uint32_t i = 0; char timer_read_string[10]; char final_string [20]; temp_adc_read = temp_timer_read = 0; GPIO_Analog_Input( &GPIOA_BASE, _GPIO_PINMASK_4 ); GPIO_Digital_Input( &GPIOD_BASE, _GPIO_PINMASK_10 ); UART1_Init(57600); delay_ms(500); // Uart1_Write_Text("Uart initialized rn"); // this is for testing only, it should stay commented when implementing ECG reading ADC_Set_Input_Channel(_ADC_CHANNEL_4); ADC1_Init(); Delay_ms(100); while(1) { if (Button(&GPIOD_IDR, 10, 10, 1)) // if the button is pressed, interrupts are activated and the measuring begins { Uart1_Write_Text("STARTrn"); InitTimer2(); EnableInterrupts(); delay_ms(500); } if (read_flag == true) // every 3.9ms, measure data, measure time, and send them to the PC { DisableInterrupts(); temp_adc_read = ADC1_Get_Sample(4); temp_timer_read = interrupt_ctr*3.9; inttostr(temp_adc_read, final_string); sprintf(timer_read_string,"%.2f", temp_timer_read); strcat(final_string, ","); strcat(final_string, timer_read_string); ltrim(final_string); Uart_Write_Text(final_string); Uart_Write_Text("rn"); read_flag = false; EnableInterrupts(); } if (seconds_counter == 15) // after 15 seconds of measuring is done, disable interrupts and finish { DisableInterrupts(); while(1); } } } void Timer2_interrupt() iv IVT_INT_TIM2 { TIM2_SR.UIF = 0; interrupt_ctr++; if (interrupt_ctr % 256 == 0) seconds_counter++; read_flag = true; }
The code on the MCU is fairly simple: we have a timer set to interrupt every 3.9 milliseconds.
Why 3.9? The ECG click works best when the sampling rate is 265 Hz, which means that we need to measure the analog pin 256 times per second: 1000ms/256 = 3.9 milliseconds.
When a button is pressed (D10 in this example), the timer and interrupts are initialized, and reading can begin. The MCU sends a "START" string to notify the PC to get ready to accept data. When the PC recognizes the "START" string, it will clear the previous plot (if one exists), and get ready for new measurement. Once the timer interrupt is fired, we will capture the value of the analog pin, as well as the time of our measurement. These two values are then concatenated into a string with the following pattern "adc_value,time_valuern". This string will be sent over the serial port to our PC. Every sample of ECG data will follow this procedure for 15 seconds. Once 15 seconds have passed, the MCU will disable interrupts and jump into an infinite while loop. Want to measure longer? No problem, just change the
if (seconds_counter == 15)
line, and set the threshold to be as many seconds as you'd like, or remove this part of code completely if you want to measure infinitely.
For every sample of data the PC receives, it will parse it and update the plot.
The whole process can be described with these steps:
- Connect the ECG electrodes to the body
- Start up the app
- Start up your board
- Find your serial port on the application
- Connect the right serial port
- If everything is ready, press the button on the board (D10) to start measuring.
- Data is plotted and at your disposal
If you want to measure and plot data again, reset your board, and press the button (D10) again if the serial port is connected.
You can download the ECG click firmware here and the MikroPlot application here.
More information about ECG click and MikroPlot can be found on our documentation page.
======================================================================================================================================
UPDATE: This is the old tutorial video before the updated MikroPlot, the new MikroPlot, which works in real-time, can be found below
======================================================================================================================================
Other implementations
The application is pretty flexible with data, and we will see to it that it becomes even more flexible. Heart rate click and Heart rate 3 click can also acquire data and send it over UART to the PC, you can use the MikroPlot to analyze data from these two clicks also. Not to mention all the other sensors which contain precious information ( temperature, pressure, air particles etc.), all these can be plotted and analyzed when a timer is added and the value is sent in the string format described above.
Remember Heart rate click? We have made a completely new example for it: the heart rate data is collected from the finger, and sent to MikroPlot to plot it in real-time. The new example for Heart rate click can be found here.
Whether it's heart rate data or room temperature, take it to MikroPlot, and make the most out of it!
If any problems arise, feel free to contact us on our forum, or on our helpdesk. Have fun!
References
ECG, Wikipedia 2016
Electrode, Wikipedia 2016
High-pass filter, Wikipedia 2016
Low-pass filter, Wikipedia 2016
Amplifier, Wikipedia 2016
Plot, Wikipedia 2016
UART - Serial Communication, MikroElektroinika 2016
Libstock, MikroElektronika 2016