EDA senses ECG when ECG and EDA sensors are used together

Connection, ECG, EMG, EDA, etc.
MrFloppy
Posts: 10
Joined: Tue Oct 11, 2016 12:59 pm

EDA senses ECG when ECG and EDA sensors are used together

Postby MrFloppy » Mon Oct 31, 2016 11:50 am

I have build a device based on an Arduino Uno R3 using the Bitalino ECG and EDA. If both Sensors are connected to the Arduino but only the electrodes of 1 sensor are attached everything seems to be fine. But when I attach electrodes of both sensors the EDA is strongly influenced by the ECG.

I also attached a draft of the circuit.

I am using the following code:

Code: Select all

/* Includes */


//#define PLOTECG
//#define PLOTEDA
//#define PRINTJSON
#define PRINTCSV

/*sensor type*/
#define SENSOR_TYPE_EDA   0
#define SENSOR_TYPE_ECG   1

/* Timer constant */
#define TIMER_CHECK_INTERVALL 1

/* Analog read pins */
#define READ_ANALOG_PIN_EDA   0
#define READ_ANALOG_PIN_ECG   1

/* Digital write pins */
#define WRITE_DIGITAL_PIN_LED 13

/* Digital read pins */
#define READ_DIGITAL_PIN_BUTTON 12

/* Hardware constants */
#define ARDUINO_ADC 10
#define ARDUINO_VCC 3.3
#define ECG_GAIN 1100

/* Button state detection */
int buttonState = 0;
int previousButtonState = 0;

bool recording = false;

/* Number of values stored in the JSON array */
const int numberOfValuesConst = 10;

/* Arrays to store sensor data */
double edaValues[numberOfValuesConst];
double ecgValues[numberOfValuesConst];

/* Counts how many items were stored in the arrays in the actual period */
int counterValues = 0;

/* Used to calculate the timestamp */
unsigned long timeInMillis = 0;
unsigned long timeInMillisOffset = 0;

void printCSVLine(unsigned long timeInMillis) {
  //Serial.print(edaValues[counterValues]);
  Serial.print(timeInMillis);
  Serial.print(",");
  Serial.print(ecgValues[counterValues]);
  Serial.print(",");
  Serial.println(edaValues[counterValues]);
}

/* Sets pinmodes for led, button, eda and ecg sensor */
void setup() {
  Serial.begin(115200);
  pinMode(READ_ANALOG_PIN_EDA, INPUT);
  pinMode(READ_ANALOG_PIN_ECG, INPUT);
  pinMode (WRITE_DIGITAL_PIN_LED, OUTPUT);
  pinMode (READ_DIGITAL_PIN_BUTTON, INPUT);
}

void loop() {
  /* Used to calculate the next timer event */
  static unsigned long time;
 
  /* Used to calculate the timestamps */
  timeInMillis = millis() - timeInMillisOffset;

  /* Called when the timer is triggered */ 
  if(millis() - time >= TIMER_CHECK_INTERVALL) {
    if(recording) { 
      read_ecg();
      read_eda();
      /* Increases and resets the pointer to the array elements. */
      counterValues = (counterValues + 1) % numberOfValuesConst;
      /* Prints the JSON objects when the arrays are filled */
      #ifdef PRINTCSV
     
      printCSVLine(timeInMillis);
       
      #endif
      if (counterValues == 0) {
        #ifdef PRINTJSON
        //printJsonObject(timeInMillis, 1, 1000, numberOfValuesConst, edaValues);
        printJsonObject(timeInMillis, 2, 1000, numberOfValuesConst, ecgValues);
        #else
        #ifdef PLOTEDA
        Serial.println(calculateEda());
        #else
        #ifdef PLOTECG
        //Serial.println(analogRead(READ_ANALOG_PIN_ECG));
        Serial.println(calculateEcg());
       
       
        #endif
        #endif
        #endif               
      }
    }

    /* Listen for a button press */
    buttonState = digitalRead(READ_DIGITAL_PIN_BUTTON);
    /* If something has changed */
    if (buttonState != previousButtonState) {
      /* If the finger was released from the button */
      if (previousButtonState == HIGH) {
        /* If not recording yet */
        if (recording == false) {
          /* Add offset so that the timestamps start from 0 */
          timeInMillisOffset += timeInMillis;
          recording = true;
          #ifdef PRINTJSON
          printJsonPrefix();
          #endif
        }
        else {
          recording= false;
          #ifdef  PRINTJSON
          printJsonPostfix();
          #endif
        }   

      }
      previousButtonState = buttonState;
    }

    time = millis();

    /* Enlighten led when recording */
    if (recording)
      digitalWrite(WRITE_DIGITAL_PIN_LED, HIGH);
    else 
      digitalWrite(WRITE_DIGITAL_PIN_LED, LOW);
  }
}


/* Read from the eda sensor and store it in the array */
void read_eda() {
  /* Fakes some data */
  //edaValues[counterValues] = counterValues;
  edaValues[counterValues] = calculateEda();
}

double calculateEda() {
  double result = 0;
  double eda = analogRead(READ_ANALOG_PIN_EDA);
  //result = ((eda / pow(2, ARDUINO_ADC)) * ARDUINO_VCC - 0.259388) / 0.2;
  result = 1 - (eda/ (double)pow(2, ARDUINO_ADC));
  return (double)1 / result;
}

/* Read from the ecg sensor and store it in the array */
void read_ecg() {
  //edaValues[counterValues] = counterValues;
  ecgValues[counterValues] = calculateEcg();
}

double calculateEcg() {
  double result = 0;
  double ecg = analogRead(READ_ANALOG_PIN_ECG);
  //result = (((ecg / pow(2, ARDUINO_ADC)) - (.5)) * ARDUINO_VCC) / ECG_GAIN * 1000;
  result = ecg;
  result /= (double)pow(2, ARDUINO_ADC);
  result -= 0.5;
  result *= ARDUINO_VCC;
  result /= ECG_GAIN;
  result *= 1000;
  return result;
}

/* Creates a string representing the JSON object and prints it via serial interface. */
/* TODO: CHANGE LAST LINE TO SOMETHING WITH "lastItem" */
void printJsonObject(unsigned long timeStamp, unsigned int sensorType, unsigned int frequency, unsigned int numberOfValues, double valuesArray[]) {
  String jsonObject = "";
  jsonObject += "{\"timeStamp\":";
  jsonObject += timeStamp;
  jsonObject += ", \"sensorType\":";
  jsonObject += sensorType;
  jsonObject += ", \"frequency\":";
  jsonObject += frequency;
  jsonObject += ", \"numberOfValues\":";
  jsonObject += numberOfValues;
  jsonObject += ", \"values\":[";
  jsonObject += getStringFromValues(valuesArray);
  jsonObject += "]},";
  Serial.println(jsonObject);
}

/* Returns a String containing the whole array in a JSON array form. */
String getStringFromValues(double valuesArray[]) {
  String result = "";
  for (int i = 0; i < numberOfValuesConst - 1; i++) {
    result += valuesArray[i];
    result += ", ";
  }
  result += valuesArray[numberOfValuesConst - 1];
  return result;
}

void printJsonPrefix() {
  Serial.println("{\"Prototype1A\":[");
}

void printJsonPostfix() {
  Serial.println("{}]}");
}


Does anybody ever faced this problem or knows how to deal with it?
Attachments
IMG_0003 Kopie.JPG
Circuit diagram
IMG_0003 Kopie.JPG (178.18 KiB) Viewed 929 times
comparisonEcgAndEda.png
ECG influences EDA
comparisonEcgAndEda.png (81.42 KiB) Viewed 929 times

cazevedo
Posts: 18
Joined: Tue Feb 25, 2014 2:17 pm

Re: EDA senses ECG when ECG and EDA sensors are used together

Postby cazevedo » Thu Nov 03, 2016 6:06 pm

Hi there,

The problem seems to be correlated with the reference point that you are using.
I would recommend that you use the same reference point for both sensors, otherwise the resistors will be in parallel.
I also recommend that you use a buffer as shown in the diagram bellow.
IMG_20161103_175056.jpg
IMG_20161103_175056.jpg (187.47 KiB) Viewed 907 times


In order to avoid undesirable fluctuations on the reference voltage I also suggest that you use a voltage regulator before the VCC, because from your diagram seems that you are using a battery directly connected to the VCC.

Best regards,
Carlos Azevedo


Return to “Sensors”




cron