Two pulse sensors in the same time is not working (arduino nano)

Hi everyone.

i'm using 2 pulse sensors (the one with a heart shape in it) to measure my BPM both in the same time because each one will be connected to a different place.

the problem is that when i use only one everything is fine, once i add the second one only the first one gives me BPM and the second doesn't detect it.

when i switch places, the second one work and the first one not.

So, my question is (does Arduino nano support 2 interrupt pins at the same time?) i'm using A0 and A7 (tried different combinations) )


/*  Getting_BPM_to_Monitor prints the BPM to the Serial Monitor, using the least lines of code and PulseSensor Library.
    Tutorial Webpage: https://pulsesensor.com/pages/getting-advanced

  --------Use This Sketch To------------------------------------------
  1) Displays user's live and changing BPM, Beats Per Minute, in Arduino's native Serial Monitor.
  2) Print: "♥  A HeartBeat Happened !" when a beat is detected, live.
  2) Learn about using a PulseSensor Library "Object".
  4) Blinks LED on PIN 13 with user's Heartbeat.
  --------------------------------------------------------------------*/

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   


const int OUTPUT_TYPE = SERIAL_PLOTTER;

/*
   Number of PulseSensor devices we're reading from.
*/
const int PULSE_SENSOR_COUNT = 2;



//  Variables
const int PULSE_INPUT0 = A0;
const int PULSE_BLINK0 = 13;    // Pin 13 is the on-board LED
const int PULSE_FADE0 = 5;

const int PULSE_INPUT1 = A7;
const int PULSE_BLINK1 = 3;
const int PULSE_FADE1 = 11;

const int THRESHOLD = 525;   // Adjust this number to avoid noise when idle
// Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
// Otherwise leave the default "550" value.

PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"


void setup() {

  Serial.begin(250000);          // For Serial Monitor

  // Configure the PulseSensor object, by assigning our variables to it.
  pulseSensor.analogInput(PULSE_INPUT0, 0);
  pulseSensor.blinkOnPulse(PULSE_BLINK0, 0);
  pulseSensor.fadeOnPulse(PULSE_FADE0, 0);

  pulseSensor.analogInput(PULSE_INPUT1, 1);
  pulseSensor.blinkOnPulse(PULSE_BLINK1, 1);
  pulseSensor.fadeOnPulse(PULSE_FADE1, 1);

  pulseSensor.setSerial(Serial);
  pulseSensor.setOutputType(OUTPUT_TYPE);
  pulseSensor.setThreshold(THRESHOLD);

  // Double-check the "pulseSensor" object was created and "began" seeing a signal.
  if (pulseSensor.begin()) {
    Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.
  }
}



void loop() {

  int BPM1 = pulseSensor.getBeatsPerMinute(0);  // Calls function on our pulseSensor object that returns BPM as an "int".
  int BPM2 = pulseSensor.getBeatsPerMinute(1);  // Calls function on our pulseSensor object that returns BPM as an "int".

  for (int i = 0; i < PULSE_SENSOR_COUNT; ++i) {
    if (pulseSensor.sawStartOfBeat(i)) {
      pulseSensor.outputBeat(i);
      if (i == 0)
      {
        Serial.print("BPM (1): ");
        Serial.println(BPM1);
      }
      else{
        
         Serial.print("BPM (2): ");
        Serial.println(BPM2);
        
        }
    }
  }

  delay(20);                    // considered best practice in a simple sketch.

}

Please state the exact type of Arduino you are using, post links to the sensors and the library.

No MCU can do two things at the "same time", all actions are sequential. That includes interrupts.

There is a delay statement in your code, which means "do nothing at all", and the comment
// considered best practice in a simple sketch.
demonstrates that whoever wrote the program does not understand best practice for an Arduino

Hi,
Can you please post a schematic of your circuit please?

Thanks.. Tom. :smiley: :+1: :coffee: :australia:

Arduino nano

Its the playground library for the pulse rate sensor.

My sketch is a modified version of the example sketch and they had this delay which i dont know the reason for.

