This guide will help you create an IoT-Based Heart Monitoring System using ECG. The system will continuously monitor heart activity and send data to the cloud for real-time analysis and alerts.

Components Required

  • Microcontroller or development board (e.g., Arduino, ESP8266, or ESP32)
  • ECG sensor (e.g., AD8232 or similar)
  • Wi-Fi module (if not integrated into the microcontroller)
  • Connecting wires and breadboard
  • Power supply (e.g., 5V DC adapter or battery)
  • Cloud service account (e.g., ThingSpeak, Blynk, or AWS IoT)

Setting Up the Hardware

Follow these steps to set up the hardware:

  1. Connect the ECG Sensor

    Connect the ECG sensor to the microcontroller. The ECG sensor will have pins for power (VCC), ground (GND), and output (OUT). Connect these pins to the corresponding pins on the microcontroller.

  2. Power the System

    Ensure that the microcontroller and ECG sensor are properly powered using the appropriate power supply.

Programming the Microcontroller

Write the code to read ECG data from the sensor and send it to the cloud. Below is a basic outline of the code using Arduino:


                #include <WiFi.h>
                #include <PubSubClient.h>

                #define ECG_SENSOR_PIN A0
                #define WIFI_SSID "your_SSID"
                #define WIFI_PASSWORD "your_PASSWORD"
                #define MQTT_SERVER "broker_address"
                #define MQTT_TOPIC "heart/monitoring"

                WiFiClient espClient;
                PubSubClient client(espClient);

                void setup() {
                    Serial.begin(9600);
                    setup_wifi();
                    client.setServer(MQTT_SERVER, 1883);
                }

                void setup_wifi() {
                    delay(10);
                    Serial.print("Connecting to ");
                    Serial.print(WIFI_SSID);
                    
                    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
                    while (WiFi.status() != WL_CONNECTED) {
                        delay(500);
                        Serial.print(".");
                    }
                    Serial.println(" connected");
                }

                void reconnect() {
                    while (!client.connected()) {
                        Serial.print("Attempting MQTT connection...");
                        if (client.connect("HeartMonitorClient")) {
                            Serial.println("connected");
                            client.subscribe(MQTT_TOPIC);
                        } else {
                            Serial.print("failed, rc=");
                            Serial.print(client.state());
                            Serial.println(" try again in 5 seconds");
                            delay(5000);
                        }
                    }
                }

                void loop() {
                    if (!client.connected()) {
                        reconnect();
                    }
                    client.loop();
                    
                    int ecgValue = analogRead(ECG_SENSOR_PIN);
                    String payload = "ECG Value: " + String(ecgValue);
                    client.publish(MQTT_TOPIC, payload.c_str());

                    Serial.println(payload);
                    delay(1000); // Wait 1 second before the next reading
                }
            

This code connects to a Wi-Fi network, reads data from the ECG sensor, and publishes it to an MQTT broker for real-time monitoring.

Setting Up Cloud Integration

To visualize and analyze the ECG data, integrate the system with a cloud service. For instance, you can use ThingSpeak, Blynk, or AWS IoT. Configure your cloud service to receive and display the data published by the microcontroller.

Testing and Calibration

Test the system by monitoring ECG data under different conditions and verifying that the data is accurately sent to the cloud. Adjust the thresholds and parameters in the code as needed for better accuracy and reliability.

Conclusion

The IoT-Based Heart Monitoring System Using ECG offers a way to continuously monitor heart health and alert users to potential issues. By integrating ECG sensors with microcontrollers and cloud services, this system provides real-time insights into heart activity and helps in maintaining heart health.