Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO Based Heartbeat Monitor with KY-039 Sensor

Image of Arduino UNO Based Heartbeat Monitor with KY-039 Sensor

Circuit Documentation

Summary

This circuit consists of an Arduino UNO microcontroller board interfaced with a KY-039 Heartbeat Sensor module. The Arduino UNO is responsible for processing the analog signal from the KY-039 sensor to detect heartbeats and calculate the heartbeat rate. The sensor's output is connected to one of the Arduino's analog input pins for signal processing. The Arduino is programmed to read the sensor data, process the signal to identify heartbeats, and calculate the beats per minute (BPM), which is then output through the serial interface.

Component List

Arduino UNO

  • Description: A microcontroller board based on the ATmega328P.
  • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D13-D0.
  • Purpose: Acts as the central processing unit for the circuit, reading sensor data, processing signals, and communicating results.

KY-039 Heartbeat Sensor

  • Description: A sensor module designed to detect the pulse of a finger.
  • Pins: GND, Vcc, S (Signal).
  • Purpose: Measures the blood volume changes in the fingertip and outputs an analog signal corresponding to a heartbeat.

Wiring Details

Arduino UNO

  • 5V: Provides power to the KY-039 sensor.
  • GND: Common ground with the KY-039 sensor.
  • A0: Receives the analog signal from the KY-039 sensor's signal output.

KY-039 Heartbeat Sensor

  • Vcc: Connected to the 5V output on the Arduino UNO.
  • GND: Connected to the ground (GND) on the Arduino UNO.
  • S (Signal): Connected to the analog input A0 on the Arduino UNO.

Documented Code

#define samp_siz 4
#define rise_threshold 4

// Pulse Monitor Test Script
int sensorPin = 0;

void setup() {
    Serial.begin(9600);
}

void loop ()
{
    float reads[samp_siz], sum;
    long int now, ptr;
    float last, reader, start;
    float first, second, third, before, print_value;
    bool rising;
    int rise_count;
    int n;
    long int last_beat;

    for (int i = 0; i < samp_siz; i++)
      reads[i] = 0;
    sum = 0;
    ptr = 0;

    while(1)
    {
      // calculate an average of the sensor
      // during a 20 ms period (this will eliminate
      // the 50 Hz noise caused by electric light
      n = 0;
      start = millis();
      reader = 0.;
      do
      {
        reader += analogRead (sensorPin);
        n++;
        now = millis();
      }
      while (now < start +  20);  
      reader /= n;  // we got an average
      
      // Add the newest measurement to an array
      // and subtract the oldest measurement from the array
      // to maintain a sum of last measurements
      sum -= reads[ptr];
      sum += reader;
      reads[ptr] = reader;
      last = sum / samp_siz;
      // now last holds the average of the values in the array

      // check for a rising curve (= a heart beat)
      if (last > before)
      {
        rise_count++;
        if (!rising && rise_count > rise_threshold)
        {
          // Ok, we have detected a rising curve, which implies a heartbeat.
          // Record the time since last beat, keep track of the two previous
          // times (first, second, third) to get a weighed average.
          // The rising flag prevents us from detecting the same rise more than once.
          rising = true;
          first = millis() - last_beat;
          last_beat = millis();

          // Calculate the weighed average of heartbeat rate
          // according to the three last beats
          print_value = 60000. / (0.4 * first + 0.3 * second + 0.3 * third);
          
          Serial.print(print_value);
          Serial.print('\n');
          
          third = second;
          second = first;
          
        }
      }
      else
      {
        // Ok, the curve is falling
        rising = false;
        rise_count = 0;
      }
      before = last;
      
      
      ptr++;
      ptr %= samp_siz;

    }
}

Filename: sketch.ino

Description: This code is designed to run on an Arduino UNO and reads analog values from the KY-039 Heartbeat Sensor connected to pin A0. It processes the signal to detect the rise and fall associated with heartbeats and calculates the time between beats to determine the beats per minute (BPM). The BPM value is then sent to the serial monitor for observation. The code includes noise reduction by averaging sensor readings over a 20 ms period to filter out 50 Hz noise from electrical lighting.