wrong pulse sensor readings

the readings from the heartbeat sensor keep going to 232bpm with and without my finger
what is wrong ? :slight_smile:

#include <U8glib.h>
#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>

const int PulseWire = A0;
const int LED13 = 13;
const int Threshold = 550;

const int pinSCK = 4;
const int pinMOSI = 5;
const int pinCS = 6;
const int pinDC = 7;

int bpm = 0;

PulseSensorPlayground pulseSensor;

U8GLIB_SH1106_128X64 SH1106_Display( 4, 5, 6, 7 );


void DrawHeartBeat( int pulseBPM )
{
  char
  szBpm[10];

  SH1106_Display.setFont ( u8g_font_courB12);
  SH1106_Display.drawStr (8, 15 , " MG-MARK-1 ");
  SH1106_Display.setFont ( u8g_font_courB12);
  SH1106_Display.drawStr (3,54 , "BPM:");
  SH1106_Display.drawFrame(0, 0, 128, 64);
  SH1106_Display.drawFrame(72 ,41, 50, 16);
  sprintf( szBpm, "%d", pulseBPM );
  SH1106_Display.drawStr( 40,54, szBpm );
  SH1106_Display.drawBox( 74 ,44, 14, 10);
  SH1106_Display.drawBox( 90,44, 14, 10);
  SH1106_Display.drawBox( 106 ,44, 14, 10);


}


void setup(void)
{
  SH1106_Display.setFont(u8g_font_courB12);
  Serial.begin(9600);

  pulseSensor.analogInput(PulseWire);
  pulseSensor.blinkOnPulse(LED13);
  pulseSensor.setThreshold(Threshold);
  
  if (pulseSensor.begin())
    Serial.println(" pulseSensor activated ");

}

void loop(void)
{
  UpdateBPM();
  UpdateDisplay();

}

void UpdateBPM( void )
{
  static unsigned long
  timeUpdate = 0;
  unsigned long
  timeNow;

  timeNow = millis();

  if ( (timeNow - timeUpdate) < 50 )
    return;
  timeUpdate = timeNow;

  bpm = UpdateHeartRate();
  if ( bpm >= Threshold )
    digitalWrite( LED13, HIGH );
  else
    digitalWrite( LED13, LOW );

}

void UpdateDisplay( void )
{
  static unsigned long
  timeUpdate = 0;
  unsigned long
  timeNow;

  timeNow = millis();

  if ( (timeNow - timeUpdate) < 250 )
    return;
  timeUpdate = timeNow;

  SH1106_Display.firstPage();
  do
  {
    DrawHeartBeat( bpm );
    Serial.print("BPM: ");
    Serial.println( bpm );

  } while ( SH1106_Display.nextPage() );

}

int UpdateHeartRate( void )
{
  return ( pulseSensor.getBeatsPerMinute() );

delay(20
);

Without the SH1106 hardware and software, do you get the right results? In other words, do you have a clue where the problem is?

yep i get pretty accurate results , and if i change the threshold to a higher value the problem disappears but the sensor gets very nonsensitive

nanoR4K:
yep i get pretty accurate results , and if i change the threshold to a higher value the problem disappears but the sensor gets very nonsensitive

So, without some undefined hardware (display, no link) connected, using some unknown pins, your other unknown hardware (pulse sensor, no link) works.

I hope you can figure out what to do.