Pulse sensor not working

I am doing project with arduino uno r4 wifi, MAX30100 pulse sensor, LM35 sensor, GPS neo 6M, and sim800C. I have issues with the MAX30100 pulse sensor and GPS. They don't send back data, just 0.
Wire:
GPS neo 6M Arduino
VCC 5V
RX 9
TX 8
GND GND

pulse sensor Arduino
VIN 3.3V
GND GND
SCL A5
SDA A4
Anyone has any ideas? Please help.

/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/22415be1-a3e3-4581-9e61-3053d7e61969

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudHeartRate heart_rate;
  CloudTemperatureSensor temperature;
  CloudLength length;
  CloudLocation location;
  bool emergency_button;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <WiFi.h>
#include <ArduinoIoTCloud.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <MAX30100_PulseOximeter.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>

#define SIM800_TX 10
#define SIM800_RX 11
#define GPS_TX 8
#define GPS_RX 9
#define TEMP_SENSOR A0
#define HR_READ_INTERVAL 1000  // Read every 1 second

unsigned long lastHRRead = 0;
uint32_t tsLastReport = 0;
void onBeatDetected()
{

  Serial.println("Beat!!!");

}

PulseOximeter pulseSensor;
TinyGPSPlus gps;
SoftwareSerial sim800(SIM800_TX, SIM800_RX);
SoftwareSerial gpsSerial(GPS_TX, GPS_RX);

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  sim800.begin(9600);
  gpsSerial.begin(9600);
  pulseSensor.begin();
  pinMode(TEMP_SENSOR, INPUT);
  pulseSensor.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
  pulseSensor.setOnBeatDetectedCallback(onBeatDetected);

  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);
  // Kết nối WiFi
  WiFi.begin(SSID, PASS);
  Serial.print("Đang kết nối WiFi...");

  int timeout = 20; // Giới hạn thời gian kết nối
  while (WiFi.status() != WL_CONNECTED && timeout > 0) {
    delay(1000);
    Serial.print(".");
    timeout--;
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nWiFi đã kết nối!");
    Serial.print("Địa chỉ IP: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("\nKết nối WiFi thất bại!");
    return;
  }

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
  */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  readTemperature();
  if (!pulseSensor.begin()) {
    Serial.println("❌ MAX30100 initialization failed! Check wiring.");
    while (1); // Stop execution if sensor is not found
  } else {
    Serial.println("✅ MAX30100 Initialized successfully!");
  }
  if (millis() - lastHRRead > HR_READ_INTERVAL) {
    readHeartRate();
    lastHRRead = millis();
  }
  readGPS();
  // Your code here
  // Kiểm tra WiFi
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi bị mất kết nối! Đang thử kết nối lại...");
    WiFi.begin(SSID, PASS);
  } else {
    Serial.println("WiFi OK!");
  }

  // Kiểm tra kết nối Cloud
  if (ArduinoCloud.connected()) {
    Serial.println("Arduino Cloud OK!");
  } else {
    Serial.println("Mất kết nối với Arduino Cloud!");
  }

  delay(5000); // Chờ 5 giây trước khi kiểm tra lại
  if (heart_rate > 100) {
    sendNotification("Your heart rate is high! Please take a rest.");
  }

}

void readTemperature() {
  int rawValue = analogRead(TEMP_SENSOR);
  float voltage = rawValue * 3.5 / 1023.0;
  temperature = voltage * 100.0;
  Serial.print("temperature: ");
  Serial.println(temperature);
}

void readHeartRate() {
  pulseSensor.update();  // Must be called frequently!

  float hr = pulseSensor.getHeartRate();
  heart_rate = hr;
  float spo2 = pulseSensor.getSpO2();

  if (hr > 0) {
    heart_rate = hr;  // Update Cloud variable
    Serial.print("💓 Heart Rate: ");
    Serial.print(hr);
    Serial.print(" BPM, SpO2: ");
    Serial.print(spo2);
    Serial.println("%");
  } else {
    Serial.println("⏳ Waiting for valid heart rate...");
  }
}

void readGPS() {
  while (gpsSerial.available() > 0) {
    if (gps.encode(gpsSerial.read())) {
      if (gps.location.isValid()) {
        // Create a new location object and assign it directly
        location = {gps.location.lat(), gps.location.lng()};

        if (gps.altitude.isValid()) {
          length = gps.altitude.meters();
        }
      }
    }
  }
}

void sendEmergencyMessage() {
  Serial.println("Sending emergency SMS...");

  sim800.println("AT+CMGF=1");  // Set SMS mode
  delay(300);

  sim800.println("AT+CMGS=\"+84943364938\"");  // Replace with your phone number
  delay(500);

  // Access lat/lon directly from the CloudLocation object
  float lat = location.getValue().lat;
  float lng = location.getValue().lon;

  sim800.println("Emergency!");
  sim800.print("Location: ");
  sim800.print(lat, 6);
  sim800.print(", ");
  sim800.println(lng, 6);
  sim800.print(" Temp: ");
  sim800.print(temperature);
  sim800.println(" C");
  sim800.print("HR: ");
  sim800.print(heart_rate);
  sim800.println(" bpm");

  sim800.write(26);  // CTRL+Z to send SMS
  delay(500);

  Serial.println("Emergency SMS sent");
}

void sendNotification(String msg) {
#define PHONE_NUMBER "+84943364938"  // Replace with the actual phone number
  sim800.println("AT+CMGF=1");
  delay(1000);
  sim800.println("AT+CMGS=\"" + String(PHONE_NUMBER) + "\"");
  delay(1000);
  sim800.println(msg);
  delay(1000);
  sim800.write(26);
}
/*
  Since Temperature is READ_WRITE variable, onTemperatureChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTemperatureChange()  {
  // Add your code here to act upon Temperature change
}

/*
  Since EmergencyButton is READ_WRITE variable, onEmergencyButtonChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onEmergencyButtonChange()  {
  // Add your code here to act upon EmergencyButton change
  if (emergency_button) {
    sendEmergencyMessage();
  }
}

/*
  Since HeartRate is READ_WRITE variable, onHeartRateChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onHeartRateChange()  {
  // Add your code here to act upon HeartRate change
}

/*
  Since Length is READ_WRITE variable, onLengthChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLengthChange()  {
  // Add your code here to act upon Length change
}

/*
  Since Location is READ_WRITE variable, onLocationChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLocationChange()  {
  // Add your code here to act upon Location change
}

Hi, @minhluan
Welcome to the forum.

Have you been able to get each peripheral working separately?
That is do have code JUST for each bit of hardware?

Even before programming for the cloud.

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

yeah. I have run separately and they work

Hi,
Have you checked which combinations of hardware your code faults?
You may have clashes with timer usage between libraries.

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

Hi,
I have plugged out sim and gps but nothing
Luan

should I delete code related to sim and gps too?

Hi,
I have deleted all except pulse sensor and it works. Perhaps I should do step by step to figure out where is conflicted
Thanks Tom
Luan

Good idea..

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

1 Like

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