Heart Rate Monitoring System using Heart Rate Pulse Sensor with I2C 1602 LCD

Overview

When it comes to our heart rate, of course,we don’t want it to beat too fast, and to beat too slow. But, most of the time, we don’t really think about the rhythm and pace of our heart. And if there is something unusual happening in our heart, we are really completely unaware of it.

In this tutorial we made a simple Heart Rate Monitoring System that monitors the rate of your heart and also the Beats Per Minute.

Sample Video:

Hardware Components


  • I2C Liquid Crystal Display 16x2
    
    
  • Pulse Sensor
  • 
    
  • Breadboard
  • 
    
  • Jumper Wires (Male to Male)
  • Jumper Wires (Male to Female)
  • 
    

    You can buy it all here at CreateLabz

    Software Components

    Application Discussion

    How Pulse Sensor works

    The working of the Pulse/Heart beat sensor is very simple. The sensor has two sides, on one side the LED is placed along with an ambient light sensor and on the other side we have some circuitry. This circuitry is responsible for the amplification and noise cancellation work. The LED on the front side of the sensor is placed over a vein in our human body. This can either be your Finger tip or you ear tips, but it should be placed directly on top of a vein.

    Now the LED emits light which will fall on the vein directly. The veins will have blood flow inside them only when the heart is pumping, so if we monitor the flow of blood we can monitor the heart beats as well.  If the flow of blood is detected then the ambient light sensor will pick up more light since they will be reflect ted by the blood, this minor change in received light is analysed over time to determine our heart beats.

    Note:

    Since all the electronics on the sensor are directly exposed it is also recommended to cover the sensor with hot glue, vinyl tape or other non conductive materials. Also it is not recommended to handle these sensors with wet hands.

    The sensor is covered with an electrical tape.

    For more info about this sensor, visit our store.

    What is an I2C 1602 LCD

    The I2C 1602 LCD module is a 2 line by 16 character display interfaced to an I2C daughter board. The I2C interface only requires 2 data connections, +5 VDC and GND to operate.

    I2C (Inter-Integrated Circuit)

    In I2C you can connect multiple slaves to a single master and you can have multiple masters controlling single, or multiple slaves. This is really useful when you want to have more than one microcontroller logging data to a single memory card or displaying text to a single LCD.

    I2C is a serial communication protocol, so data is transferred bit by bit along a single wire (the SDA line). Like SPI, I2C is synchronous, so the output of bits is synchronized to the sampling of bits by a clock signal shared between the master and the slave. The clock signal is always controlled by the master.

    For more info about this I2C 1602 LCD, Visit our store.

    Hardware Setup

    Note:

    The LCM1602 I2C is located at the back of the LCD or it is already connected at the back of the LCD.

    Code

    Library included

    Arduino code

    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>
    #include <millisDelay.h>
    
    millisDelay timerDelay;
    int pulsesensor = 0;
    int counter = 0;
    int bpm = 0;
    int test = 0;
    int rate = 0;
    int r1,r2,r3,r4,r5 = 0;
    LiquidCrystal_I2C lcd(0x27, 16, 2);
    
    void setup() {
    
      Serial.begin(9600);
      lcd.init();
      lcd.setCursor(0, 0);
      lcd.print("Heart Beat");
      lcd.setCursor(2, 1);
      lcd.print("Sensor");
      lcd.backlight();
    }
    
    void loop() {
    
      pulsesensor = analogRead(A0);
      delay(20);
      Serial.println(pulsesensor);
      
      if (pulsesensor < 70)
      {
        r1 = 1;
        r2 = 1;
        r3 = 1;
        r4 = 1;
        r5 = 1;
        lcd.setCursor(0, 0);
        lcd.print("Heart Rate:");
        lcd.setCursor(2, 1);
        lcd.print("Please Wait");
        delay(6000);
        test = 1;
        timerDelay.start(15000);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Heart Rate:");
        lcd.setCursor(2, 1);
        lcd.print("Scanning");
      }  
    
      if (test == 1) {
        if (pulsesensor == 540 && r1 == 1)
        {
          r2 = 0;
          r3 = 0;
          r4 = 0;
          r5 = 0;
          counter++;
          delay(500);
          Serial.print("counter:");
          Serial.println(counter);
          
        }
        else if (pulsesensor == 530 && r2 == 1)
        {
          counter++;
          delay(500);
          Serial.print("counter:");
          Serial.println(counter);
          r1 = 0;
          r3 = 0;
          r4 = 0;
          r5 = 0;
        }
        else if (pulsesensor == 520 && r3 == 1)
        {
          counter++;
          delay(500);
          Serial.print("counter:");
          Serial.println(counter);
          r2 = 0;
          r1 = 0;
          r4 = 0;
          r5 = 0;
        }
        else if (pulsesensor == 512 && r4 == 1)
        {
          counter++;
          delay(500);
          Serial.print("counter:");
          Serial.println(counter);
          r2 = 0;
          r3 = 0;
          r1 = 0;
          r5 = 0;
        }
        else if (pulsesensor == 500 && r5 == 1)
        {
          counter++;
          delay(500);
          Serial.print("counter:");
          Serial.println(counter);
          r2 = 0;
          r3 = 0;
          r4 = 0;
          r1 = 0;
        }
      
      if (timerDelay.isFinished())
      {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Heart Rate:");
        lcd.setCursor(2, 1);
        lcd.print("Remove");
        bpm = counter * 4;
        rate = 1;
        test = 0;
        counter = 0;
        Serial.println("bpm:");
        Serial.println(bpm);
        delay(5000);
        lcd.clear();
    
      }
      }
      if (rate == 1)
      {
        if (bpm > 60 && bpm < 100 || bpm == 60 || bpm == 100)
        {
          normal();
          rate = 0;
          delay(5000);
          bpm = 0;
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Heart Rate:");
        }
        else if (bpm < 60)
        {
          slow();
          rate = 0;
          delay(5000);
          bpm = 0;
          lcd.setCursor(0, 0);
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Heart Rate:");
        }
        else if (bpm > 100)
        {
          fast();
          rate = 0;
          delay(5000);
          bpm = 0;
          lcd.setCursor(0, 0);
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Heart Rate:");
        }
      }
    }
    void slow()
    {
      lcd.setCursor(0, 0);
      lcd.print("Heart Rate:");
      lcd.setCursor(2, 1);
      lcd.print("Slow");
      lcd.setCursor(9, 1);
      lcd.print("BPM:");
      lcd.setCursor(13, 1);
      lcd.print(bpm);
    }
    void normal()
    {
      lcd.setCursor(0, 0);
      lcd.print("Heart Rate:");
      lcd.setCursor(2, 1);
      lcd.print("normal");
      lcd.setCursor(9, 1);
      lcd.print("BPM:");
      lcd.setCursor(13, 1);
      lcd.print(bpm);
    }
    void fast()
    {
      lcd.setCursor(0, 0);
      lcd.print("Heart Rate:");
      lcd.setCursor(2, 1);
      lcd.print("Fast");
      lcd.setCursor(9, 1);
      lcd.print("BPM:");
      lcd.setCursor(13, 1);
      lcd.print(bpm);
    }

    Code Breakdown

    void setup() {
    
      Serial.begin(9600);
      lcd.init();                      // initialize the lcd
      lcd.init();
      lcd.setCursor(0, 0);
      lcd.print("Heart Beat");
      lcd.setCursor(2, 1);
      lcd.print("Sensor");
      lcd.backlight();
    }

    In this code we begin the serial communication and set the baud rate to 9600 bps. And initialize the LCD and displays the “Heart Beat” text in the first row of the LCD and the “Sensor” text in the second row of the LCD.

    pulsesensor = analogRead(A0);

    This code will read the value from the analog pin 0 where the pulse sensor is connected. And it will map input voltages between 0 and 5 volts into integer values between 0 and 1023.

    if (pulsesensor < 70)
      {
        r1 = 1;
        r2 = 1;
        r3 = 1;
        r4 = 1;
        r5 = 1;
        lcd.setCursor(0, 0);
        lcd.print("Heart Rate:");
        lcd.setCursor(2, 1);
        lcd.print("Please Wait");
        delay(6000);
        test = 1;
        timerDelay.start(15000);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Heart Rate:");
        lcd.setCursor(2, 1);
        lcd.print("Scanning");
      }

    This the code that tells the system to start scanning when the value suddenly drops less than 70 which means that a fingertip is placed on the sensor. And set the test variable to 1, this variable acts like a switch for the other condition to start running, also variables r1, r2, r3, r4 and r5 acts like a switch for the next conditions. And displays the corresponding text on the LCD. But it has a delay of 6 seconds before it starts to scan.

    After the delay, a timer is set for 15 seconds to read or scan the pulse. And change the text “please wait” on the LCD to “scanning”.

    if (test == 1) {
        if (pulsesensor == 540 && r1 == 1)
        {
          r2 = 0;
          r3 = 0;
          r4 = 0;
          r5 = 0;
          counter++;
          delay(500);
          Serial.print("counter:");
          Serial.println(counter);
          
        }
        else if (pulsesensor == 530 && r2 == 1)
        {
          counter++;
          delay(500);
          Serial.print("counter:");
          Serial.println(counter);
          r1 = 0;
          r3 = 0;
          r4 = 0;
          r5 = 0;
        }
        else if (pulsesensor == 520 && r3 == 1)
        {
          counter++;
          delay(500);
          Serial.print("counter:");
          Serial.println(counter);
          r2 = 0;
          r1 = 0;
          r4 = 0;
          r5 = 0;
        }
        else if (pulsesensor == 512 && r4 == 1)
        {
          counter++;
          delay(500);
          Serial.print("counter:");
          Serial.println(counter);
          r2 = 0;
          r3 = 0;
          r1 = 0;
          r5 = 0;
        }
        else if (pulsesensor == 500 && r5 == 1)
        {
          counter++;
          delay(500);
          Serial.print("counter:");
          Serial.println(counter);
          r2 = 0;
          r3 = 0;
          r4 = 0;
          r1 = 0;
        }

    This code will now run after the test variable is equal to 1.
    The range of values when a pulse is detected are between 540, 530, 520, 512, and 500. If the pulsesensor is equal to 540 and r1 equals to 1, since the r1 is already set to 1 from the previous condition. The counter variable will now increase every time the value reaches 540 which means the counter variable counts how many pulse it detected. And also sets the variables r2, r3 ,r4 and r5 to 0, to stop the other conditions from running since the 540 is the highest value we got from the sensor every time it detects a pulse. And it’s the same process for the other conditions if it equals to the highest value that we got.

    if (timerDelay.isFinished())
      {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Heart Rate:");
        lcd.setCursor(2, 1);
        lcd.print("Remove");
        bpm = counter * 4;
        rate = 1;
        test = 0;
        counter = 0;
        Serial.println("bpm:");
        Serial.println(bpm);
        delay(5000);
        lcd.clear();
    
      }

    This code runs after the timer runs out, the timer is set for 15 seconds. The text “scanning” on the LCD will now be change to “remove”, which means to remove your finger on the sensor. And the counter will be multiplied to 4, means the number of pulse that we got will be multiplied to 4 to get the beats per minute which will be stored in the bpm variable. Sets the rate variable to 1 to run the next condition it also acts like a switch. and the test variable equal to 1 to stop the previous condition from running. And also resets the counter to 0. And a delay of 5 seconds before moving to the next condition.

    if (rate == 1)
      {
        if (bpm > 60 && bpm < 100 || bpm == 60 || bpm == 100)
        {
          normal();
          rate = 0;
          delay(5000);
          bpm = 0;
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Heart Rate:");
        }
        else if (bpm < 60)
        {
          slow();
          rate = 0;
          delay(5000);
          bpm = 0;
          lcd.setCursor(0, 0);
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Heart Rate:");
        }
        else if (bpm > 100)
        {
          fast();
          rate = 0;
          delay(5000);
          bpm = 0;
          lcd.setCursor(0, 0);
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Heart Rate:");
        }
      }

    This code will run when the rate variable is equal to 1. This conditions will analyze if your heart rate is slow, normal or fast. If bpm is between 60 and 100, the normal() function is called and sets the rate variable to 0 to stop this code from running again. And a delay of 5 seconds before resetting the bpm to 0. And clearing the LCD screen leaving the “Heart Rate: ” text on the Screen. The same process will happen, if the bpm is below 60 the slow() function is called and if the bpm is greater than 100 the fast() function will be called.

    void slow()
    {
      lcd.setCursor(0, 0);
      lcd.print("Heart Rate:");
      lcd.setCursor(2, 1);
      lcd.print("Slow");
      lcd.setCursor(9, 1);
      lcd.print("BPM:");
      lcd.setCursor(13, 1);
      lcd.print(bpm);
    }
    void normal()
    {
      lcd.setCursor(0, 0);
      lcd.print("Heart Rate:");
      lcd.setCursor(2, 1);
      lcd.print("normal");
      lcd.setCursor(9, 1);
      lcd.print("BPM:");
      lcd.setCursor(13, 1);
      lcd.print(bpm);
    }
    void fast()
    {
      lcd.setCursor(0, 0);
      lcd.print("Heart Rate:");
      lcd.setCursor(2, 1);
      lcd.print("Fast");
      lcd.setCursor(9, 1);
      lcd.print("BPM:");
      lcd.setCursor(13, 1);
      lcd.print(bpm);
    }

    This code are the functions that are used in the previous code or conditions. slow() function displays “Heart Rate: ” on the first row of the LCD and “slow” and “BPM: ” text are display on the second row of the LCD and also the bpm value next to the “BPM: ” text. And it’s same for the other functions, but for the normal() function instead of the text “slow” it displays “normal” and for the fast() function it displays “fast”.

    Conclusion

    There are so many different kinds of sensors whose purpose is to detect events or changes in it’s environment. And because of that we are able to generate new ideas in making technologies that helps us solve real life problems. For example the pulse sensor that allows us to read live heart-rate data, because of this type of sensor we are able to create a project that monitor or checks our heart rate.

    Reference:

    [1] https://www.health.harvard.edu/heart-health/hows-your-heart-rate-and-why-it-matters

    [2] http://www.circuitbasics.com/basics-of-the-i2c-communication-protocol/

    [3] https://components101.com/sensors/pulse-sensor

    The post Heart Rate Monitoring System using Heart Rate Pulse Sensor with I2C 1602 LCD appeared first on CreateLabz.

    1602ArduinoHeart rateHeart rate monitoring systemHeart rate pulse sensorI2c 1602 lcdI2c lcdKnowledgebaseLcdPulsePulse sensor

    Leave a comment

    All comments are moderated before being published