glenzac
raw_ecg

Generating ECG/EEG signals with a microcontroller

This makes use of the inbuilt DAC of a microcontroller.The following are some microcontrollers with a DAC.

  • Arduino DUE
  • Particle Photon
  • Particle Electron
  • Gadget Renesas Sakura Board

For making this work on other microcontrollers without a DAC refer to the following posts or use an additional DAC peripheral IC.

The DAC is used to analogWrite the ECG values obtained from an online database: Physionet

I made an Excel sheet that automatically calculates the required DAC values based on the DAC resolution entered by the user. The database used in the document is MIT-BIH Normal Sinus Rhythm Record No: 16265 that is 10mins long and has 7680 sample values. The sheet also automatically produces comma separated values that can be directly stored into a microcontroller’s memory.

EXCEL_sRpjTDCvFx.jpg

You can choose the required number of values and also download and use a new database (just paste the new values in the ‘Raw Values’ column ).

I tested it on my GR Sakura board. I used only few values (about 10sec) to run the trial. I’ve also used 3 other databases to simulate problematic ECG conditions. The DAC outputs the required waveform based on an external potentiometer position. The database values are sampled at 128Hz hence the DAC needs to output each value at intervals of 7.8125 ms.

I’ve also added an additional hack to vary the ECG rate by pushing the values out faster from the DAC (by varying the delay - very crude I know :P ). This function too is achieved using a second potentiometer.

Code

[code language=“cpp”] // ---------------------------------------------------------------------- // Emergency Diver Alert System - Test Signals using GR SAKURA */ //\\\\\\\\\\ created by Glen Zachariah ******************// // glenzac@gmail.com //

// ---------------------------------------------------------------------- //

#include

float ma1;

