Arduino Forum

Topics => Science and Measurement => Topic started by: mentaikosauceboy on Jan 24, 2020, 10:00 am

Title: How to measure the voltage peak from pulse sensor?
Post by: mentaikosauceboy on Jan 24, 2020, 10:00 am
So my project requires me to find the voltage peak from the pulse sensor.

1) How can i measure my voltage peak(The highest amplitude from the graph)


2)How can i take the average value of the Voltage peak?
Lets just say,there are 10 peaks in this waveform.

I am using Arduino Uno and Pulse sensor for heart rate.

Really appreciate if anyone can help me out!
Title: Arduino uno collect peak voltage
Post by: mentaikosauceboy on Jan 25, 2020, 01:16 pm
Hi all,how can i collect my voltage peak with my sensor?

The attached is the graph from serial plotter.

My requirement in code is to collect the average voltage peak for every 10 seconds or so.

How do i go about this?
Title: Re: How to measure the voltage peak from pulse sensor?
Post by: UKHeliBob on Jan 25, 2020, 01:42 pm
Topics merged

Why did you start a new topic on the same subject ?
Title: Re: How to measure the voltage peak from pulse sensor?
Post by: Idahowalker on Jan 25, 2020, 02:18 pm
int someHighestValue = 0;

int some NewValue = 0;

if NewValue > someHighestValue then someHighestValue = NewValue.
Title: Re: How to measure the voltage peak from pulse sensor?
Post by: mentaikosauceboy on Jan 25, 2020, 03:50 pm
How can i measure my peak voltage then?

Appreciate your help!
Title: Re: How to measure the voltage peak from pulse sensor?
Post by: Idahowalker on Jan 25, 2020, 04:17 pm
Well, you could hook the signal up to the Analog To Digital converter pin on the Uno, sample the signal and then do the thing.
Title: Re: How to measure the voltage peak from pulse sensor?
Post by: ballscrewbob on Jan 25, 2020, 04:20 pm
@mentaikosauceboy

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.
Continued cross posting could result in a time out from the forum.

