IoT based health monitoring system | Arduino Project

In this tutorial we are going to design and construct an IoT based patient health monitoring system using Arduino and generic ESP8266. The proposed project can collect and send patient’s health data to an IoT cloud server such as Thingspeak where real time health status of the patient can be recorded and monitored in a remote location where a healthcare professional is present.

We will see:

  • What is IoT based health monitoring system?
  • Block diagram of IoT health monitoring system.
  • How to setup Thingspeak account?
  • Circuit diagram of IoT health monitoring system.
  • How to upload code to ESP8266 correctly?
  • Program code for ESP8266 and Arduino.
  • Prototype, result and troubleshooting.

What is IoT patient based health monitoring system?

IoT based patient health monitoring system is a generic term given to any medical equipment that has internet capability and can measure one or more health data of a patient who is connected to the device such as heartbeat, body temperature, blood pressure, ECG, steps etc. The equipment can record, transmit and alert if there is any abrupt change in the patient’s health.

By this definition, it includes devices such as smart-watches, fitness trackers, smart-phones to expensive hospital equipment which can connect to internet.

IoT based health monitoring system is used where the patient and heath expert(s) are at different locations. For example, a patient can stay at home and continue his/her routine life and a doctor can monitor patient’s heath. Based on the received data the heath expert can prescribe a best treatment or take an immediate action in case of an emergency.

Block diagram:

Block diagram - IoT based patient health monitoring system
Block diagram – IoT based patient health monitoring system

The proposed design of IoT based heath monitoring system is built around Arduino microcontroller which is the brain of the project.

Arduino collects real time health data from pulse sensor which measures heartbeat in minutes or BPM (beats per minute). A digital temperature sensor connected to Arduino measures body temperature of the patient.

A buzzer produces auditory beeps when the patient’s heartbeat occurs / detected. This gives a brief insight to a healthcare professional how a patient’s heart is performing in a particular health condition. Abnormal heartbeats can be detected by just listening to the beeps.

A generic ESP8266 IoT module is connect to Arduino via UART, it is responsible for connecting the machine to internet and also for sending health data to a IoT server (Thingspeak) for storing and monitoring.

This circuit is not only capable of sending patient’s health data to a server but also can show real time data on a 16×2 LCD display. This is useful for a healthcare professional who is actively monitoring a patient on site.

How to set-up Thingspeak account to receive patient data:

You need to setup your Thingspeak account correctly to receive patient’s health data.

  • You need to create a Thingspeak account by clicking this link.
  • Enter the credentials it asks while signing up.
  • Click “new channel” and edit the following in the channel settings tab.
Channel Setting
  • Don’t forget to click save.
  • Now click “API keys” tab on Thingspeak dashboard to see your “write” API key. The API key is an access code using which you can write data to your Thingspeak channel.
API Key
API Key
  • Take note of your “write API key” and channel ID which need to be inserted in the given program code.
  • By clicking “Private view” tab you can see a couple of empty channels which are ready to receive data.

Circuit diagram of IoT based patient health monitoring system:

IoT based Patient Monitoring System Circuit
IoT based Patient Monitoring System Circuit

Circuit description:

Arduino board:

As mentioned earlier Arduino is the brain of the project and you may use any Arduino board that is available with you and you can connect to the same pin numbers that are shown in the schematic.

The arduino is responsible for collecting, displaying and sending the data to ESP8266. The whole circuit can be powered using USB or via “Vin” pin (9V-12V).

Generic ESP8266:

Generic ESP8266
Generic ESP8266

The generic ESP8266 module is a miniature microcontroller board which has RAM, ROM, clock, communication protocols and I/O pins just like any other microcontrollers and it need to be programmed to make it functional.

It does not come with a USB port so we need to use its UART pins to upload a code which will be discussed in the later part of this post.

It operates at 3.3V (5V can kill) and it supports UART / serial communication protocol and it has (only) two GPIO pins which we are not going to use.

Arduino collects and sends patient data to this ESP8266 over its UART pins. ESP8266 will connect to its designated server which is programmed to it and passes the data.

Pulse sensor:

The pulse sensor / heartbeat sensor is an inexpensive analog sensor which can measure reasonably accurate pulse rate of human heart with the help of a microcontroller.

A microcontroller like Arduino can be programmed to calculate its analog output to BPM or beats per minute.

It can operate from 3.3V to 5V, but here we are connecting it to 5V supply. It has just 3 pins: Vcc, GND and signal (Analog out).

Pulse Sensor- Heartbeat
Pulse Sensor- Heartbeat

On the front we have a green LED with a specific wavelength and a light sensor; you can place your finger (except thumb) on front part of the sensor with a slight pressure.

The process behind detecting heartbeat is when our heart pumps there will be blood flow in our nerves, this flow changes the light intensity reflected to the light sensor.

Pulse Sensor- Heartbeat
Pulse Sensor- Heartbeat

