Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Components and supplies

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
Arduino Nano 33 BLE Sense
×1
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
SparkFun Single Lead Heart Rate Monitor - AD8232
×1
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
ElectroPeak 0.96" OLED 64x128 Display Module
×1

Necessary tools and machines

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
Soldering iron (generic)
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
Solder Wire, Lead Free
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
10 Pc. Jumper Wire Kit, 5 cm Long

Apps and online services

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
Edge Impulse Studio
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
MATLAB
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
Arduino IDE
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
Microsoft Visual Studio 2017

About this project

Sudden heart attack and increase in fatality : Growing Concern

In a past decade, the sudden heart attack deaths were increased tremendously.

Particularly in a developing nations like India, apart from genetic and life styles, lack of medical resources in rural areas causes most of the fatality in heart attack.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Technology contribution to solve this global problem:

I worked on a TinyML application powered by Edge Impulse to develop a mini-Diagnosis ECG analyzer device which can fit in a pocket and it can diagnose heart diseases independently without a cloud connectivity.

Present ECG Analyzer machines in market and its features

•Present IoT Medical device sends bulk ECG data to the mobile/server and analysis is done in high processor / mobile App

•Computer based application which receives signals from ECG device and analyze the ECG patterns

•All the ECG analyzing device has dependency on Internet or high processing computers/ Mobile application.

So this can be summarized in below chart;

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Key Solution

•ECG Analyzer powered by Edge Impulse will analyze the ECG data without any dependency on the Internet.

•Latency is lowest compared to IoT devices

•A 15Kb Rom - ECG Analyzing TinyML model can run on any TinyML supported microcontrollers.

•The device will analyze ECG patterns and classify into Normal, Atrial Fibrillation and First-Degree heart block

Architecture

The ECG analyzer involves

1. ECG reading using AD8232

2. Simulating different heart disease ECG patterns

3.Novel approach to produce quality datasets

4.Training the Model

5.Model test accuracy andIntegratingapplication code with deployedlibrary

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Know about ECG graph

Before proceeding with the technical work, first will go through some basics of ECG graph.

The ECG graph was split into 5 Waves - P, Q, R, S and T waves.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Atrial Fibrillation

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

First-Degree Heart Block

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Lets build the ECG Analyzer

1. ECG reading using AD8232

Connect the ECG sensor AD8232 to the Arduino Nano 33 BLE sense as per the below connection diagram.

Flash the below code and then press "Ctrl+Shift+L" to visualize the graphical streaming data.

void setup() {
// initialize the serial communication:
Serial.begin(115200);
pinMode(2, INPUT); // Setup for leads off detection LO +
pinMode(3, INPUT); // Setup for leads off detection LO -

}

void loop() {

if((digitalRead(2) == 1)||(digitalRead(3) == 1)){
Serial.println('!');
}
else{

// send the value of analog input 0:
Serial.println(analogRead(A0));
}
//Wait for a bit to keep serial data from saturating
delay(5);
}

1.1 Placement of ECG electrodes

The ECG electrodes are placed in RA, LA and LL as mentioned in above diagram and connect the jack to the AD8232 sensor on the ECG Analyzer.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

2.Simulating different ECG patterns using Matlab-signal builder

Step2.1 : Save the normal ECG data in Excel

First copy and save the serial monitor data into the excel file as below template. The ECG value should be in second column under 'Y'. The first column is time series. it should be incremented as ( * 0.005) 5ms.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Step2.2 : Signal builder in Matlab

Create a new Simulink model in Matlab

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Then type 'signal builder' in workspace and select it. Also insert

the 'scope' to connect it to the signal builder. Please refer the below screenshot.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

To load the saved excel data, open the signal builder and select 'import from file' option.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Please select the mentioned options to import the data.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Then confirm the selection and import without saving the model. since we need to some more steps.

2.3 : ECG Data visualization in signal builder

A 60 second ECG data will be look like this in signal builder view, For manual editing, Please zoom it for 5 seconds data and the do 'drag' to edit the ECG wave.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

After Zooming, when you place the mouse pointer close to the ECG wave, you can able to select any points in the ECG and drag it as per your application requirement.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

2.4 : Editing P-R interval data in Signal builder for AV Block 1 Case

I have manually drag down the P wave and shift the P wave much before the R wave, so that the P-R interval exceeds 200ms.

Repeat this step in different timeline in signal builder data.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

After editing, Export the data into the mat file. you can find this option in signal builder. Once the mat file is saved, Kindly follow the below steps.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Steps to follow --> Double click mat file first and double click the 1x1 Dataset--> you can see the modified data under Data:1.

Copy and paste the data in new excel file.

Step2.5 : Export the excel data into Array

I have wrote a.m script which can convert the excel data into a array.

Run this script in Matlab, before running replace the ExcelFilename with your local saved file name and ExcelSheetName into a corresponding sheet name.

data=xlsread('ExcelFilename.xlsx','ExcelSheetName');

ECGExtract=(data(1:end,1)); %%Column A data

fid=fopen('test.txt','wt');%opening with the t flag auto-converts \n to \r\n on Windows
fprintf(fid,'{');
FormatSpec=[repmat('%i ',1,size(ECGExtract,2)) ','];%or should that have been \r\n instead?
fprintf(fid,FormatSpec, ECGExtract);
fprintf(fid,'}')
fclose(fid);

