0% found this document useful (0 votes)
54 views16 pages

Activity 9 - ECG

The document provides instructions for setting up and using an ECG sensor with an Arduino. It describes how to install the necessary BioE Arduino library, connect the sensor to the Arduino and electrodes, and upload code to read ECG data. The code samples ECG signals at a configurable rate and broadcasts the voltage readings to a computer. The data can be viewed in the serial plotter or exported to Matlab for analysis. Three tasks are outlined to collect and analyze resting and exercise ECG data and investigate filtering the signals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views16 pages

Activity 9 - ECG

The document provides instructions for setting up and using an ECG sensor with an Arduino. It describes how to install the necessary BioE Arduino library, connect the sensor to the Arduino and electrodes, and upload code to read ECG data. The code samples ECG signals at a configurable rate and broadcasts the voltage readings to a computer. The data can be viewed in the serial plotter or exported to Matlab for analysis. Three tasks are outlined to collect and analyze resting and exercise ECG data and investigate filtering the signals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

ECG Sensor

BIOE 240

Developed By: Anthony Grana (2020)


Modified By: Elain Fu
1
Table of Contents
Table of Contents 2

Software Preparation 3
Installing the BioE Arduino Library 3

Wiring 5
Arduino connections 5
Electrode Connections 7

Software 9
Configurable variables 10
Uploading the Code 11

Looking at the Data 11


Serial Plotter 11
Putty and MatLab 12

2
Software Preparation

Installing the BioE Arduino Library


Installing a library is simple, and can make Arduino coding easier. The BioE240 library includes
the code for the ECG sensor and all of the dependencies for that code. The steps to install the
library are as follows:

1st. download the library (.zip) folder from the activity assignment in Canvas

2nd. In the Arduino IDE under, “Sketch>>Include_Library,” click “Add .ZIPlibrary…”

3rd. Navigate to and select the .zip library from your downloads folder.

3
4th. Wait for the library installed notification in Arduino.

5th. Check to make sure the library is installed by navigating through, “File>>Examples,” looking
for an example folder labelled, “BioE 2xx Library.” It should be near the bottom.

4
Wiring

The ECG we’re using has two connections that need to be made. The first is a 3.5mm audio
headphone jack. Although this uses an audio connector, this actually connects to the electrodes
that will take the measurements. The second connection is the power/signal connection to the
Arduino. This connection is made through the 3-pin beige connector on the other side of the
signal board.

Arduino connections
It’s recommended to connect to the Arduino before connecting to the electrodes. Start by
connecting the beige cable to the sensor board, but you will notice that the other side of the
cable can’t connect directly to the Arduino. To solve this we will use three male to male headers.
Connect the male to male headers to the female pins at the end of the cable, being sure to note
the colors. The other end of the male to male headers connect to the Arduino according to the
following wiring chart.

Blue -> A0
Red -> 5V
Blk -> Gnd

Lastly, make sure the Arduino is connected to your computer via the USB cable. This should
power the system.

5
6
Electrode Connections
The electrodes are simpler to connect. First, they need to be connected to the sensor board
through the 3.5mm audio jack. Second, each electrode cables needs to be snapped into
electrode pads.

7
Finally, the electrodes need to be peeled and stuck to your body. It is recommended to put the
electrodes on your torso, but if needed they can be connected to your limbs. Please refer to this
diagram for electrode placements.

8
Software

To use the ECG code written for this sensor select, “ECG_Vx.x.” under,
“File>>Examples>>BioE_Library.” This should pull up a new window with the ECG code. This
code reads the data coming from the ECG at a controlled interval, then converts it into a signal
that your computer can understand. The data being broadcast to the computer is the voltage
that the Arduino sees from the ECG after it has been amplified by the ECG sensor board. This
means that the data is in units of volts.

9
Configurable variables
There are some configurable variables within the code to change how it behaves.

The first is the timestamp definition. If this line is uncommented, the Arduino will automatically
add a timestamp to the data as it is streamed to your computer. This is not recommended if
you are plotting your data with the built-in serial monitor, but could be useful if you are doing any
post-processing in MatLab. To comment out this line of code, add a double forward slash (“//’)
before the “#define,” on line 30.

The next configurable variable is the number of decimal points that you want your data to have.
This variable is on line 38. Please keep in mind that the Arduino can only measure the ECG
signal with a resolution of 4.88 mV. So any number of decimal places beyond three does not
add any more information. Do not uncomment this line of code.

The last variable you might want to regularly change is the sampling frequency, or the number
of samples taken per second. Because the Arduino is a digital device, it can’t measure a signal
continuously over time. It has to take individual measurements one at a time. This is called a
discrete time signal. The variable “sampPerSec,” controls the approximate frequency of
samples per second that the Arduino will attempt to sample at. Please note that this sampling
frequency is not guaranteed. Although the Arduino will attempt to sample at that frequency, it
will likely be actually sampling at a slightly lower frequency. If you need to accurately measure
the frequency, use the timestamp definition listed above.

10
Uploading the Code
Before you upload the final code, you will need to check to make sure that the IDE knows where
to upload the code to. To do this, you need to make sure that “Arduino Uno.” is selected under,
“Tools>>Board.” Lastly, the COM port that the Arduino is connected to needs to be selected
under, “Tools>>Port.”

Lastly, click the upload button on the IDE.

Looking at the Data

Serial Plotter
The easiest way to view the data is with the serial plotter built into the Arduino IDE. For viewing
data with the serial plotter, the timestamp definition must be commented out. Refer to the
section of this document labelled “Configurable Variables,” to learn how to do this.

To view the data with the serial plotter, select, “Serial Plotter,” under tools in the Arduino IDE. It
should start plotting data immediately and the graph should look like this.

If the data is moving too fast, try reducing the sampling frequency variable.

11
Putty and MatLab
The data can also viewed, processed, and logged in matlab. If you are doing this, it is
recommended to enable the timestamp definition. To do this, after putty is installed, there are a
few settings that need to be configured in putty. First, on the putty opening screen, make sure
it’s in serial mode, the baud rate is set to 9600, and that the Arduino COM port is selected.

Second, in the menu on the left side of the application, select, “logging,” under session. Then
select, “All session output,” under Session logging. Lastly choose a file location and make sure
it saves as a csv file as opposed to a log file.

Then click the “open” button on the bottom of the application. This will start logging data to the
assigned csv file and give you a live feed of the data. To stop logging, simply close out of the
window. Note: This will only work if you do not have a serial monitor or serial plotter open
in the background.

The csv file can then be plotted and processed in excel, matlab, python, or any other data
processing software.

12
ECG Sensor Tasks

Task 1:
How can you use the time stamp feature to identify the approximate period of the ECG signal?
Do this and record data for at least 10 cycles of the ECG signal. You will analyze this data in
your HW, e.g., measure the average period, standard deviation, and coefficient of variation. (So
please save your data.)

Using time stamp will log tiem stamps in putty

Task 2:
Jog in place or perform jumping jacks for 1 minute (you may want to leave the electrodes in
place, but disconnect the audio jack from the sensor board) and record at least 10 cycles of the
ECG signal (again including the timestamp in your data). You will use this data in your HW to
compare the ECG signal post exercise to the resting state. (So please save your data.)

Task 3:
Run the sample sketch below, that has been modified to include a moving average filter, and
observe both the original ECG signal and the signal processed with a moving average filter.
(Note: Turn off the timestamp, so that you can view the signals in the serial plotter.) Take a
screen shot of the two signals for the following two cases: (i) the window size does not
appreciably change the moving average-filtered signal and (ii) the window size starts to
appreciably change the moving average-filtered signal. Further investigate the effects of
different window sizes (for a fixed sampling rate). Determine the “optimal” window size (or
range) for filtering the ECG signal. Report it here with a screen shot. Also, define what you
mean by optimal.

ECG Sensor at Rest


3.5
3
2.5
2
Voltage

1.5
1
0.5
0
2000 2500 3000 3500 4000 4500 5000 5500
Time (ms)

Raw Moving Average Filter

13
The optimal size is a window size of 5. This optimal because it removes a lot of the noise and
closely follows the curve.

/*********************************************************
Date: 5/19/2021
Title: ECGV2wfilter
Description:
ECGV1.0 (from AG) with a moving average filter incorporated.
For great arduino references go to https://www.arduino.cc/reference/en/
*********************************************************/

/*********************************************************
Hardware Wiring

Pin A0 -> Signal (blue wire)


Pin 5V -> Vcc (red wire)
Pin Gnd -> Gnd (black wire)

Left Chest -> Yellow electrode


Right Chest -> Red electrode
Right Waist -> Green electrrode
*********************************************************/

/**********************
Compiler Definitions
**********************/
//#define TimeStamp //uncomment this
line to add a timestamp to your datastream IN MILLISECONDS!

/***********************
Global Variables
***********************/
int sensePin = A0; //sensor pin
definition. Change this to change the sensor pin.
int decimalPoints = 3; //number of decimal
points that the data will have
int sampPerSec = 500; //approximate number
of samples per second

#ifdef TimeStamp
String delim = ", "; //delimiter
definition if there is a timestamp.
#endif

float Vref = 5.0; //Arduino voltage


reference. Typically 5V.
float voltScale = Vref / 1023.0l; //conversion factor
for voltage calculation

14
float delayTime = 1000.0 / float(sampPerSec); //calculation for
sample period

#define WINDOWSIZE 3
int INDEX = 0;
float measuredValue = 0;
float SUM = 0;
float READINGS[WINDOWSIZE]; //assumed initialized
to 0...
float AVERAGED = 0;

/***********************
Setup
***********************/
void setup() {
pinMode(sensePin, INPUT); //set to receive
signal through the sensePin.

Serial.begin(9600); //set communication


baudrate to the computer
}

/***********************
Loop
***********************/
void loop() {
measuredValue = (analogRead(sensePin))*voltScale; //read the value
of senseoPin, convert it to a voltage, and save it to a floating point
variable called "measuredValue"
Serial.print(measuredValue, decimalPoints); //print signal
voltage with the appropriate number of decimal points

SUM = SUM - READINGS[INDEX];

READINGS[INDEX] = measuredValue;
SUM = SUM + measuredValue;
INDEX = (INDEX + 1) % WINDOWSIZE;
AVERAGED = SUM / WINDOWSIZE;
Serial.print(", ");
Serial.println(AVERAGED,decimalPoints);

#ifndef TimeStamp
//If no time stamp is defined, start a new line before ending the loop.
Serial.print('\n');
#endif

#ifdef TimeStamp
//If a timestamp is defined, delimit the data, then print the current data
in milliseconds, then start a new line.
Serial.print(delim);
Serial.println(millis());
#endif

15
delay(delayTime);
//wait an aditional number of milliseconds
}

16

You might also like