On the back of the pulse sensor there is an amplifier and noise reduction circuit which outputs analog signal proportional to the changes in the reflected light.

The pulse senor must be used with dry fingers and the back of the sensor must not be touched and since ambient light can interfere with the sensor while measuring, it is recommended to wrap your finger and the sensor with an insulation tape as shown below for the best result:

Temperature sensor:

We are using a general purpose water proof digital temperature sensor DS18B20 to measure body heat which comes in a sealed metallic enclosure.

Measuring body temperature can reveal a lot about patient’s health and a healthcare professional can identify abnormalities in a patient’s health.

Pin diagram:

DS18B20 Water proof temperature sensor
DS18B20 Water proof temperature sensor

Inside the metallic enclosure there is a transistor like sensor which measures the temperature (that’s why a transistor like component is shown in the circuit diagram).

It has 3 wires: Vcc, GND and data, the data wire must be connected to a pull-up 4.7K resistor.

We are using a water proof sensor because the sensor will be placed on human body for prolong amount of time and dust, sweat and other body fluids can accumulate on the sensor which could lead to inaccurate temperature measurement.

A water proof sensor can be easily cleaned and dust and body fluids won’t able to clog the sensor much.  

Where the temperature sensor can be place on human body?

There are several places on human body where temperature sensor can be placed like: oral, ear, underarm, rectal etc. Our best recommendation is underarm for the sensor placement after considering accuracy and convenience. You need to place it for at-least 2 minutes so that temperature gets settled properly. 

LCD display:

A LCD display is provided to see the real time data from the sensors; it is set to show the body temperature and heartbeat in BPM. A 10K potentiometer is provided using which you can adjust LCD contrast for optimum legibility.

Buzzer:

We are utilizing a 5V DC buzzer to detect heartbeat rhythm, just like in hospital rooms (or in TV serials/movies) where beep sound emerges when a patient is monitored using ECG / EKG etc.

The DC buzzer has polarity, the –Ve of buzzer is connected to GND and +Ve is connected to pin 8 of Arduino.

Audio beeps can give a healthcare professional an insight about pulse of a patient, for example, slow pulse indicates that the patient is sleeping or in coma or fast beep indicates that the patient is in some health trouble and need immediate medical attention.

Please note that not all medical devices in hospitals beep is indication of heartbeat, it can be of several types of alerts ranging from not so important alerts to extreme emergency alerts.

How to upload code to generic ESP8266?

As mentioned earlier the ESP8266 does not come with a USB like Arduino or NodeMCU which allows us to upload code directly, instead we have to wire up the ESP8266 according to the given circuit.

Before we do that we need to install core ESP8266 files to Arduino IDE software only then we will able to compile and upload code.

  • Now paste the URL to the box and click OK.
  • Now go to Tools > Board > Boards Manager. A window will open as shown below.

Type “ESP8266” and you will see a drop-down option and install button. Select the latest version and click install. Installation could take about 5 minutes.

ESP8266 code upload circuit diagram:

Uploading code to ESP8266
Uploading code to ESP8266
Uploading code to ESP8266
Uploading code to ESP8266
  • Wire-up circuit as shown above, the microcontroller IC must be removed from Arduino’s socket.
  • Now copy and paste the code to your IDE and insert your Wi-Fi credentials, channel ID and write API key.
  • Select Tools > Board > Generic ESP8266 Module.
  • Now press and hold the flash button and press reset button momentarily once and release the flash button. This will make the ESP8266 ready for uploading a new code.
  • Select baud rate as 115200, select the correct COM port and click upload.
  • Once the code is uploaded you will see “Done uploading” as shown below:
  • Once you successfully uploaded the code to ESP8266, you need to re-wire the circuit as per the main circuit diagram and don’t forget to insert the microcontroller IC back to the socket.

Download Thingspeak.h library: Click here

Program code for generic ESP8266:

// ----------(c) Electronics-project-hub-------- //
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>

//------- WI-FI details ----------//
char ssid[] = "XXXXXXXXXXX"; // SSID here
char pass[] = "YYYYYYYYYYY"; // Passowrd here
//--------------------------------//

//----------- Channel details ----------------//
unsigned long Channel_ID = 123456; // Channel ID
const char * myWriteAPIKey = "ABCDEFG1234"; //Your write API key
//-------------------------------------------//

const int Field_Number_1 = 1;
const int Field_Number_2 = 2;
String value = "";
int value_1 = 0, value_2 = 0;
int x, y;
WiFiClient  client;

void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
  internet();
}

