KY039 Arduino Heart rate code

Hi all,

I am a newly using arduino. I got an KY039 sensor to get heartbeat and rate.

So I have 2 questions
2- is it normal that last value are around 1023?
1 - How to calculate Rate every 1min in this code?

this is the code ...thanks

// Project 12 - Pulse Monitor Test Script

int ledPin = 13;
int sensorPin = 0;
double alpha = 0.75;
int period = 20;
double change = 0.0;
void setup()                   
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
 }
void loop()                    
{
    static double oldValue = 0;
    static double oldChange = 0;
    int rawValue = analogRead(sensorPin);
    double value = alpha * oldValue + (1 - alpha) * rawValue;
    Serial.print(rawValue);
    Serial.print(",");
    Serial.println(value);
    oldValue = value;
    delay(period);
}

Please remove the unnecessary blank lines from your code and post it again using code tags as requested in Read this before posting a programming question ... - Programming Questions - Arduino Forum

Is that really the output from that program ? Why are there no commas in it ?

How is the sensor wired ?

Hi,
I tried to delete spacing , sorry.
with commas is the same, with using commas,I tried to get value, oldvalue and change still weird too.
I add a pic of the sensor

Promotion-5pcs-lot-5V-Heartbeat-Sensor-Senser-Detector-Module-By-Finger-For-Arduino.jpg_50x50.jpg

The code is easier to read but you have put it in quote tags rather than code tags.

The picture of the sensor is no good at all. Too small and it does not show how you have connected it to the Arduino. If you are consistently reading rawValue as 1023 then my guess would be that the sensor is wired wrongly. Please provide a diagram, hand drawn if necessary, of how the sensor is wired. The sensor appears to have 3 pins. What are they and which Arduino pins are they wired to ?

Ok thanks, my post is much better now.

Sensor is well connected, do you know why I am getting data around 1023 plz?

Sensor is well connected, do you know why I am getting data around 1023 plz?

I will ask one more time then I am going to give up if you don't provide details.

Exactly how is it connected ? Which sensor pin to which Arduino pin ? The larger picture tells us nothing. For instance, have you got the sensor analogue pin connected to Arduino A0 or Arduino pin 0 ?

If you have got the wiring right then have you tried shielding the whole sensor to prevent ambient light affecting the reading ? I assume that you are actually putting your finger between the LED and the sensor in the first place.

Hi,
signal pin of Sensor is connected to A0 analog Arduino UNO.
I think that you just answered my question... and yah I was puting finger between two led emitter/reciever. I shall cover it from light, that is right?

a pic of my connection.

The zedgraph Im a getting on C# is weired too.

The connections to the sensor look right in the picture but I cannot see a resistor for that other LED. I know that you are not using it in this project, but it looks wrong to me just connected to two Arduino pins.

Yes, I meant that you should put the sensor in the dark and try reading it.

Hi,

even in dark it give same results... I will change sensor, maybe it is defected
Thanks

I also didn't understand how to make use of the data this sensor was returning. When I probed the signal from the pulse rate sensor with an oscilloscope, there was no discernible voltage change when my finger was on the sensor. i.e. it wasn't picking up any pulse and showing even a slight difference in values. The waveform was flat and zoomed in as far as the scope would allow.

The sensor was responding though. When I had my finger on the sensor with the IR LED right on top of my finger nail I was getting integer values in the 960's. When uncovered, I got readings in the low 900's.

