How to read the voltage so I can make the BPM and show it on the LCD

int bpm;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {

  int sensorValue = analogRead(A0); // Arduino reading the ECG values
  float voltage= sensorValue*(7.5/1023.0); // To change the reading up to 5V
  Serial.println(voltage);


      if (voltage <= 5){
       bpm=1;
        delay (3000);
       Serial.print (bpm);
        }
}

So, this is what I have an it's kinda showing the ECG. I have attached the picture.

However, when I'm trying to show it on my LCD and do the calculations, it's not working. This is the code:

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27, 16,2);   
  
  int bpm=0;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  lcd.init ();
  lcd.backlight ();
  lcd.print ("MACHA ECG"); // show company name
}

void loop() {

  int sensorValue = analogRead(A0); // Arduino reading the ECG values
  float voltage= sensorValue*(7.5/1023.0); // To change the reading up to 5V
  Serial.println(voltage);


      do {
        if (voltage >= 5.0){ // read the value what comes from the serial plotter and make the comparation
       bpm= bpm+1; // if the voltage is more than 5V it will count as a heart beat
        delay (3000);
      
        }continue;
        }while (bpm*6);
       

        lcd.display();
        lcd.setCursor(0,3);
        lcd.write(1);
        
       if (bpm>= 100){ 
      lcd.print("Sinus Tachycardia");    
      return; 
      
      } else if (bpm <= 60){
        lcd.print ("Sinus Bradycardia");
        return;
        
      } else{
        lcd.print ("Normal Sinus");
        return;
    }
        
        

Please advise. I’m using a wire from the ECG directly to the patient simulator.

What is the source of beat measurement? Like are you using a wire to your finger or some other sensor?

@anshls, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

===
Take it step-by-step and don't modify too much.

  1. Take the original code and only add some stuff for the lcd and see if that works.
  2. Or add serial prints to your new code for debugging.

It’s a wire from the ECG to the patient simulator

Thank you. I wasn’t sure which one was the most suitable

And your LCD has 2 lines with which you want to display a wave for on, correct?

Make, model, and other info of the LCD display.

I don’t want to display the lines on the LCD display. I want to get the bpm of the heart beat and display the number

well detect the peaks and count them for a determined time frame.

When you look at the wave form you should be able to see a flat line, then a dip, then a sharp rise. detect the sharp rise and count them.

You could do an internet search on the words "arduino peak detector."

I suggest you start with this little sketch and Serial Plotter. Look at the plot and try to figure out what signal level indicates the start of a peak. The value you are using now is about 683.

void setup()
{
  Serial.begin(115200);
  delay(200);
}

void loop()
{
  Serial.println(analogRead(A0));
}

Once you have chosen a trigger point, use the StateChangeDetection example to detect when the level goes from below the trigger point to above the trigger point. When that happens, subtract the previous pulse time from the current time (millis()) and divide that into the number of milliseconds in a minute (60000ul) to get BPM to display.