DIY ECG/EKG Electrocardiogram

DIY ECG/EKG Electrocardiogram

a year ago, March 7, 2023
Reading time: 5 mins


When it comes to healthcare, timely access to medical services and diagnostic tests can be critical. Unfortunately, in some cases, getting an appointment with a specialist like a cardiologist can take weeks or even months. This was the case for me when I needed an ECG (electrocardiogram) to assess my heart health.

What is an ECG/EKG?

ECG (that stands for electrocardiogram) is a medical test that records the electrical activity of your heart. They check the electric waves, show them on a screen, or plot it on paper. It can tell you some important things about your heart, like how it’s beating and if it’s healthy or not. It can help doctors determine heart problems, like arrhythmias or heart attacks.

I have a hobby of working with electronics

Frustrated with the wait time, I started exploring other options. That’s when I came across the AD8232 ECG module, which can be used with a microcontroller to measure and display ECG data. I decided to order some modules and an ESP32-S3 dev module with an LCD screen (LilyGo T-Display-S3) to start experimenting.

AD8232 ECG module and ESP32-S3 dev module with an LCD screen (LilyGo T-Display-S3)
AD8232 ECG module and ESP32-S3 dev module with an LCD screen (LilyGo T-Display-S3)

After some tinkering, I was able to build a working ECG device that read the ECG signal from the AD8232 module and displayed the graph on the LCD screen of the ESP32-S3 dev module. But I didn’t want to stop there.

ECG signal visualized on an LCD on ESP32-S3

Utilizing the WiFi function of ESP32 for real-time monitoring on a web browser

I wanted to take advantage of the ESP32-S3’s Wi-Fi functionality to create a WebSocket server that could communicate with a web browser on a laptop in real time, sending ECG data directly to the browser.

With this new setup, I was able to view the ECG graph on a much larger screen, which made it easier to detect any abnormalities or irregularities in the heart’s electrical activity. Plus, since the data was being sent directly to a web browser, I could use JavaScript to implement more complex features that would have been difficult to achieve on an Arduino.

ECG on the browser using WebSocket

I added the ability to detect the frequency of the ECG data in Hz and calculate the heart rate via the RR interval. These are fundamental to assessing heart health and identifying any potential issues. I will be adding more features in the future.

GitHub repo for the client code

If you want to try the client app yourself or want to make your hand dirty and modify the code as you need, the client app source code can be found in the GitHub repo here dejavu1987/ecg-client (


Overall, I would call my DIY ECG device project a success, it can monitor my heart and get vital data like heart rate measurements, I still needed to wait months for an appointment because when it comes to health we should only rely on experts. Plus, it gave me a deeper understanding of how ECGs work and the technology behind them. While I wouldn’t necessarily recommend building your own medical devices without proper training or supervision, in my case, it was a fun project that had some practical applications.

Live video explanation and DEMO

I did a live video on YouTube demonstrating the setup. Although there was not much live participation I welcome comments and questions as well as feedback.

YouTube Live Demo of ECG

Video timestamps

  • Introduction (00:00): The video starts with an introduction to the DIY ECG monitoring project.
  • Socket Event Handling (02:35): Discussion on handling socket events using the async web server library for ESP32.
  • Setup Configuration (03:32): Explanation of the setup process, including serial communication and Wi-Fi connection.
  • Data Buffering (04:08): Insights into the loop function, where data is buffered and sent to clients every 50 milliseconds.
  • Frontend Simulation (09:35): A fake server is used to simulate ECG data to test the frontend visualization.
  • d3.js Visualization (10:42): Utilization of the d3.js library to visualize ECG data on the front end.
  • Real ECG Signal (20:11): Transition from simulated data to real ECG signals captured by the ESP32.
  • Hardware Demonstration (20:40): A hands-on demonstration of the hardware setup and electrode application.
  • Signal Analysis (22:06): Application of electrodes and analysis of signal wandering and noise.
  • ECG Signal Improvement (28:00): Adjustments made to obtain clearer ECG signals, including addressing a missing P wave.
  • Battery Power (30:00): Switching to battery power to achieve a cleaner signal and the return of P waves.
  • Physical Activity (31:40): Observation of signal changes with physical activity, like squats, to improve P wave detection.
Smart temperature control for a cheap Soldering iron
Build SuDoKu game using ReactJs
© 2024 Anil Maharjan