AdSense

Saturday 24 January 2015

Read out heartbeat sensor

(Deutsche Version) If you look for "heartbeat sensor" on ebay, you find a small circuit board which contains an LED and a photo transistor. This board claims to be able to measure the heart beat. I bought one sensor and tried it. Unfortunately, there is very few (ok, basically absolutely no) documentation about the sensor and not even something like a product key, only xinda and Lcup can be found on the sensor.
The heartbeat sensor
The sensor has 3 pins which are labelled very good. One of them has a -, here you have to connect GND. The next pin has an S, this is the pin for the sensor output. The last pin (in the middle) which has no label is connected to the LED. This pin has to be connected to +5V and the sensor output has to be connected to an analog input of the arduino.

The program reads the sensor value and sends it to the computer via the serial port where you can further process the data (e.g. with Matlab, maybe I will write a post about that). Here is a picture of the plotted heartbeat data on the computer and below the code for the Arduino.
//Define pins for LED and sensor.
int ledPin = 13;
int sensorPin = 0;
//alpha is used in the original proposed code by the company (see below).
double alpha = 0.75;
//lasttime is used to have a very precise measurement of the time so the calculated pulse is correct.
unsigned long lasttime;

void setup ()
{
  //Switch on the LED.
  pinMode (ledPin, OUTPUT);
  digitalWrite(ledPin,HIGH);
  Serial.begin (9600);
  lasttime = micros();
}

void loop ()
{
  //Also used for smoothening the signal.
  static double oldValue = 0;
  
  //Wait 10 ms between each measurement.
  while(micros() - lasttime < 10000)
  {
    delayMicroseconds(100);
  }
  
  //Read the signal.
  int rawValue = analogRead (sensorPin);
  lasttime += 10000;
  
  //In the "original" code example, "value" was sent to the computer. This calculation basically smoothens "rawValue".
  //double value = alpha * oldValue + (1 - alpha) * rawValue;
  
  //Send back the measured value.
  Serial.println (rawValue);
  oldValue = value;
}

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi, your code is very helpful.
    I think there is an error in the code: you have commented this line:
    //double value = alpha * oldValue + (1 - alpha) * rawValue;
    and Arduino gives back an error (no "value" variable found).
    I have the same sensor, and I've tried to use your code, connecting the pins in the same way:
    "Signal" -> A0 ; "+" -> pin13 ; "-" -> GND and I've written in the code the line that before was commented (double value =….).
    I cannot understand why, when I'm not putting my finger in the sensor, it gives me back these value:
    875 (RawValue) , 863.03 (value)
    and when I put the finger:
    933 (RawFinger) , 931.44 (value).
    What does Arduino give you back in the monitor? My values are really strange, it's like if there is an offset..
    Any suggestions? Am I doing something wrong?
    I will be grateful if you can send me this information.
    Thank you

    ReplyDelete
  3. Hi Andrea,
    basically you are right. The line with "double value..." is in the code provided as an example on the item description on ebay. This line simply smoothens the signal. I did all the smoothening in the code on the computer, so I did not need any smoothening. As you can see, I send rawValue back to the computer and oldValue and value are not used, so you could just comment the last line and then everything should work fine.

    When I read out the sensor I get values about 167 when my finger is not in the sensor and about 861 when it is. This value depends on the ambient light, so I always place my finger with the sensor on the table so the least possible amount of light is able to reach the sensor, this is responsible for the measured offset.

    I hope this helped you, if you have further questions, simply ask.
    Udo

    ReplyDelete