const PROGMEM uint16_t DAC_normal_sinus[1173] = { 205,197,197,201,193,197,205,226,247,243,222,222,197,184,180,172,176,176,176,180,180,176,176,176,176,184,193,155,84,167,428,822,893,415,50,29,113,172,180,176,167,172,172,176,184,184,180,193,188,193,197,205,214,218,222,230,230,243,247,260,272,281,285,302,310,323,327,331,335,327,310,302,281,268,251,239,239,230,226,226,226,222,222,230,230,226,226,230,230,230,226,230,230,226,222,214,218,214,209,214,218,209,205, 214,239,268,268,251,243,230,209,205,197,197,197,193,201,197,193,201,197,201,205,201,100,159,415,877,910,302,25,75,172,214,201,188,193,193,193,201,197,201,201,205,214,214,218,226,230,239,243,243,256,264,272,285,293,298,310,323,339,348,356,365,360,348,335,314,306,285,272,260,251,247,243,239,243,243,247,251,247,247,247,239,243,247,247,247,247,247,243,235,239,230,226,226,226,226,226,218,222,222,230,264,289,281, 256,235,230,222,209,205,214,214,205,205,201,201,205,205,214,214,218,163,88,251,591,998,675,125,42,117,201,214,205,197,205,201,201,209,205,209,209,214,222,226,226,230,247,251,256,268,272,277,293,298,310,323,335,348,352,356,360,348,344,323,302,293,281,260,260,243,247,239,243,247,243,243,247,243,243,247,239,243,247,243,243,235,239,235,230,230,230,222,226,222,222,218,222,239,264,281,272,243,243,226,214,214,214, 205,205,201,205,205,205,214,209,218,218,121,109,277,671,1023,705,159,21,96,180,222,209,209,205,205,205,205,209,209,218,222,230,226,230,226,239,251,247,264,277,281,281,285,306,323,323,344,352,365,365,369,369,365,344,335,318,298,281,277,256,247,251,243,243,243,243,251,243,251,247,251,256,256,247,251,243,251,247,243,251,251,281,293,272,268,256,230,226,218,209,214,205,209,209,205,214,209,209,214,226,188,84,155, 499,965,893,310,21,54,134,201,214,201,193,201,201,201,205,205,218,214,214,222,222,230,230,247,251,251,260,264,285,289,293,314,323,327,335,339,352,348,331,323,302,289,272,247,243,239,230,235,235,230,230,226,230,235,235,239,239,239,239,230,235,235,230,230,226,235,243,260,272,256,243,230,209,205,193,193,197,193,193,201,197,193,197,193,205,201,130,113,251,566,948,776,230,25,71,167,205,193,188,180,188,188,193,201, 197,201,205,197,209,214,218,226,230,239,247,247,260,268,277,298,298,310,318,323,335,339,344,335,318,302,281,260,251,239,230,235,222,218,218,214,222,226,218,226,230,226,226,218,226,226,226,226,230,226,218,209,218,214,205,209,222,243,268,256,239,226,205,209,197,197,188,193,193,188,188,197,201,197,201,201,125,100,293,696,1002,516,54,37,134,201,201,193,193,188,197,193,197,197,197,205,209,209,214,218,226,230,235, 247,256,260,268,268,289,302,306,327,335,344,344,344,356,344,327,318,302,281,268,260,239,235,235,235,239,230,230,235,226,235,235,235,239,239,239,235,226,235,230,235,235,230,226,222,209,218,218,214,222,214,214,218,205,214,218,214,214,218,214,214,205,214,209,226,251,272,260,243,222,218,209,209,209,205,201,197,201,205,201,218,226,188,96,184,449,918,872,298,46,84,188,214,209,201,201,205,205,209,209,205,209,214,218, 226,222,230,230,235,247,251,264,272,281,289,298,302,318,335,348,365,369,373,373,365,352,339,323,310,298,272,260,260,243,247,239,243,247,247,247,243,243,243,243,247,251,243,243,235,239,243,239,243,235,230,222,218,222,222,222,222,222,222,218,209,218,222,218,226,222,218,222,218,218,222,222,222,226,218,222,218,222,239,268,289,272,256,247,230,226,218,214,218,218,218,214,209,214,214,218,230,222,142,138,293,633,939, 688,222,63,109,172,209,209,205,201,205,209,209,205,209,214,214,218,218,222,226,230,243,243,251,256,260,272,281,289,302,306,327,335,344,356,360,356,356,335,323,306,285,277,256,243,243,230,226,218,218,226,222,218,222,226,226,222,226,230,226,226,226,222,222,218,218,218,214,209,201,205,205,201,205,205,201,197,193,205,239,256,251,230,226,205,193,197,193,184,188,188,193,184,193,193,193,197,209,184,79,125,352,805,1015, 549,100,16,79,155,193,188,188,184,188,180,193,193,201,201,201,197,209,214,209,209,222,230,239,247,264,272,277,285,306,310,318,327,335,331,335,323,302,277,272,256,235,222,230,201,197,201,209,197,193,218,209,201,214,205,218,218,222,218,218,214,218,205,209,209,201,209,188,218,226,251,239,230,226,201,180,180,172,172,172,180,176,180,180,176,172,180,188,184,117,172,390,734,893,436,113,54,121,167,184,188,184,188,193, 184,201,201,197,214,222,218,214,239,243,247,243,256,260,268,277,289,293,302,314,318,331,335,348,339,335,318,310,277,260,247,230,218,214,205,188,193,184,197,188,188,201,201,197,201,205,209,205,201,209,205,201,205,201,188,201,218,235,251,230,222,214,184,184,176,176,180,180,172,176,176,180,176,180,184,193,142,58,151,470,918,969,293,0,29,121,172,180,176,176,180,180,172,176,180,184,184,197,201,201,197,205,218,222, 230,247,247,256,268,277,293,306,314,327,331,331,327,310,302,277,256,247,235,222,214,201,201,201,201,201,201,205,201,193,201,205,209,209,205

};

const PROGMEM uint16_t DAC_fibrillation[593] = { 163,159,157,159,159,161,159,160,158,155,159,161,158,158,159,159,159,157,157,155,157,159,159,157,156,157,155,156,155,155,153,156,155,158,155,153,153,151,152,151,155,155,152,152,152,151,155,156,153,151,149,152,152,150,152,156,150,145,152,152,157,151,152,150,150,150,152,149,148,148,150,150,149,149,151,150,151,151,159,157,160,158,164,167,161,163,159,160,158,155,148,150,145,142,140,143,145,150,147,147,144,147, 147,143,143,148,148,147,145,142,145,145,144,145,144,150,149,151,143,147,164,181,205,213,217,209,218,205,174,134,110,114,126,136,141,145,152,159,158,156,152,152,147,150,152,159,155,152,151,150,150,153,152,155,155,156,155,164,161,164,166,167,158,156,164,161,163,160,169,172,171,169,167,180,182,185,185,178,176,180,180,176,184,182,182,186,191,190,186,191,193,194,198,191,200,194,186,186,189,188,183,185,185,181, 177,177,175,169,172,173,168,166,167,165,168,161,163,164,164,165,167,168,165,167,165,159,161,165,166,168,168,163,158,160,161,161,161,166,165,159,164,164,158,161,168,163,165,168,161,152,157,155,164,165,167,161,157,155,150,150,157,152,156,160,156,164,163,157,160,155,153,160,159,151,149,151,152,153,151,155,159,159,156,150,151,149,149,150,157,156,157,158,155,155,155,155,158,157,156,158,161,157,155,153,155,161, 155,156,157,159,156,158,165,168,169,175,167,160,159,156,157,155,155,155,153,155,153,148,155,155,149,155,158,155,155,153,158,158,158,153,153,157,158,157,159,159,161,166,164,164,164,165,182,190,211,219,226,226,227,225,218,185,155,132,135,145,153,164,169,168,173,173,176,175,172,164,158,165,166,167,169,171,171,173,171,174,174,173,173,171,174,169,171,177,181,180,176,175,181,181,182,186,190,185,182,183,191,189, 186,192,190,188,194,200,198,205,205,203,203,205,206,202,202,210,211,214,213,206,210,210,207,209,208,215,210,209,206,203,201,197,197,194,194,192,200,194,198,199,193,188,189,189,189,188,188,192,183,186,188,189,191,192,189,192,190,193,192,190,188,190,185,189,189,191,189,184,189,189,186,190,193,191,190,191,188,191,189,191,192,189,185,188,188,192,191,188,183,190,191,186,189,191,188,191,188,192,191,191,188,190, 191,189,192,184,186,190,190,189,191,186,186,189,198,189,191,193,193,193,191,191,191,185,188,191,192,193,196,200,200,205,206,205,205,205,202,200,194,189,183,186,190,184,182,181,184,180,183,190,196,199,202,213,213,200,185,175,171,178,182,188,189,181,178,178,182,188,184,185,185,200,218,243,253,257,252,253,246,224,188,157,145,151,163

};

const PROGMEM uint16_t DAC_Supraventricular_Arrhythmia[511] = { 199,197,199,199,201,201,201,206,203,206,206,206,208,206,215,217,215,217,217,217,222,226,228,231,233,242,256,258,265,272,281,290,292,297,297,292,285,269,256,242,222,215,201,194,185,190,192,185,183,183,185,181,183,188,185,188,188,190,194,185,192,197,197,199,201,199,197,194,201,197,197,199,197,197,194,197,197,194,197,194,197,199,194,192,197,192,192,194,197,194,194,192,199,194,199, 197,197,197,197,194,197,197,199,199,194,199,199,194,197,199,199,201,199,197,201,197,201,201,197,201,197,203,203,199,199,201,201,201,199,203,203,199,199,210,208,206,203,206,206,203,206,206,206,208,203,203,203,201,203,203,203,208,197,217,267,388,526,549,449,303,158,103,110,101,97,115,147,188,192,192,192,190,201,194,194,201,199,201,203,201,201,206,206,210,208,213,213,213,213,217,219,219,224,231,233,238,247, 249,267,274,276,283,290,288,285,283,269,258,240,231,215,206,199,197,190,190,190,188,190,185,185,188,190,192,192,190,197,192,199,199,201,201,197,203,201,199,199,203,201,203,199,203,203,199,199,203,201,201,199,201,201,199,201,201,199,201,199,206,201,201,203,197,201,203,203,201,201,201,203,203,197,208,208,208,203,203,206,208,203,206,206,206,208,203,208,208,206,210,208,208,208,208,208,210,208,210,210,213,213, 210,210,222,222,213,215,213,215,210,215,217,213,215,215,213,208,203,210,217,217,217,217,210,242,308,451,535,524,406,260,115,124,128,126,122,138,167,201,206,203,201,206,208,210,206,210,210,210,210,213,215,215,215,217,215,222,219,224,224,226,231,235,235,242,247,253,263,269,278,285,290,297,297,301,294,290,278,267,244,235,222,215,210,201,197,197,197,194,192,192,197,194,197,197,194,201,197,201,206,206,208,208, 210,208,208,210,210,213,206,213,210,213,208,208,208,210,208,206,206,203,208,206,208,206,203,208,208,210,203,203,206,208,206,206,203,208,206,206,203,208,208,208,203,208,206,201,210,208,206,210,210,210,206,208,208,206,208,208,206,208,208,208,213,208,210,208,213,210,210,215,213,208,219,210,208,208,213,215,210,210,215,210,206,208,206,208,210,203,217,210,235,303,448,551,529,383,238,97,115,117,110,110,135,172, 197,199,194,194,197,199,201,199 };