My code is almost identical to that posted above (it's the sample code shown in the pdf for the sensor pack this comes in).

// Pulse Monitor Test Script from 37-1 manual
int  sensPin      = 19;     //Sample code specs pin 2, which is digital and does nothing that I can tell
                                       // in that the return values don't change, regardless of sensor being covered
                                       // So I assigned an analog pin
double alpha   = 0.75;
int period         = 20;
double change = 0.0;

void setup ()
{
  pinMode (sensPin, INPUT);
  Serial.begin (115200);
}

void loop ()
{
	static double oldValue = 0;
	int rawValue = analogRead (sensPin);
	double value = alpha * oldValue + (1 - alpha) * rawValue;
	Serial.print (rawValue);
	Serial.print (",");
	Serial.println (value);
	oldValue = value;
	delay (period);
}

Not sure how to proceed but if anyone has had success with this sensor, I'd love to hear about it. Thanks

Try the code on this page http://www.ebay.co.uk/itm/NEW-KY-039-Finger-Measuring-Heartbeat-Sensor-Module-for-Arduino-/331127966386

Note the advice on the page about shielding stray light from the sensor.

The same problem: The same connected; The same code
Always ~1022 in console (light / dark)
Tested two sensors

With finger drop to ~900

The sensor does not light up. So it should be?

1021,1021.54
1022,1021.65
1021,1021.49
1021,1021.37
1021,1021.28
1022,1021.46
1022,1021.59
1021,1021.44
1021,1021.33
1021,1021.25
1022,1021.44
1021,1021.33
1022,1021.50
1021,1021.37
1021,1021.28
1021,1021.21
1021,1021.16
1021,1021.12
1022,1021.34
1022,1021.50
1022,1021.63
1021,1021.47
1021,1021.35
1022,1021.51
1021,1021.39
1022,1021.54
1021,1021.40
1022,1021.55
1021,1021.42
1022,1021.56
1022,1021.67
1021,1021.50

int ledPin = 13;
int sensorPin = 0;
double alpha = 0.75;
int period = 20;
double change = 0.0;
void setup ()
{
  pinMode (ledPin, OUTPUT);
  Serial.begin (115200);
}
void loop ()
{
    static double oldValue = 0;
    static double oldChange = 0;
    int rawValue = analogRead (sensorPin);
    double value = alpha * oldValue + (1 - alpha) * rawValue;
    
    Serial.print (rawValue);
    Serial.print (",");
    Serial.println (value);
    oldValue = value;
    delay (period);
}

Like you are holding the sensor in the picture it can not work. The light from the led is going over your finger. The concept is that it reflects from your finger tot the phototransistor. Or it goes trough your finger.
So you will have to point the led on your finger, for it to work.

I'm after getting one of these sensors,should there be light coming from the Led when you apply power to the sensor?

In the code I think there is error.

You can verify if trasmitter LED ( 5mm round ) lights with you smartphone camera (keep a trasmitter led near camera and if you must see a blue light). If didn't, try to modify code:

void setup ()
{
pinMode (ledPin, OUTPUT);
digitalWrite(ledPin,HIGH); //new line to power on led
Serial.begin (115200);
}

Retry to see led light.
With led on you must read min value.

Did you solve the problem ?

The Keyes-039 or KY039 Heart rate monitor consists of two things, an Infrared LED, and an Infrared Phototransistor. The IR LED should come on whenever the sensor has power and stay on (because it is infrared, you won't be able to see that it is on, but if you look through your cell phone camera as suggested previously you will). The IR photo transistor causes the voltage to change on a "sensor" wire, and this should be connected to one of the Arduino's analog pins.

Here is a picture of the sensor:

And here is a schematic:

There are 3 pins. The top pin (with the backwards S by it) should go to one of the 6 analog pins of the Arduino (A0 to A5), The middle pin should be plugged into 5 volts, and the bottom pin (with the - next to it) should be plugged into ground.

Once the Arduino is powered the IR LED should come on and stay on (check with a cell phone camera).

Commented code for seeing the values is as follows:

// Pulse Monitor Test Script
int ledPin = 13; // This pin is connected on the Arduino board to an LED, if you want to blink this LED
                       // to indicate a pulse is being detected, defining it here is a good idea, however
                       // in the code that follows, this is never done, so this line could be deleted.
int sensorPin = 0; // This is the analog sensor pin the backwards S pin is connected to
                           // you can use any of the analog pins, but would need to change this to match
double alpha = 0.75; // This code uses a rather cool way of averaging the values, using 75% of the 
                               // average of the previous values and 25% of the current value.
int period = 20; // This is how long the code delays in milliseconds between readings (20 mSec)
double change = 0.0; // My guess is that this was going to be used to detect the peaks, but the 
                                // code never does... you can delete this line as well.

// This code runs on startup, it sets ledPin to output so it can blink the LED that is built into the
// Arduino (but then never does), and sets the serial port up for fast 115,200 baud communication
// of the values (make sure you change your serial monitor to match).
void setup ()
{
  pinMode (ledPin, OUTPUT); // not used, can remove
  Serial.begin (115200); // the baud rate of the serial data
}
void loop ()
{
    static double oldValue = 0; // used for averaging.
    static double oldChange = 0; // not currently used
    int rawValue = analogRead (sensorPin); // This reads in the value from the analog pin.
                                                              // this is a 10 bit number, and will be between 0 and 1023
                                                              // If this value doesn't change, you've connected up
                                                              // something wrong


    double value = alpha * oldValue + (1 - alpha) * rawValue; // Calculate an average using 75% of the
                                                                                         // previous value and 25% of the new 
    
    Serial.print (rawValue); // Send out serially the value read in
    Serial.print (",");          // Send a comma
    Serial.println (value);   // Send out the average value and a new line
    oldValue = value;        // Save the average for next iteration
    delay (period);            // Wait 20 mSec
}

Hi guys, thank you for your effort for explaining it. I followed the commented code, I can understand it now however it is still giving me the same results.

yttytyt.png

Hello,

I just got one of those, and i was able to (sort of) make a led blink with the same frequency of my heart.

As you can see at the image below, in a heart beat there is more than one area of "no pressure variation". Consider this while you coding.

Do not press your finger too tight, or the sensor wont catch the blood pressure change.

Using the code below, the led will light when there is no pressure variation at the sensor.

int ledc = 10;  
int sensorPin = 0;
int period = 100;
int change, oldvalue, value;
void setup ()
{
  pinMode(ledc, OUTPUT);
  Serial.begin (9600);
}
void loop ()
{
  value = analogRead (sensorPin);
  change = abs(oldvalue - value);
  if(change >= 2 )
  {
    Serial.println (change);
    analogWrite(ledc, 0); 
  }
  else
  { 
    analogWrite(ledc,255); 
  }
  oldvalue = value;
  delay (period);
}