Measuring Heart Rate With A Piezo

While trying to create a circuit that detects whether water is flowing through a pipe by measuring the vibration with a piezoelectric sensor, just to see what happens I taped the sensor around my finger and – to my surprise – got values that were a very noise-free representation of my heart rate!

This is even easier than using LEDs as it only requires a piezoelectric sensor and an Arduino. (and a piece of tape)
The sensor I used is a DFRobot Piezo Disc Vibration Sensor Module.

Measuring Heart Rate With A Piezo



void loop() {
int avg = 0;
for(int i=0;i<64;i++){
avg+=analogRead(A2);
}
Serial.println(avg/64,DEC);
delay(5);
}
1
2
3
4
5
6
7
8
9
10
11
12
void setup() {
  Serial.begin(57600);
}
void loop() {
  int avg = 0;
  for(int i=0;i<64;i++){
    avg+=analogRead(A2);
  }
  Serial.println(avg/64,DEC);
  delay(5);
}

When defining an arbitrary threshold (e.g. half of the maximum measured value), the rising edge of the signal will pass the threshold once per heartbeat, making measuring it as simple as measuring the time between two successive beats. For less jitter, I chose to calculate the heart rate using the average of the last 16 time differences between the beats.

Here’s a quick and dirty code that calculates the heart rate and outputs the average heart rate over the last 16 beats at every beat:

int threshold = 60;
int oldvalue = 0;
int newvalue = 0;
unsigned long oldmillis = 0;
unsigned long newmillis = 0;
int cnt = 0;
int timings[16];
void setup() {
  Serial.begin(57600);}
void loop() {  oldvalue = newvalue;
  newvalue = 0;  for(int i=0; i<64; i++){ // Average over 16 measurements
    newvalue += analogRead(A2);  }
  newvalue = newvalue/64;  // find triggering edge
  if(oldvalue<threshold && newvalue>=threshold){
    oldmillis = newmillis;
    newmillis = millis();
    // fill in the current time difference in ringbuffer
    timings[cnt%16]= (int)(newmillis-oldmillis);
    int totalmillis = 0;  }
 
 
For more detail: Measuring Heart Rate With A Piezo

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top