BH1790GLC Heart rate calculation

Hey,

I have been tring to calculate the heart rate using this sensor with the example code "Heart Rate".
Everything works great except the bpm value that continiously gives "0".
thanks

BH1790GLC_HeartRate (1).zip (30.8 KB)

Why does everything else work great ?
When you disconnect the sensor, do you have the same result ?
The functions to communicate with the sensor say: "Need to implement I2C".
Which Arduino board do you use ? That is a 3.3V sensor and some Arduino boards are 5V boards.

People here usually don't like to download zip files. People prefer to view code and images in the posts directly.

everything works great because there is another sample code that check if sensor works and the connection is well.

I know that people usually prefer to see the code here, except it includes many libraries..

but this is the main code:

thanks for your help!

#include <Wire.h>
#include <BH1790GLC.h>
#include <FlexiTimer2.h>

extern "C" {
#include <hr_bh1790.h>
}

#define SAMPLING_CNT_32HZ   (32)

BH1790GLC bh1790glc;
volatile bool timer_flg;
void timer_isr(void);

static uint8_t    s_cnt_freq = 0;

void setup() {

  uint16_t ret16 = ERROR_NONE;

  timer_flg = false;
  Serial.begin(115200);
  while (!Serial);

  Wire.begin();
  Wire.setClock(400000L);

  s_cnt_freq = 0;

  ret16 = hr_bh1790_Init();
  if (ret16 == ERROR_NONE) {
    Serial.println(F("BPM, wearing"));
    FlexiTimer2::stop();
    FlexiTimer2::set(250, 1.0/8000, timer_isr);  // 32Hz timer
    FlexiTimer2::start();

    ret16 = hr_bh1790_StartMeasure();
    if (ret16 != ERROR_NONE) {
      Serial.println(F("Error: hr_bh1790_StartMeasure function"));
      Serial.print(F("ret16 = "));
      Serial.println(ret16, HEX);      
    }
  } else {
    Serial.println(F("Error: hr_bh1790_Init function"));
    Serial.print(F("ret16 = "));
    Serial.println(ret16, HEX);
  }
}

void loop() {

  uint8_t  bpm     = 0U;
  uint8_t  wearing = 0U;
  uint16_t ret16   = ERROR_NONE;

  if (timer_flg) {
    ret16 = hr_bh1790_Calc(s_cnt_freq);
    if (ret16 == ERROR_NONE) {
      s_cnt_freq++;
      if (s_cnt_freq >= SAMPLING_CNT_32HZ) {
        s_cnt_freq = 0;
        hr_bh1790_GetData(&bpm, &wearing);
        Serial.print(bpm, DEC);
        Serial.print(F(","));
        Serial.println(wearing, DEC);
      }
    } else {
      Serial.println(F("Error: hr_bh1790_Calc function"));
      Serial.print(F("ret16 = "));
      Serial.println(ret16, HEX);
    }
    timer_flg = false;
  }
}

void timer_isr(void) {
  timer_flg = true;
}

int8_t bh1790_Write(uint8_t adr, uint8_t *data, uint8_t size)
{
  byte   rc  = 0;
  int8_t ret = 0;
  
  rc = bh1790glc.write(adr, data, size);
  if (rc == 0) {
    ret = BH1790_RC_OK;
  } else {
    ret = BH1790_RC_I2C_ERR;
  }

  return (ret);
}

int8_t bh1790_Read(uint8_t adr, uint8_t *data, uint8_t size)
{
  byte   rc  = 0;
  int8_t ret = 0;

  rc = bh1790glc.read(adr, data, size);
  if (rc == 0) {
    ret = BH1790_RC_OK;
  } else {
    ret = BH1790_RC_I2C_ERR;
  }
  
  return (ret);
}