Its really a simple one.

Only 2 sensors connected to 5V and GND of arduino nano. And the signal pin is connected to A0 and A7.

I have relay and buzzer and led but they are simple on/off devices i dont think they interfere with reading the pulse sensor.

If you're trying to use A7 as a digital on a Nano, pretty sure you can forget it. Switch to another analog, from A0 thru A5.

No im using it as analog input.
Because pulse sensor is giving analog values but use interrupt to automatically read the value from the sensor and give pulse per min value

Hi,
Did you try the example AS PRESENTED in the examples of PulseSensorPlayground.h?

/*
   Arduino Sketch to detect pulses from two PulseSensors.

   Here is a link to the tutorial
   https://pulsesensor.com/pages/two-or-more-pulse-sensors

   Copyright World Famous Electronics LLC - see LICENSE
   Contributors:
     Joel Murphy, https://pulsesensor.com
     Yury Gitman, https://pulsesensor.com
     Bradford Needham, @bneedhamia, https://bluepapertech.com

   Licensed under the MIT License, a copy of which
   should have been included with this software.

   This software is not intended for medical use.
*/

/*
   Every Sketch that uses the PulseSensor Playground must
   define USE_ARDUINO_INTERRUPTS before including PulseSensorPlayground.h.
   Here, #define USE_ARDUINO_INTERRUPTS false tells the library to
   not use interrupts to read data from the PulseSensor.

   If you want to use interrupts, simply change the line below
   to read:
     #define USE_ARDUINO_INTERRUPTS true

   Set US_PS_INTERRUPTS to false if either
   1) Your Arduino platform's interrupts aren't yet supported
   by PulseSensor Playground, or
   2) You don't wish to use interrupts because of the side effects.

   NOTE: if US_PS_INTERRUPTS is false, your Sketch must
   call pulse.sawNewSample() at least once every 2 milliseconds
   to accurately read the PulseSensor signal.
*/
#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>


/*
   The format of our output.

   Set this to PROCESSING_VISUALIZER if you're going to run
    the multi-sensor Processing Visualizer Sketch.
    See https://github.com/WorldFamousElectronics/PulseSensorAmped_2_Sensors

   Set this to SERIAL_PLOTTER if you're going to run
    the Arduino IDE's Serial Plotter.
*/
const int OUTPUT_TYPE = SERIAL_PLOTTER;

/*
   Number of PulseSensor devices we're reading from.
*/
const int PULSE_SENSOR_COUNT = 2;

/*
     PULSE_POWERx = the output pin that the red (power) pin of
      the first PulseSensor will be connected to. PulseSensor only
      draws about 4mA, so almost any micro can power it from a GPIO.
      If you don't want to use pins to power the PulseSensors, you can remove
      the code dealing with PULSE_POWER0 and PULSE_POWER1.
     PULSE_INPUTx = Analog Input. Connected to the pulse sensor
      purple (signal) wire.
     PULSE_BLINKx = digital Output. Connected to an LED (must have at least
      470 ohm resistor) that will flash on each detected pulse.
     PULSE_FADEx = digital Output. PWM pin onnected to an LED (must have
      at least 470 ohm resistor) that will smoothly fade with each pulse.

     NOTE: PULSE_FADEx must be pins that support PWM.
       If USE_INTERRUPTS is true, Do not use pin 9 or 10 for PULSE_FADEx
       because those pins' PWM interferes with the sample timer.
*/
const int PULSE_INPUT0 = A0;
const int PULSE_BLINK0 = 13;    // Pin 13 is the on-board LED
const int PULSE_FADE0 = 5;

const int PULSE_INPUT1 = A1;
const int PULSE_BLINK1 = 12;
const int PULSE_FADE1 = 11;

const int THRESHOLD = 550;   // Adjust this number to avoid noise when idle

/*
   All the PulseSensor Playground functions.
   We tell it how many PulseSensors we're using.
*/
PulseSensorPlayground pulseSensor(PULSE_SENSOR_COUNT);

