How do i plot ECG sensor data from arduino to ECG graph using matlab?

36 views (last 30 days)
Can anyone share with the code on how to plot ECG sensor data into ECG graph using Matlab visualization in Thingspeak. I am able to do using Processing IDE.I want to do the same in Thingspeak.
Teh processing code is https://github.com/sparkfun/AD8232_Heart_Rate_Monitor. I want the equivalent matlab code. I am not able to get the equivalent.
Please someone help me.
  2 Comments
Hans Scharler
Hans Scharler on 20 Feb 2019
Are you using an Arduino to collect data? I recommend an Arduino MKR 1000 or 1010. These devices have Wi-Fi and can send data to ThingSpeak using our official library.
Sai Pavan Nunna
Sai Pavan Nunna on 20 Feb 2019
Yes, I am using Arduino Uno. I am not able to send ECG data to Thingspeak.please help me.

Sign in to comment.

Answers (2)

Madhu Govindarajan
Madhu Govindarajan on 22 Feb 2019
You can search through the MATLAB File Exchange to see if there are pre-existing libraries that you can use. If not, you will have to first write the MATLAB Code equivalent of the arduino sketches yourself to bring in the ECG data to MATLAB or write custom add-on libraries to bring this in using existing arduino libraries (https://www.mathworks.com/help/supportpkg/arduinoio/ug/custom-arduino-add-on-device-library-and-code.html).
I would recommend posting the code you want converted here in MATLAB answers and ask for help, as some experts might be able to help you with that. Once you have the code to bring in the data to MATLAB, then it should be easy to send data to ThingSpeak.
  2 Comments
Sai Pavan Nunna
Sai Pavan Nunna on 23 Feb 2019
Edited: Sai Pavan Nunna on 23 Feb 2019
The processing IDE code for ECG Sensor is as shown below . I know on how to get the data from the sensor to Matlab. I want to how to convert the input signals to the ECG waves done by the code.
[code]
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
float height_old = 0;
float height_new = 0;
float inByte = 0;
int BPM = 0;
int beat_old = 0;
float[] beats = new float[500]; // Used to calculate average BPM
int beatIndex;
float threshold = 620.0; //Threshold at which BPM calculation occurs
boolean belowThreshold = true;
PFont font;
void setup () {
// set the window size:
size(1000, 400);
// List all the available serial ports
println(Serial.list());
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0xff);
font = createFont("Ariel", 12, true);
}
void draw () {
//Map and draw the line for new data point
inByte = map(inByte, 0, 1023, 0, height);
height_new = height - inByte;
line(xPos - 1, height_old, xPos, height_new);
height_old = height_new;
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0xff);
}
else {
// increment the horizontal position:
xPos++;
}
// draw text for BPM periodically
if (millis() % 128 == 0){
fill(0xFF);
rect(0, 0, 200, 20);
fill(0x00);
text("BPM: " + inByte, 15, 10);
}
}
void serialEvent (Serial myPort)
{
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null)
{
// trim off any whitespace:
inString = trim(inString);
// If leads off detection is true notify with blue line
if (inString.equals("!"))
{
stroke(0, 0, 0xff); //Set stroke to blue ( R, G, B)
inByte = 512; // middle of the ADC range (Flat Line)
}
// If the data is good let it through
else
{
stroke(0xff, 0, 0); //Set stroke to red ( R, G, B)
inByte = float(inString);
// BPM calculation check
if (inByte > threshold && belowThreshold == true)
{
calculateBPM();
belowThreshold = false;
}
else if(inByte < threshold)
{
belowThreshold = true;
}
}
}
}
void calculateBPM ()
{
int beat_new = millis(); // get the current millisecond
int diff = beat_new - beat_old; // find the time between the last two beats
float currentBPM = 60000 / diff; // convert to beats per minute
beats[beatIndex] = currentBPM; // store to array to convert the average
float total = 0.0;
for (int i = 0; i < 500; i++){
total += beats[i];
}
BPM = int(total / 500);
beat_old = beat_new;
beatIndex = (beatIndex + 1) % 500; // cycle through the array instead of using FIFO queue
}
[/code]

Sign in to comment.


Madhu Govindarajan
Madhu Govindarajan on 25 Feb 2019
Here are my thoughts on this topic and unfortunately I am not a subject matter expert on this.
This video shows how we built a heart rate detector using Arduino and MATLAB to analyze ECG data. When you bring in ECG data as an analog voltage into an Arduino, usually you need a high sample rate (in our case 200 Hz) to be able to see the P wave QRS complex and T wave. When you have a code running on Arduino and send data serially over to MATLAB it is difficult to achieve those speeds reliably (best we got was around 60 Hz).
This is why we chose the Simulink Support package for Arduino workflow. As shown in the video, you can bring in Analog voltage data at 200 Hz and then use ThingSpeak blocks to log data to the cloud.
HTH,
Madhu

Communities

More Answers in the  ThingSpeak Community

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!