ECG With Display and SD-Card


As I want / need to write an ECG every now an then I was looking for a self-made solution to write my ECG at home. Finally I found a project from Peter Balch (https://www.instructables.com/ECG-Display-With-Arduino/) which I adapted to my needs, especially for recording the data to an SD-card.
Supplies
- Arduino Nano
- AD8232 EKG Sensor Module
- 1.3 inch LCD-Module 240x240 ST7789
- microSD-Card Module
- Step-up Regulator 5V (or adjustable)
- LiPo Battery (from Smartphone)
- two Switches
- Perfboard
Overview



I'm not good at programming so I was very glad to find the great project of Peter Balch linked above that I could use as a basis for my ECG.
What I wanted is to see the ECG-trace on the display and to store it on an SD-card like a holter-ecg. Unfortunately with an Arduino nano it is not possible to do both at a time, so you have to switch between the two functions. Which is not too bad because otherwise it would be very energy-consuming writing the ECG for many hours to the SD-card with the display always on.
Moving the arms may produce a lot of artifacts, so be sure to find a good place for the electrodes. I also fixed a plug with a line to the AD8232 as the forces on the built-in plug seemed quite high when wearing the device with a belt around the waist.
Unfortunately I don't have a circuit diagram, but connecting the devices should not be too difficult reading the code. I have one switch for on/off and one for display/SD-card. As only switching from SD-card to display will close the file on the SD-card correctly it is advised to do that before switching the ECG off. And for the same reason take care the battery doesn't run out.
Most part of the code for creating the ECG plot is from Peter Balch, only that my another display-controller is different from his. I adapted the size of the curve, removed, among other, the 50Hz filters, the Poincaré plot and the fast ADC (I saw no improvement from the latter). Everything concerning the SD-card is from me and might be improvable. It is still work in (slow) progress, please don't mind the mix of English and German. Switching to SD-card will always produce a new file (with a counter).
Because of the speed of the Arduino a new value is written to file every 5 ms approximately. But this depends highly on the speed of the SD-card! I tried to improve it, without success. But the quality of the resulting figure is good enough. In the file there are two rows, one with the millis and the other with the value for the amplitude from the AD8232. Depending on the further processing a comma or space is needed as separator.
The next problem I encountered was how to plot the ECG from the file. My first idea was to use gnuplot, but then I had a plot with all data compressed to the size of the gnuplot window, a bit awkward. The data can be imported to audacity as raw data which works quite good (in audacity normalize it after importing), and also importing it to edfbrowser works. But in both cases you have one very long ECG-line. And both can't process the column with the time values (it has to be deleted first), you have to tell it the frequency, that is not always constant because the distance of the values changes and is sometimes even up to a two-digit value, which causes an inaccuracy.
I wanted to have the plot in lines from up to down and as I found no ready solution I programmed some Python scripts (partly with the help of chatGPT) using matplotlib, pandas and numpy. First the millis must be set to zero (set-millis.py, also adding a comma as separator), then the file can be plotted (plot-file.py). But if the resulting picture is too large you get an error, so the file has to be split first (split-file.py). In every case the name of the file to be processed has to be given as argument to the script. The result is a very "long" svg-graphic.
So now here are the files: