Advertisement
microrobotics

AD8232 Heart Rate Monitor Simple Heart Rate Display

Apr 18th, 2023 (edited)
1,535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The AD8232 Heart Rate Monitor Kit is a great way to measure heart rate using Arduino. Here's a sample Arduino code for interfacing with the AD8232 module:
  3.  
  4. This code uses the AD8232 module to measure heart rate and displays the result on an I2C LCD. You can adjust the pin connections and other parameters as needed for your specific setup. Please make sure you've installed the "LiquidCrystal_I2C" library before uploading the code to your Arduino board.
  5. */
  6.  
  7. // Include the required libraries
  8. #include <Wire.h>
  9. #include <LiquidCrystal_I2C.h>
  10.  
  11. // Define the pin connections
  12. const int AD8232_LO_PLUS = 2;
  13. const int AD8232_LO_MINUS = 3;
  14. const int AD8232_OUTPUT = A0;
  15.  
  16. // Create an instance of the LiquidCrystal_I2C library
  17. LiquidCrystal_I2C lcd(0x27, 16, 2);
  18.  
  19. unsigned long previousMillis = 0;
  20. const long interval = 200;
  21. int heartRate = 0;
  22. int beatCounter = 0;
  23.  
  24. void setup() {
  25.   Serial.begin(9600);
  26.   pinMode(AD8232_LO_PLUS, INPUT);
  27.   pinMode(AD8232_LO_MINUS, INPUT);
  28.   pinMode(AD8232_OUTPUT, INPUT);
  29.  
  30.   lcd.init();
  31.   lcd.backlight();
  32.   lcd.setCursor(0, 0);
  33.   lcd.print("Heart Rate:");
  34. }
  35.  
  36. void loop() {
  37.   int LO_plus = digitalRead(AD8232_LO_PLUS);
  38.   int LO_minus = digitalRead(AD8232_LO_MINUS);
  39.  
  40.   if (LO_plus == 1 || LO_minus == 1) {
  41.     lcd.setCursor(0, 1);
  42.     lcd.print("Leads Off");
  43.   } else {
  44.     int output = analogRead(AD8232_OUTPUT);
  45.     calculateHeartRate(output);
  46.   }
  47. }
  48.  
  49. void calculateHeartRate(int output) {
  50.   unsigned long currentMillis = millis();
  51.  
  52.   if (output > 550 && currentMillis - previousMillis >= interval) {
  53.     previousMillis = currentMillis;
  54.     beatCounter++;
  55.     heartRate = 60 * 1000 / interval * beatCounter;
  56.  
  57.     if (beatCounter >= 10) {
  58.       beatCounter = 0;
  59.     }
  60.  
  61.     lcd.setCursor(0, 1);
  62.     lcd.print("          ");
  63.     lcd.setCursor(0, 1);
  64.     lcd.print(heartRate);
  65.     lcd.print(" BPM");
  66.   }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement