Introduction

In this project, IoT Based ECG and Heart rate Monitoring, We will learn how to monitor Heart and ECG rates with AD8232 ECG Sensor and ESP8266(nodemcu). We will also learn to plot the ECG graph online on any IoT cloud platform . To accomplish this, we will connect the AD8232 ECG Sensor to the ESP8266. Then, by connecting ECG leads to the chest or hand, we will generate an ECG signal. We will send the ECG graph to the cloud using MQTT Broker using Ubidots parameters such as API Key or Token.

This image has an empty alt attribute; its file name is ezgif.com-optimize.gif

Heart disease has become a major concern in recent decades, and many people have died as a result of various health issues. As a result, heart disease should not be taken lightly. This disease can be avoided by analyzing or monitoring the ECG signal early on. As a result, we present this project IoT Based ECG and heart rate Monitoring with AD8232 ECG Sensor and Arduino.

In our previous tutorial, I explained how to interface AD8232 ECG Sensor with Arduino and monitor the ECG waveform on Serial Plotter. You can read my previous guide here:

ECG Monitoring system using AD8232 with Arduino

IoT Based ECG and Heart Monitoring with AD8232 ECG Sensor & ESP32

PCB Manufacturer

PCBWAY is quite professional in the field of PCB manufacturing; you can try their services at extremely low prices, Only 5 dollars for 10 PCBs , besides this the new members also get a 5 Dollars bonus.

You can now upload the Gerber File to the Website and place an order. The PCB quality is superb & high standard. That is why most of people trust PCBWAY for PCB & PCBA Services.


You Will get FREE prototype pcb from PCBWAY. So do not be late to register and place your first order from PCBWAY

If you want to order pcb from PCBWAY . CLICK IN THE LINK BELOW:

Some Sample PCB ‘ S From PCBWAY

Bill Of Materials

Edit

SNCOMPONENTS NAMEDESCRIPTIONQUANTITY
NodemcuESP8266-12E Board1https://amzn.to/3gZVsvh
2Connecting wiresjumper wiresomehttps://amzn.to/3fMoSw7
3BreadboardNormal1https://amzn.to/3FUQlXe
4AD8232 ECG Sensor-1https://amzn.to/3h1nAOk
5Data cable5V Micro USB Data Cable1https://amzn.to/3iJLa2U

AD8232 ECG Sensor

This sensor is a cost-effective board used to measure the electrical activity of the heart. This electrical activity can be charted as an ECG or Electrocardiogram and output as an analog reading. ECGs can be extremely noisy, the AD8232 Single Lead Heart Rate Monitor acts as an op-amp to help obtain a clear signal from the PR and QT Intervals easily.

The AD8232 is an integrated signal conditioning block for ECG and other biopotential measurement applications. It is designed to extract, amplify, and filter small biopotential signals in the presence of noisy conditions, such as those created by motion or remote electrode placement.

The AD8232 module breaks out nine connections from the IC that you can solder pins, wires, or other connectors to. SDN, LO+, LO-, OUTPUT, 3.3V, GND provide essential pins for operating this monitor with an Arduino or other development board. Also provided on this board are RA (Right Arm), LA (Left Arm), and RL (Right Leg) pins to attach and use your own custom sensors. Additionally, there is an LED indicator light that will pulsate to the rhythm of a heartbeat.

Circuit Diagram Of IoT Based ECG and heart Monitoring

Here is a circuit digram for Interfacing AD8232 ECG Sensor with NodeMCU ESP8266. There are 6 pins in AD8232 Breakout Board. SDN is not connected.

Circuit Diagram Of IoT Based ECG and heart Monitoring

Connect the OUTPUT to analog A0 of Nodemcu. Connect the LO+ & LO- to D5 & D6 of NodeMCU respectively. Supply the AD8232 kit with 3.3V VCC & Connect its GND to GND.

Manufacturing FIles

The PCB for IoT Based ECG Monitoring has been designed in EasyEDA online PCB making tool. Below is the front view and Back View of the PCB.

The gerber file for the PCB is given below.

IoT-Based-ECG-Monitoring-PCBDownload

ECG Leads/Electrode Placement

It is recommended to snap the sensor pads on the leads before application to the body. The closer to the heart the pads are, the better the measurement. The cables are color coded to help identify proper placement.

Red: RA (Right Arm)
Yellow: LA (Left Arm)
Green: RL (Right Leg)

I have connected by ESP32 with AD8232 to my hand or simply you can place in your chest as shown in the figure below.

Source Code