const PROGMEM uint16_t DAC_Sudden_Cardiac_Death[596] = { 218,216,218,217,221,223,222,221,219,221,222,223,225,222,223,225,227,227,230,235,243,253,261,268,273,276,280,282,284,285,286,288,289,291,297,297,299,296,291,282,274,267,257,252,248,248,247,246,246,244,244,244,244,243,244,248,248,250,246,248,249,249,249,249,256,258,266,269,272,271,273,288,298,314,324,336,350,375,400,440,481,509,527,523,523,508,481,416,319,231,171,152,163,175,183,186,194,205,214,217,217,215,215, 214,210,209,209,213,213,217,217,214,213,213,214,211,213,211,213,213,215,213,214,219,216,219,218,214,210,209,210,211,211,208,208,209,213,213,211,214,209,208,207,206,206,203,209,207,209,209,208,208,211,217,219,222,222,224,225,231,238,243,247,248,251,251,252,248,250,251,251,248,247,246,243,242,238,235,230,223,221,219,216,210,207,202,203,207,211,216,217,221,217,218,218,219,221,222,226,224,231,234,236,238,238,238, 235,241,246,255,261,273,282,293,314,336,372,402,436,454,469,488,506,532,522,481,399,303,225,172,160,163,166,166,161,163,168,173,173,173,173,175,177,180,177,173,174,175,176,174,173,173,171,168,166,169,173,175,175,177,180,178,180,182,182,182,183,182,182,177,174,175,176,176,172,171,169,168,172,173,180,176,177,178,181,180,180,182,186,189,189,192,196,200,207,214,218,214,213,213,216,217,221,225,221,221,223,224,222, 218,219,218,217,217,218,219,219,216,215,216,214,214,214,214,213,211,208,210,209,208,203,201,205,200,199,198,200,200,200,200,198,196,192,192,192,196,197,198,196,192,190,189,189,191,190,193,189,190,186,185,184,184,188,186,189,188,192,197,197,192,192,194,194,196,193,194,196,193,197,194,196,196,192,188,184,185,185,186,190,191,191,189,188,189,191,194,194,191,190,189,197,199,200,203,207,211,215,219,218,224,233,240, 240,242,250,252,255,256,260,263,265,266,263,259,248,241,232,226,221,217,219,211,209,208,213,217,221,230,231,233,231,234,239,252,266,284,298,313,328,350,390,431,474,497,507,512,502,476,399,308,213,132,93,86,101,117,125,135,145,156,163,167,167,164,163,164,163,164,167,171,169,171,173,172,172,169,171,171,172,177,171,169,164,163,163,161,158,157,157,153,153,155,159,161,159,158,152,152,155,156,155,153,155,145,144,147, 145,147,144,145,144,143,147,150,155,155,157,156,158,158,163,165,168,171,176,181,180,182,186,191,196,200,201,205,203,203,203,202,205,207,208,206,206,206,207,207,210,214,216,218,219,219,221,224,230,225,226,225,230,231,233,234,235,240,238,238,236,238,236,236,240,235,233,230,225,224,225,227,221,221,221,222,223,222,222,218

};

void setup() { pinMode(PIN_LED0,OUTPUT); // initialize all on-board LED’s pinMode(PIN_LED1,OUTPUT); pinMode(PIN_LED2,OUTPUT); pinMode(PIN_LED3,OUTPUT); Serial.begin(9600); };

void normal_sinus() { for (int i = 0; i<1173 ; i++) { analogWriteDAC(53, DAC_normal_sinus[i]); delay(ma1); }; };

void fibrillation() { for (int i = 0; i<593 ; i++) { analogWriteDAC(53, DAC_fibrillation[i]); delay(ma1); }; };

void Supraventricular_Arrhythmia() { for (int i = 0; i<511 ; i++) { analogWriteDAC(53, DAC_Supraventricular_Arrhythmia[i]); delay(ma1); }; };

void Sudden_Cardiac_Death() { for (int i = 0; i<596 ; i++) { analogWriteDAC(53, DAC_Sudden_Cardiac_Death[i]); delay(ma1); }; };

void loop() { int a1=analogRead(A0); int a2=analogRead(A1); ma1=map(a1,0,675,4.8,7.8); Serial.println(a1); Serial.println(ma1);

if(a2 100 && a2300 && a2500) { Sudden_Cardiac_Death(); };

};

[/code]

Output images:

raw_ecg.jpg

P1.jpg

P2.jpg

P3.jpg

Comments