ECG click: Arduino IDE code examples


My latest post was about two ECG acquisition boards, one from MikroElektronika, the other from Olimex. Today I will continue to further explore the ECG click, and I will provide some code examples that work in Arduino IDE, using one Arduino Uno and one Arduino Uno click shield.

BEFORE WE BEGIN, A FAIR WARNING: THESE ARE DEVELOPMENT BOARDS USED TO LEARN HOW ELECTROCARDIOGRAPHY WORKS, FROM THE POINT OF VIEW OF SIGNAL PROCESSING AND ELECTRONICS. THEY ARE NOT TOOLS USED TO MAKE A DIAGNOSIS, AND CERTAINLY ARE NOT MENT TO REPLACE AN ACTUAL ECG MACHINE. THEY ARE NOT CALIBRATED. NOR THEY ARE CERTIFIED FOR MEDICAL USE. PLAYING WITH ELECTRICAL SIGNALS AROUND YOU HEART CAN BE DANGEROUS, AND YOU SHOULD KNOW WHAT YOU ARE DOING BEFORE STARTING TO USE THESE BOARDS. I’M NOT A DOCTOR, AND I CAN’T PROVIDE ANY MEDICAL ADVICE.

ECG BOARDS SUCH AS THOSE I HAVE IN TEST TODAY CAN INTERFERE WITH PACEMAKERS. AS THE BOARDS ARE NOT CERTIFIED FOR MEDICAL USE, THERE’S NO WAY TO TELL WHAT CAN HAPPEN IN SUCH SITUATIONS.{alertError}

As a prerequisite, the JP1 jumper must be moved into the “4096” position, to increase the gain in order to match the capabilities of the A/D converter of the ATMEGA328P. Unless you have some SMD soldering tweezers, like I do, this step is quite nasty. You can also choose to leave the jumper as it is, but the amplitude of the recorded signal will be much lower.

And now comes the fun part: the code…

NOTE: ALL THE EXPERIMENTS BELOW WERE PERFORMED ON A NOTEBOOK, ON BATTERY POWER. I DON’T RECOMMEND USING AN MAINS-POWERED PC, BOTH FOR YOUR OWN PROTECTION AND BECAUSE YOU WILL HAVE A HUGE NOISE PICKUP FROM THE POWER LINES.{alertError}

Part I: using Arduino Serial plotter

The simplest and easiest thing to do is use the Serial plotter from Arduino IDE. Assuming a 256Hz sampling rate, the following code performs A/D conversion and then sends data over the serial interface.

 // ECG click: plotting data using Serial plotter from Arduino IDE

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(57600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  // about 256Hz sample rate
  delayMicroseconds(3900);
}>

If you look closely at this code, it’s just a slightly modified version of the AnalogReadSerial example. At the first try I got this:

ECG click: Arduino Serial Plotter example

Simple as it might be, the waveform looks quite nice.

Part II: EEG click, Arduino Uno and Mikroplot

OK, let’s complicate things a little bit. The following code is designed to work with MikroPlot, a data visualization software developed by MikroElektronika, used in their ECG click examples. You can find all documentation for MikroPlot on http://learn.mikroe.com/ecg-click-mikroplot-complete-solution-human-heart-data-analysis/.

My Arduino IDE code for MikroPlot is:

int EEG_Value;
float mytime = 0;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(57200);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
 }
  Serial.println("START");
}

// the loop routine runs over and over again forever:
void loop() {
 int EEG_Value = analogRead(A0);
 Serial.print(EEG_Value);
 Serial.print(",");
 Serial.println(mytime);
 mytime = mytime + 3.90;
 delayMicroseconds(3900);
}

and the acquired waveform looks like this:

ECG click: MikroPlot example

Nice, but I want more…

Part III: EEG click, Arduino Uno and Electric Guru

Electric Guru is a neurofeedback software. It can be used to view not only ECG, but also EEG waveforms. It also provides some nice statistics. The only issue is that is quite old, and it recognizes only COM1 to COM4 for communication. So, I have to change my COM port used by Arduino Uno to fit in this range.

Arduino Uno: changing the COM port

OK, this looks complicated but in fact is quite simple to do.

In Win10, the first step is to open the Device Manager.

Arduino Uno: changing serial port. Step 1- Device manager
 

Then we select the corresponding COM port of out Arduino Uno and we go to “Properties”.

Arduino Uno: changing serial port. Step 2 – Select port

Arduino Uno: changing the serial port. Step 3 – Port settings

We then click on the “Advanced” button and from there we can change the port – I will set it to COM2.

Arduino Uno: changing the serial port. Step 4 – Advanced settings


Arduino Uno: changing the serial port. Step 5 – New serial port

