Adruino Mega keeps restarting when trying to configure pulsesesnor user pulse sensor playground

Hi, I am hoping someone can help me with an issue I am having. I am trying to configure a heart rate sensor from pulsesensor.com. I am using an Adruino Mega. I have downloaded the example sketch, and I have also coded up the entire sketch myself - the Mega's setup method keeps being called repeatedly whenever I run the .begin() method.

Here is my connection

Arduino Sensor
5v---------------------------------------------> VCC
GND-------------------------------------------- - (GND)
A0--------------------------------------------------S

I am using Serial on baud 115200 (as per the instructions).

Here is my code


#define USE_ARDUINO_INTERRUPTS true


const int pulseWire = A1;

const int led = 7;

int threshold = 440;

PulseSensorPlayground pulseSensor;

void setup() 
{
  Serial.begin(115200);
  pulseSensor.analogInput(pulseWire);
  pulseSensor.blinkOnPulse(led);
  pulseSensor.setThreshold(threshold);
  pulseSensor.setSerial(Serial);

  Serial.println("Hi");
  pulseSensor.begin();



}

void loop() {
  
if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened".
int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
                                               // "myBPM" hold this BPM value now. 
 Serial.println("♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
 Serial.print("BPM: ");                        // Print phrase "BPM: " 
 Serial.println(myBPM);                        // Print the value inside of myBPM. 
}

}

I have made sure that I am using the latest version of the pulse sensor playground library, I have looked at all the GitHub examples, no luck
I added the text output "Hi" above the method call to be sure that it is what is causing the board to maybe restart the MCU. I have a looong list of Hi messages. I am a PhD student, in Computer Science, not electronics and I really need to finish this project. Any and all help is appreciated. I am also new and not an expert, so apologies if I broke any rules or posted anything in the wrong format.
Thank you
Sarina

As I understand it computer science deals with the theory of computer processes, while computer programming is the practical application that brings those ideas to life. In this case a lack of electronics should not be a problem.

I suggest you use there code to be sure the hardware is working first.

Thank you so much for your reply. I have used the example sketch , same problem. I have tried in two different Arduino Megas Ans with three different sensors, I still have the same issue.

Have you tried it with the processor they used ie the UNO? There are some differences between them. Since this is a learning experience post a simple schematic showing how it is wired.

The sensor works fine on an Uno - however I need to use a Mega because I need the I/O ports. Is there anything I should be doing differently. Thank you again for your help. I am not sure how to draw an electric diagram, but is literally just the sensor connected to A0, GND and 5V VCC.
Once again thank you for all the help!

Your pulse sensor is connected to A0, but the code is using A1.
:scream:

const int pulseWire = A1;

Hi , thank you so much for replying. My sincere apologies, I have updated the script and not my diagram - I have tried A0, A1, A2 (thought maybe a pin was fried ). So that is not the cause. Apologies for the confusion.
Sarina

Hello Sarina,

I don't have a pulse sensor like yours.

However I have been able to test your code using a Function/Arbitrary Waveform Generator to simulate the output from the pulse sensor.

First of all I tried the code on a Uno R3.
The code worked after I added the missing:

#include <PulseSensorPlayground.h>  

I then tried the same code on an Arduino Mega 2560.
I can confirm that I am getting the same result as you.

Here are the results I got with a 2Hz signal going to both the Uno on Com3 and the Mega on Com9. I'm using A0 on both Arduinos.

The Uno correctly measures 120 bpm.
The Blue oscilloscope trace is the output from pin 7 on the Uno.

THANK YOU for your reply, I was afraid that I was losing my mind. I ended up stepping back the library to 1.7x and it is now reading a heartbeat, but unfortunately is only returning heart attack-type returns , like 232.

There are many varieties of port ex panders available, some in the $1.50 range US. They are easy to use, I do not use any of the libraries except maybe the wire library sometimes. PCF8574 is common for 8 bit and PCF8575 for 16 bit expansions. They connect to the I2C buss so you need +5, Ground, SCL and SDA connections. All of them go to the same four pins on the Arduino IE it is a bus.

Thank you so much for your recommendation, I did not know about these, I will absolutely get some. I am too far into this project to move controllers now ( I think ) it is the last stretch to deploying my PhD artefact and writing up my thesis. Maybe that heart reading is correct (just kidding) . I truly appreciate the support and advice

Good luck, I think you will do very well.