void setup() {
  /*
     Use 250000 baud because that's what the Processing Sketch expects to read,
     and because that speed provides about 25 bytes per millisecond,
     or 50 characters per PulseSensor sample period of 2 milliseconds.

     If we used a slower baud rate, we'd likely write bytes faster than
     they can be transmitted, which would mess up the timing
     of readSensor() calls, which would make the pulse measurement
     not work properly.
  */
  Serial.begin(250000);

  /*
     Configure the PulseSensor manager,
     telling it which PulseSensor (0 or 1)
     we're configuring.
  */

  pulseSensor.analogInput(PULSE_INPUT0, 0);
  pulseSensor.blinkOnPulse(PULSE_BLINK0, 0);
  pulseSensor.fadeOnPulse(PULSE_FADE0, 0);

  pulseSensor.analogInput(PULSE_INPUT1, 1);
  pulseSensor.blinkOnPulse(PULSE_BLINK1, 1);
  pulseSensor.fadeOnPulse(PULSE_FADE1, 1);

  pulseSensor.setSerial(Serial);
  pulseSensor.setOutputType(OUTPUT_TYPE);
  pulseSensor.setThreshold(THRESHOLD);


  // Now that everything is ready, start reading the PulseSensor signal.
  if (!pulseSensor.begin()) {
    /*
       PulseSensor initialization failed,
       likely because our Arduino platform interrupts
       aren't supported yet.

       If your Sketch hangs here, try changing USE_ARDUINO_INTERRUPTS to false.
    */
    for (;;) {
      // Flash the led to show things didn't work.
      digitalWrite(PULSE_BLINK0, LOW);
      delay(50);
      digitalWrite(PULSE_BLINK0, HIGH);
      delay(50);
    }
  }
}

void loop() {

  /*
     Wait a bit.
     We don't output every sample, because our baud rate
     won't support that much I/O.
  */
  delay(20);

  // write the latest sample to Serial.
  pulseSensor.outputSample();

  /*
     If a beat has happened on a given PulseSensor
     since we last checked, write the per-beat information
     about that PulseSensor to Serial.
  */
  for (int i = 0; i < PULSE_SENSOR_COUNT; ++i) {
    if (pulseSensor.sawStartOfBeat(i)) {
      pulseSensor.outputBeat(i);
    }
  }
}

If not, can you rewire your sensors to A0 and A1 and try it?
Don't edit the example, use it as it was written.
Note serial monitor is at 250000, it was written to run with Processing, but it should still run on the IDE serial monitor if you set the IDE 250000.
If that speed doesn't work try 115200 baud, edit the example.

Tom.... :smiley: :+1: :coffee: :australia:

As you know the example for 2 sensors does not give you the BPM only the pulse wave form.

So i tried to take some command from the other example (which calculate the BPM)

And tried my best to merge them and get 2 BPM from the two sensors.

Yes i take note of the different speed for different examples

POST A LINK TO THE LIBRARY YOU ARE USING.

I've glanced through this pulse sensor library code, and see that can manage more than one sensor. If that is the library you are using, you are not using it properly.

   /*
       Construct the one PulseSensor Playground manager,
       that manages the given number of PulseSensors.
       Your Sketch should declare either PulseSensorPlayground() for one sensor
       or PulseSensorPlayground(n) for n PulseSensors.

       For example:
         PulseSensorPlayground pulse();
       or
         PulseSensorPlayground pulse(2); // for 2 PulseSensors.

I'm using the same library but only one sensor work good, when i use the second sensor i see the first sensor start to copy the result as the second one and both the results become not very useful.

And the library only provide pulse wave form for 2 sensors.
But i need BPM for both sensors

You are doing that incorrectly. Follow the directions in the library, if you want your program to work.

I followed all YouTube videos about it, no one used 2 sensors to measure BPM.

even the library creators.

They only put a link to another program (processing) to measure BPM for 2 sensors.

I need someone with previous experience with these sensors to help me

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.