Before starting Electric Guru, here’s the Arduino IDE code:

        
            /* ECG click Arduino code for use with Electric Guru software

            Inspired from an example code by Olimex
            and adapted for use with ECG click

            See the original code at
            https://www.olimex.com/Products/Duino/Shields/SHIELD-EKG-EMG/

            Electric Guru can be downloaded from the above page or from
            http://www.realization.org/page/topics/electric_guru.htm


            ***********************************************************
            * ModularEEG firmware for one-way transmission, v0.5.4-p2
            * Copyright (c) 2002-2003, Joerg Hansmann, Jim Peters, Andreas Robinson
            * License: GNU General Public License (GPL) v2
            * http://openeeg.sourceforge.net/doc/modeeg/modeeg.html
            ***********************************************************

            For proper communication packet format given below have to be supported:
            ///////////////////////////////////////////////
            ////////// Packet Format Version 2 ////////////
            ///////////////////////////////////////////////
            // 17-byte packets are transmitted from Olimexino328 at 256Hz,
            // using 1 start bit, 8 data bits, 1 stop bit, no parity, 57600 bits per second.

            // Minimial transmission speed is 256Hz * sizeof(Olimexino328_packet) * 10 = 43520 bps.


            struct ECG_packet
            {
              uint8_t	sync0;		// = 0xa5
              uint8_t	sync1;		// = 0x5a
              uint8_t	version;	// = 2 (packet version)
              uint8_t	count;		// packet counter. Increases by 1 each packet.
              uint16_t	data[6];	// 10-bit sample (= 0 - 1023) in big endian (Motorola) format.
              uint8_t	switches;	// State of PD5 to PD2, in bits 3 to 0.
            };
            */

            // All definitions
            #define NUMCHANNELS 1
            #define HEADERLEN 4
            #define PACKETLEN (NUMCHANNELS * 2 + HEADERLEN + 1)


            // Global constants and variables
            volatile unsigned char TXBuf[PACKETLEN];  // The transmission packet
            volatile unsigned char TXIndex;           // Next byte to write in the transmission packet.
            volatile unsigned char CurrentCh;         // Current channel being sampled.
            volatile unsigned char counter = 0;	      // Additional divider used to generate CAL_SIG
            volatile unsigned int ADC_Value = 0;	    // ADC current value
            float current_micros;
            float next_micros;

            void setup() {

             //Write packet header and footer
             TXBuf[0] = 0xa5;    //Sync 0
             TXBuf[1] = 0x5a;    //Sync 1
             TXBuf[2] = 2;       //Protocol version
             TXBuf[3] = 0;       //Packet counter
             TXBuf[4] = 0x02;    //CH1 High Byte
             TXBuf[5] = 0x00;    //CH1 Low Byte
             TXBuf[6] = 0x02;    //CH2 High Byte
             TXBuf[7] = 0x00;    //CH2 Low Byte
             TXBuf[8] = 0x02;    //CH3 High Byte
             TXBuf[9] = 0x00;    //CH3 Low Byte
             TXBuf[10] = 0x02;   //CH4 High Byte
             TXBuf[11] = 0x00;   //CH4 Low Byte
             TXBuf[12] = 0x02;   //CH5 High Byte
             TXBuf[13] = 0x00;   //CH5 Low Byte
             TXBuf[14] = 0x02;   //CH6 High Byte
             TXBuf[15] = 0x00;   //CH6 Low Byte
             TXBuf[2 * NUMCHANNELS + HEADERLEN] =  0x01;	// Switches state

             // Set serial port at 57600 baud
             Serial.begin(57600);
             while (!Serial) {
                ; // wait for serial port to connect. Needed for native USB
             }
            }

            void loop() {
              // Read ADC and update TX buffer
              for(CurrentCh = 0; CurrentCh < 6; CurrentCh++){
                ADC_Value = analogRead(CurrentCh);
                TXBuf[((2*CurrentCh) + HEADERLEN)] = ((unsigned char)((ADC_Value & 0xFF00) >> 8));  // Write High Byte
                TXBuf[((2*CurrentCh) + HEADERLEN + 1)] = ((unsigned char)(ADC_Value & 0x00FF)); // Write Low Byte
              }

              // Send Packet
              for(TXIndex=0; TXIndex < 17; TXIndex++){
                Serial.write(TXBuf[TXIndex]);
              }

              // Increment the packet counter
              TXBuf[3]++;

              delayMicroseconds(3900); // aproximately 256 Hz
            }
        
    

We have the code, we have the correct COM port, we can start the Electric Guru. There, we go first to Preferences – Serial port and we make the required settings.

Electric Guru: Selecting COM port
Then we go to File – Open Protocol and we load “ElecGuru.proto”.

Loading protocol in Electric Guru
We are now ready to start the ECG. All we need to do is clock on the green traffic light, just under the file menu.

ECG click: on Electric Guru


Post a Comment

0 Comments