Troubleshooting Electrocardiograph Project

I am making an electrocardiograph sensor for my final project in an electronics class I’m taking according to this instructable here

Right now, I can get the speaker to emit a tone and the LCD monitor to print “you are dead <30 BPM”, but it prints you are dead even when the electrodes are attatched in the proper way. I know everything is wired correctly because the path of the current brings the input of the electrodes through to the speaker, and my speaker currently works, it’s just not picking up the electrode’s signal.

I’ve attatched the code because I think it might be a coding problem, and I’m pretty new to arduino.
Do you have any suggestions/has anyone built an electrocardiograph before?

electrocardiograph.ino (2.61 KB)

Please put your code in its own window as seen in other posts. This can be done by placing     [code] and [/code]  around the code. This makes it easier for others to read.

Not everyone reads this on a PC. I use an iPad and cannot read the .ino.


// This program reads the incoming signal from the electrocardiograph and outputs the heart rate

// Pin D7 is connected to the signal input
// Pin D6 is connected to a mini-speaker
// For the connection to the liquid crystal display, see the LiquidCrystal Librairy

// External variables
const int  signal = 7;    // Pin connected to the filtered signal from the circuit
unsigned long time;   
unsigned long frequency;

// Internal variables
int period = 2000;
int starttime = 2000;
int input = 0;
int lastinput = 0;
unsigned long death = 0;

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

byte heart[8] = {

void setup() {
  pinMode(signal, INPUT);
  lcd.createChar(1, heart);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("    bpm :       ");
  lcd.print("You are         ");

void loop() {
  time = millis();
  input = digitalRead(signal);

  if ((input != lastinput)&&(input == HIGH)) {
    // If the pin state has just changed from low to high (edge detector)
     period = time - starttime; // Compute the time between the previous beat and the one that has just been detected
     starttime = time; // Define the new time reference for the next period computing
     death = time;
     tone(6,800,100); // Output a short "bip" through the speaker
  lastinput = input; // Save the current pin state for comparison at the next loop iteration

  if (period < 0) {
    frequency = 0;
  else {
    frequency = 60000/period; // Compute the heart rate in beats per minute (bpm) with the period in milliseconds
  if ((time - death) > 2000) { // Detect if there is no beat after more than 2 seconds
    tone(6,900); // Output a continuous tone to the speaker
    lcd.print("dead!   ");
  else {
    char freq[3];
    if (frequency/100 == 0) {
      freq[0] = 32; // Print a space to the first character if the frequency is below 100 bpm
    else {
      freq[0] = frequency/100+48; // Sort the hundreds character and convert it in ASCII
    freq[1] = (frequency/10)%10+48; // Sort the thents character and convert it in ASCII
    freq[2] = frequency%10+48; // Sort the units character and convert it in ASCII
    lcd.print("alive! ");