Here is a Source Code for viualizing the ECG graph waveform on Serial Plotter. Simply copy the code and upload it to the NodeMCU ESP8266 Board.

void setup() 
{
// initialize the serial communication:
Serial.begin(9600);
pinMode(14, INPUT); // Setup for leads off detection LO +
pinMode(12, INPUT); // Setup for leads off detection LO -
  
}
  
void loop() {
  
if((digitalRead(10) == 1)||(digitalRead(11) == 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(1);
}

Result & Observations

Once the code is upload, open the Serial Monitor and Set the Buad Rate to 9600. The ECG waveform can be seen below as a visualiations effect on Serial Monitor.

IoT based ECG Monitoring with AD8232 ECG Sensor & ESP8266

Using the above code you can visualize the ECG waveform on Serial Plotter Screen. But now we want to visualize the ECG waveform remotely from any part of the world. So for that, I won’t need to send the signal generated to any IoT platform. For that I used Ubidots. Using Ubidots you can send data to the cloud from any Internet-enabled device.

The complete guide for setting up Ubidots is explained in the video below. Follow the video guide below to setup Ubidots with ESP8266 & ECG Sensor Code.

Source Code/Program

The source code for IoT Based ECG Monitoring with AD8232 ECG Sensor & ESP8266 is given below. Copy this code and change the following Parameters.

1. WIFI SSID: Your WiFi SSID
2. PASSWORD: Your WiFi password
3. TOKEN: Your Ubidots TOKEN (Check the video below to find about it)
4. MQTT_CLIENT_NAME: Your own 8-12 alphanumeric character ASCII string.

You need one library called Pubsubclient library. So go to the library manager and install the library as shown in the image below.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
  
#define WIFISSID "Alexahome"                                // Put your WifiSSID here
#define PASSWORD "12345678"                                 // Put your wifi password here
#define TOKEN "BBFF-YKxITsj1YPeTMxw7mq8lvYFBpXnCxD"         // Put your Ubidots' TOKEN
#define MQTT_CLIENT_NAME "myecgsensor"                       // MQTT client Name, please enter your own 8-12 alphanumeric character ASCII string; 
                                                            //it should be a random and unique ascii string and different from all other devices
  
/****************************************
 * Define Constants
 ****************************************/
#define VARIABLE_LABEL "myecg" // Assing the variable label
#define DEVICE_LABEL "esp8266" // Assig the device label
  
#define SENSOR A0 // Set the A0 as SENSOR
  
char mqttBroker[]  = "industrial.api.ubidots.com";
char payload[100];
char topic[150];
// Space to store values to send
char str_sensor[10];
  
/****************************************
 * Auxiliar Functions
 ****************************************/
WiFiClient ubidots;
PubSubClient client(ubidots);
  
void callback(char* topic, byte* payload, unsigned int length) {
  char p[length + 1];
  memcpy(p, payload, length);
  p[length] = NULL;
  Serial.write(payload, length);
  Serial.println(topic);
}
  
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
     
    // Attemp to connect
    if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
      Serial.println("Connected");
    } else {
      Serial.print("Failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 2 seconds");
      // Wait 2 seconds before retrying
      delay(2000);
    }
  }
}
  
/****************************************
 * Main Functions
 ****************************************/
void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFISSID, PASSWORD);
  // Assign the pin as INPUT 
  pinMode(SENSOR, INPUT);
  
  Serial.println();
  Serial.print("Waiting for WiFi...");
   
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
   
  Serial.println("");
  Serial.println("WiFi Connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  client.setServer(mqttBroker, 1883);
  client.setCallback(callback);  
}
  
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  
  sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL); // Adds the variable label
   
  float myecg = analogRead(SENSOR); 
   
  /* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
  dtostrf(myecg, 4, 2, str_sensor);
   
  sprintf(payload, "%s {\"value\": %s}}", payload, str_sensor); // Adds the value
  Serial.println("Publishing data to Ubidots Cloud");
  client.publish(topic, payload);
  client.loop();
  delay(10);
}

Results & Observations

Once the code is uploaded, you can open your serial Monitor. The Serial Monitor will display the following lines succesfully if the module is connected to Wifi and if Ubidots token is valid.

Now you can visit the Ubidots dashboard and you can observe the waveform being published on Ubidots. The waveform may not be exactly similar to above due to a little amount of delay. But it’s enough for a simple demonstration. In the near future, a better version of the device can be made.

Full Project Website LINK:

https://electronicsworkshops.com/2022/12/06/iot-based-ecg-and-heart-rate-monitoring-with-ad8232-ecg-sensor-esp8266/