void loop()
{
  internet();
  if (Serial.available() > 0)
  {
    delay(100);
    while (Serial.available() > 0)
    {
      value = Serial.readString();
      if (value[0] == '*')
      {
        if (value[5] == '#')
        {
          value_1 = ((value[1] - 0x30) * 10 + (value[2] - 0x30));
          value_2 = ((value[3] - 0x30) * 10 + (value[4] - 0x30));
        }
        else if (value[6] == '#')
        {
          value_1 = ((value[1] - 0x30) * 100 + (value[2] - 0x30) * 10 + (value[3] - 0x30));
          value_2 = ((value[4] - 0x30) * 10 + (value[5] - 0x30));
        }
      }
    }
  }
  upload();
}

void internet()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      delay(5000);
    }
  }
}

void upload()
{
  ThingSpeak.writeField(Channel_ID, Field_Number_1, value_1, myWriteAPIKey);
  delay(15000);
  ThingSpeak.writeField(Channel_ID, Field_Number_2, value_2, myWriteAPIKey);
  delay(15000);
  value = "";
}
// ----------(c) Electronics-project-hub-------- //

You need to insert your Wi-Fi credentials here:

//------- WI-FI details ----------//
char ssid[] = "XXXXXXXXXX"; //SSID here
char pass[] = "YYYYYYYYYY"; // Passowrd here
//--------------------------------//

You need to insert channel details here:

//----------- Channel details ----------------//
unsigned long Channel_ID = 123456; // Channel ID
const char * myWriteAPIKey = "ABCDEF1234"; //Your write API key
//-------------------------------------------//

Program code for Arduino:

You need to upload the below given code to Arduino once you successfully uploaded the ESP8266 code. So, yes this project uses two set of program codes to function. Don’t forget to select the correct Arduino board and COM port to upload the code. 

Download “Onewire.h” library: Click here

Download “DallasTemperature.h” library: Click here

Download “PulseSensorPlayground.h” library: Click here

// ----------(c) Electronics-project-hub -------------//
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>
SoftwareSerial esp(10, 11);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#define ONE_WIRE_BUS 9
#define TEMPERATURE_PRECISION 12
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempDeviceAddress;
int numberOfDevices, temp, buzzer = 8;
const int PulseWire = A0;
int myBPM, Threshold = 550;
PulseSensorPlayground pulseSensor;
unsigned long previousMillis = 0;
const long interval = 5000;
void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);
  esp.begin(115200);
  sensors.begin();
  numberOfDevices = sensors.getDeviceCount();
  pulseSensor.analogInput(PulseWire);
  pulseSensor.setThreshold(Threshold);
  pulseSensor.begin();
  pinMode(buzzer, OUTPUT);
  digitalWrite(buzzer, HIGH);
  lcd.setCursor(0, 0);
  lcd.print("  IoT Patient");
  lcd.setCursor(0, 1);
  lcd.print(" Monitor System");
  delay(1500);
  digitalWrite(buzzer, LOW);
  lcd.clear();
}

void loop()
{
  myBPM = pulseSensor.getBeatsPerMinute();
  if (pulseSensor.sawStartOfBeat())
  {
    beep();
    lcd.setCursor(0, 1);
    lcd.print("HEART:");
    lcd.print(myBPM);
    lcd.setCursor(9, 1);
    lcd.print(" BPM");
    delay(20);
  }
  sensors.requestTemperatures();
  for (int i = 0; i < numberOfDevices; i++)
  {
    if (sensors.getAddress(tempDeviceAddress, i))
    {
      temp = printTemperature(tempDeviceAddress);
      lcd.setCursor(0, 0);
      lcd.print("BODY:");
      lcd.print(temp);
      lcd.print(" *C");
    }
  }
  upload();
}

int printTemperature(DeviceAddress deviceAddress)
{
  int tempC = sensors.getTempC(deviceAddress);
  return tempC;
}

void beep()
{
  digitalWrite(buzzer, HIGH);
  delay(150);
  digitalWrite(buzzer, LOW);
}

void upload()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    esp.print('*');
    esp.print(myBPM);
    esp.print(temp);
    esp.println('#');
  }
}
// ----------(c) Electronics-project-hub -------------//

Prototype:

IoT based Patient Health Monitoring System
IoT based Patient Health Monitoring System
IoT based patient health monitoring system
IoT based patient health monitoring system

Result: Private view on Thingspeak

Troubleshooting:

It could take more than 30 seconds to update a field and each field will be updated one after the other.

If your channels are not getting updated with any values after few minutes, you can check your Wi-Fi router’s connected devices (for ESP8266) by logging in to its admin control.

In the above list of connected devices (to Wi-Fi router) we can see that ESP8266 has been connected successfully. It will be like “ESP” followed by some numbers.

If it does not appear here, please check the wiring and Wi-Fi credentials on the code.     

If you have any questions regarding this project feel free to ask us in the comment, you will get a guaranteed reply from us.

Avatar photo

My nick name is blogthor, I am a professional electronics engineer specialized in Embedded System. I am a experienced programmer and electronics hardware developer. I am the founder of this website, I am also a hobbyist, DIYer and a constant learner. I love to solve your technical queries via comment section.