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.
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.
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 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:
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.
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:
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.
The mikroPlot app it’s quite easy to use.
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.
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.
The reset button is pretty self-explanatory, but let’s see what happens once you click it:
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.
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 about sections shows additional information about the application, it can be closed by hitting the ESC key.
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:
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.
RCC_APB1ENR.TIM2EN = 1;
TIM2_CR1.CEN = 0;
TIM2_PSC = 5;
TIM2_ARR = 54599;
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;
uint16_t adc_read = 0;
uint32_t i = 0;
char final_string ;
temp_adc_read = temp_timer_read = 0;
GPIO_Analog_Input( &GPIOA_BASE, _GPIO_PINMASK_4 );
GPIO_Digital_Input( &GPIOD_BASE, _GPIO_PINMASK_10 );
// Uart1_Write_Text("Uart initialized \r\n"); // this is for testing only, it should stay commented when implementing ECG reading
if (Button(&GPIOD_IDR, 10, 10, 1)) // if the button is pressed, interrupts are activated and the measuring begins
if (read_flag == true) // every 3.9ms, measure data, measure time, and send them to the PC
temp_adc_read = ADC1_Get_Sample(4);
temp_timer_read = interrupt_ctr*3.9;
read_flag = false;
if (seconds_counter == 15) // after 15 seconds of measuring is done, disable interrupts and finish
void Timer2_interrupt() iv IVT_INT_TIM2
TIM2_SR.UIF = 0;
if (interrupt_ctr % 256 == 0)
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_value\r\n”. 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:
If you want to measure and plot data again, reset your board, and press the button (D10) again if the serial port is connected.
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
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!
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