The text file will be generated in the current directory location.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Copy the array content and paste it in the ECGAnalyzer.c code for simulating Atrial fibrillation and First-Degree heart block.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

3. Novel approach to produce quality datasets

If you look at ECG data, its really hard to distinguish the different heart conditions ECG data with normal ECG data in a lesser window time(example: 3 secs)

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

When I train a model with Filtered ECG Data alone for atrial fibrillation, Normal and First-Degree Heart block, The accuracy was less than 23%. The reason was in the shorter window the model can't differentiate the difference.

If I go for longer window time, the processing time and peak RAM usage was increased quite a lot. Even There was no accuracy.

Background of Novel approach:

When a doctor or Trained person try to analyze the ECG graph, They will be counting the small boxes between R to R wave, P to R interval and write it down the counts in the graph or memories it for calculation.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

I created a separate waveforms from filtered ECG data.

New waveforms:

  • R-R Interval
  • PR Interval
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Generated Datasets for Normal ECGdata :

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

The decoded R-R interval and PR interval data is always 100 and 50 for normal ECG Data.

Generated Datasets for Atrial Fibrillation- ECG Data:

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Whenever there is a deviation between previous R-R interval and present R-R interval, R-R interval data will be dropped to -100 for one cycle.

Generated Datasets for First-Degree Heart Block-ECG Data:

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Whenever the P to R interval exceeds 200ms, then PR Interval data will be dropped to -50 for one cycle.

This approach improved my model accuracy to greater than 90%.

4. Training the Model in Edge Impulse

Before getting into Edge Impulse training ML, we need to configure some parameters in library file for simulation and real time ECG sensor reading.

step4.1 : configure the SIMULATION to 0, If the data acquisition was from real time ECG sensor reading

or

Configure the SIMULATION to 1, If the data acquisition was from simulated ECG data for Atrial fibrillation and First-Degree Heart Block( followed Matlab sessions).

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Step4.2 : Comment/uncomment the required databuffer for simulation

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Step4.3 : Data acquisition

Collect the ECG data under three different labels : Normal, Atrial fibrillation and First-Degree Heart Block

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

https://docs.edgeimpulse.com/docs/arduino-nano-33-ble-sense

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

and select the frequency as 202

$ edge-impulse-daemon --frequency 202

Step4.4 : Create Impulse

In a create impulse section, window size is 3000ms and window increase is 2999ms, select the k-means anomaly detection

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Step 4.5: Spectral features

In a spectral features, select the filter type as None.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Step 4.6 : NN Classifier:

I have set 40 number of training cycles and learning rate as 0.005. I have got 92.9 accuracy.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

and for Anomaly detection, I have selected PR interval RMS and RR interval RMS.

5. Model test accuracy & Integration

In a model test accuracy, it came around 97% accurate.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

5.1Deployment:

select Arduino as deployment ;

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Add the downloaded EI deployed file to the Arduino library

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Note: Add the ECG_Analyzer library from GitHub link to the Arduino library too

This helps to integrate my ECG decoding algorithm code to integrate with EI generated model.

5.2 Integrating Application code to the core generated EI code

I have merged the application code into EI deployed "nano_ble33_sense_accelerometer_continuous" and saved it as ECGAnalyzer.ino.

It is available in attached GitHub link. Flash the code with SIMULATION as 0 in ECG_Analyzer.c in device.

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Hardware:

Connect as per the fritzing diagram:

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Assembling :

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Final Product:

Here the final product and the accuracy level in test data!!!

Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense
Precision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Credits:

ECG : https://geekymedics.com/how-to-read-an-ecg/

Code

https://github.com/Manivannan-maker/ECGAnalyzer
unzip the ECGAnalyzer.zip Code to Flash: EI_Deployed_Library\examples\ECGAnalyzer\ECGAnalyzer.ino Library to Edit simulation : ECGAnalyzer_lib\ecg_analyzer.c Matlab script and signalbuilder model: script to run in matlab --> Matlab_Simulation\Excel2Arrayconverter.m Signal builder --> Matlab_Simulation\signalbuilder.slxhttps://github.com/Manivannan-maker/ECGAnalyzer

Schematics

Connect as per the diagramecgdefault_SjMMcgW8jY.fzzPrecision ECG Analyzer with Edge Impulse and Arduino Nano 33 BLE Sense

Manufacturing process

  1. The Art and Craft of Tiaras: History, Materials, and Modern Design
  2. Lead Crystal: History, Craftsmanship, and Future of a Timeless Glass
  3. Barbed Wire Explained: History, Construction, and Modern Uses
  4. Lead: Production, Applications, and Environmental Considerations
  5. Walabot‑Powered Toilet Hygiene Monitor
  6. Hands‑On Machine‑Learning with the reTerminal: Edge Impulse & ARM NN Demo Guide
  7. TinyML Language Detector with Edge Impulse on Arduino Nano 33 BLE Sense
  8. MOSMusic Project Kit: MOSFET, Arduino, and More
  9. Advanced Arduino-Based ECG Monitor for Real-Time Heart Rate Detection
  10. 6 Organization Types Driving the Edge Computing Revolution