Could you take a few moments to Learn How To Use The Forum (https://forum.arduino.cc/index.php?topic=148850.0).
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here. (https://forum.arduino.cc/index.php?topic=52113.0)
Title: Re: How to measure the voltage peak from pulse sensor?
Post by: Idahowalker on Jan 25, 2020, 06:41 pm
The attached is the graph from serial plotter.

My requirement in code is to collect the average voltage peak for every 10 seconds or so.

How do i go about this?
Have you done anything with the millis() function? There is a lot of information on this site about millis()

So you start a counter of time, add all your readings to a variable, add to another variable the count of readings taken, at the end of the time, divide your totaled readings by the number of readings taken in the time frame allotted.


Title: Re: How to measure the voltage peak from pulse sensor?
Post by: gilshultz on Jan 26, 2020, 02:48 am
You need a peak detector circuit that integrates the signal so you can read it with the A/D. Below is a simple one. The capacitor controls how long the sample will survive, the resistor discharges the capacitor. The combination of RC will allow you to adjust the sample to what you want.
(https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR0jBFxk25hMPoeb-km1PQW9j7-yRgf1etEzTBAv7yEg286857T&s)
There are a lot of on line Peak Detector information. This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil
Title: Re: How to measure the voltage peak from pulse sensor?
Post by: mentaikosauceboy on Jan 28, 2020, 06:10 pm
Hi all thankyou for your kind replies..

I am currently using this code.. How can i go about collecting my max and min vals.. using threshold as I am collecting heart-rate signals/pulse.. and also the MAX and MIN values..!


Code: [Select]

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//  Variables
int pulsePin = A0;                
int blinkPin = 13;                
int fadePin = 10;                
int fadeRate = 0;                
int voltagePeak = 0;


// Volatile Variables, used in the interrupt service routine!
volatile int BPM;                   // int that holds raw Analog in 0. updated every 2mS
volatile int Signal;                // holds the incoming raw data
volatile int IBI = 600;             // int that holds the time interval between beats! Must be seeded!
volatile boolean Pulse = false;     // "True" when User's live heartbeat is detected. "False" when not a "live beat".
volatile boolean QS = false;        // becomes true when Arduoino finds a beat.

// Regards Serial OutPut  -- Set This Up to your needs
static boolean serialVisual = true;   // Set to 'false' by Default.  Re-set to 'true' to see Arduino Serial Monitor ASCII Visual Pulse

volatile int rate[10];                      // array to hold last ten IBI values
volatile unsigned long sampleCounter = 0;          // used to determine pulse timing
volatile unsigned long lastBeatTime = 0;           // used to find IBI
volatile int P = 512;                      // used to find peak in pulse wave, seeded
volatile int T = 512;                     // used to find trough in pulse wave, seeded
volatile int thresh = 525;                // used to find instant moment of heart beat, seeded
volatile int amp = 100;                   // used to hold amplitude of pulse waveform, seeded
volatile boolean firstBeat = true;        // used to seed rate array so we startup with reasonable BPM
volatile boolean secondBeat = false;      // used to seed rate array so we startup with reasonable BPM

void setup()
{
  pinMode(blinkPin,OUTPUT);         // pin that will blink to your heartbeat!
  pinMode(fadePin,OUTPUT);          // pin that will fade to your heartbeat!
  Serial.begin(115200);             // we agree to talk fast!
  lcd.begin(16,2);
  lcd.print("Extracting...");
  lcd.setCursor(0,0);
  lcd.print("Extracting...");
  interruptSetup();                 // sets up to read Pulse Sensor signal every 2mS
                                    // IF YOU ARE POWERING The Pulse Sensor AT VOLTAGE LESS THAN THE BOARD VOLTAGE,
                                    // UN-COMMENT THE NEXT LINE AND APPLY THAT VOLTAGE TO THE A-REF PIN
                                    //   analogReference(EXTERNAL);  
}


//  Where the Magic Happens
void loop()
{
   serialOutput();  
  
  if (QS == true) // A Heartbeat Was Found
    {    
      // BPM and IBI have been Determined
      // Quantified Self "QS" true when arduino finds a heartbeat
      fadeRate = 255; // Makes the LED Fade Effect Happen, Set 'fadeRate' Variable to 255 to fade LED with pulse
      serialOutputWhenBeatHappens(); // A Beat Happened, Output that to serial.    
      QS = false; // reset the Quantified Self flag for next time    
    }
    
  ledFadeToBeat(); // Makes the LED Fade Effect Happen
  delay(20); //  take a break
}

void ledFadeToBeat()
{
   fadeRate -= 15;                         //  set LED fade value
   fadeRate = constrain(fadeRate,0,255);   //  keep LED fade value from going into negative numbers!
   analogWrite(fadePin,fadeRate);          //  fade LED
}

void interruptSetup()
{    
  // Initializes Timer2 to throw an interrupt every 2mS.
  TCCR2A = 0x02;     // DISABLE PWM ON DIGITAL PINS 3 AND 11, AND GO INTO CTC MODE
  TCCR2B = 0x06;     // DON'T FORCE COMPARE, 256 PRESCALER
  OCR2A = 0X7C;      // SET THE TOP OF THE COUNT TO 124 FOR 500Hz SAMPLE RATE
  TIMSK2 = 0x02;     // ENABLE INTERRUPT ON MATCH BETWEEN TIMER2 AND OCR2A
  sei();             // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED      
}

void serialOutput()
{   // Decide How To Output Serial.
 if (serialVisual == true)
  {  
     arduinoSerialMonitorVisual('-', Signal);   // goes to function that makes Serial Monitor Visualizer
  }
 else
  {
      sendDataToSerial('S', Signal);     // goes to sendDataToSerial function
   }        
}

void serialOutputWhenBeatHappens()
{    
 if (serialVisual == true) //  Code to Make the Serial Monitor Visualizer Work
   {    
     voltagePeak = P;        
     //Serial.print("*** Heart-Beat Happened *** ");  //ASCII Art Madness
     Serial.print("BPM: ");
     Serial.println(BPM);
     //lcd.setCursor (0,1);
     Serial.print("Voltage Peak");
     Serial.println(voltagePeak);
     lcd.setCursor(16,2);
     lcd.print("BPM: ");
     lcd.println(BPM);
     lcd.clear();
     lcd.setCursor(0, 1);
     lcd.print("Voltage Peak:");
     lcd.println(voltagePeak);
   }
 else
   {
     sendDataToSerial('B',BPM);   // send heart rate with a 'B' prefix
     sendDataToSerial('Q',IBI);   // send time between beats with a 'Q' prefix
   }  
}

void arduinoSerialMonitorVisual(char symbol, int data )
{    
  const int sensorMin = 0;      // sensor minimum, discovered through experiment
  const int sensorMax = 1024;    // sensor maximum, discovered through experiment
  int sensorReading = data; // map the sensor range to a range of 12 options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 11);
  // do something different depending on the
  // range value:
  switch (range)
 
void sendDataToSerial(char symbol, int data )
{
   Serial.print(symbol);
   Serial.println(data);                
}

ISR(TIMER2_COMPA_vect)
{  
  cli();                                      
  Signal = analogRead(pulsePin);              
  sampleCounter += 2;                        
  int N = sampleCounter - lastBeatTime;      
                                              
  if(Signal < thresh && N > (IBI/5)*3)
    {      
      if (Signal < T) // T is the trough
      {                        
        T = Signal; // keep track of lowest point in pulse wave
      }
    }

  if(Signal > thresh && Signal > P)
    {          // thresh condition helps avoid noise
      P = Signal;                            
    }                                        

  //  NOW IT'S TIME TO LOOK FOR THE HEART BEAT
  // signal surges up in value every time there is a pulse
  if (N > 250)
  {                                   // avoid high frequency noise
    if ( (Signal > thresh) && (Pulse == false) && (N > (IBI/5)*3) )
      {        
        Pulse = true;                              
        digitalWrite(blinkPin,HIGH);                
        IBI = sampleCounter - lastBeatTime;        
        lastBeatTime = sampleCounter;              
  
        if(secondBeat)
        {                      
          secondBeat = false;                  
          for(int i=0; i<=9; i++)
          {            
            rate[i] = IBI;                      
          }
        }
  
        if(firstBeat)
        {                        
          firstBeat = false;                  
          secondBeat = true;                
          sei();                              
          return;                              
        }  
      // keep a running total of the last 10 IBI values
      word runningTotal = 0;                    

      for(int i=0; i<=8; i++)
        {                // shift data in the rate array
          rate[i] = rate[i+1];                  
          runningTotal += rate[i];            
        }

      rate[9] = IBI;                        
      runningTotal += rate[9];                
      runningTotal /= 10;                    
      BPM = 60000/runningTotal;            
      QS = true;                            
      // QS FLAG IS NOT CLEARED INSIDE THIS ISR
    }                      
  }

  if (Signal < thresh && Pulse == true)
    {  
      digitalWrite(blinkPin,LOW);          
      Pulse = false;                        
      amp = P - T;                          
      thresh = amp/2 + T;                    
      P = thresh;                          
      T = thresh;
    }

  if (N > 2500)
    {                          
      thresh = 512;                          
      P = 512;                              
      T = 512;                              
      lastBeatTime = sampleCounter;                  
      firstBeat = true;                    
      secondBeat = false;                  
    }