Receiving data from 3 sensors

Hello,
I am working with Arduino Uno and Mega for a project assignment. In this study, I aim to get movement data from the MPU6050 sensor, stress data from the GSR sensor, and pulse and SPO2 data from the MAX30102 sensor. All sensors work separately and receive data smoothly. However, when I want to combine 3 sensors at the same time in one code, only the MAX30102 sensor works. Other sensors do not measure or give a not found error. Since Uno's memory is not enough, I use Mega to receive data from 3 sensors simultaneously. I checked all the connections. I compiled the code in different formats, but the problem did not change. Can you help with this issue? RELEVANT CODE IS BELOW
Thanks.

`#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include "MAX30105.h"
#include "spo2_algorithm.h"
#include "heartRate.h"

Adafruit_MPU6050 mpu;
MAX30105 particleSensor;

#define MAX_BRIGHTNESS 255

#if defined(AVR_ATmega328P) || defined(AVR_ATmega168)
uint16_t irBuffer[100];
uint16_t redBuffer[100];
#else
uint32_t irBuffer[100];
uint32_t redBuffer[100];
#endif

int32_t bufferLength;
int32_t spo2;
int8_t validSPO2;
int32_t heartRate;
int8_t validHeartRate;

byte pulseLED = 11;
byte readLED = 13;
const byte RATE_SIZE = 4;
byte rates[RATE_SIZE];
byte rateSpot = 0;
long lastBeat = 0;

float beatsPerMinute;
int beatAvg;

const int gsrPin = A0;

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }

  // MPU6050 Başlatma
  if (!mpu.begin(0x68)) {
    Serial.println("Failed to find MPU6050 chip at address 0x68");
    if (!mpu.begin(0x69)) {
      Serial.println("Failed to find MPU6050 chip at address 0x69");
      while (1) {
        delay(10);
      }
    }
  }
  Serial.println("MPU6050 found!");

  mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
  mpu.setGyroRange(MPU6050_RANGE_250_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
  delay(100);

  pinMode(pulseLED, OUTPUT);
  pinMode(readLED, OUTPUT);

  // MAX30105 Başlatma
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
    Serial.println(F("MAX30105 was not found. Please check wiring/power."));
    while (1);
  }

  Serial.println(F("Attach sensor to finger with rubber band. Press any key to start conversion"));
  while (Serial.available() == 0);
  Serial.read();

  byte ledBrightness = 60;
  byte sampleAverage = 4;
  byte ledMode = 2;
  byte sampleRate = 100;
  int pulseWidth = 411;
  int adcRange = 4096;

  particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange);
  Serial.println("Initializing...");

  Serial.println("Place your index finger on the sensor with steady pressure.");
  particleSensor.setPulseAmplitudeRed(0x0A);
  particleSensor.setPulseAmplitudeGreen(0);

  Serial.println("GSR/EDA sensörü hazır.");
}

void loop() {
  // MPU6050'dan Veri Okuma
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  Serial.print("AccelX:");
  Serial.print(a.acceleration.x);
  Serial.print(",");
  Serial.print("AccelY:");
  Serial.print(a.acceleration.y);
  Serial.print(",");
  Serial.print("AccelZ:");
  Serial.print(a.acceleration.z);
  Serial.print(", ");
  Serial.print("GyroX:");
  Serial.print(g.gyro.x);
  Serial.print(",");
  Serial.print("GyroY:");
  Serial.print(g.gyro.y);
  Serial.print(",");
  Serial.print("GyroZ:");
  Serial.print(g.gyro.z);
  Serial.println("");

  delay(10);

  bufferLength = 100;

  for (byte i = 0; i < bufferLength; i++) {
    while (particleSensor.available() == false)
      particleSensor.check();

    redBuffer[i] = particleSensor.getRed();
    irBuffer[i] = particleSensor.getIR();
    particleSensor.nextSample();

    Serial.print(F("red="));
    Serial.print(redBuffer[i], DEC);
    Serial.print(F(", ir="));
    Serial.println(irBuffer[i], DEC);
  }

  maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);

  for (byte i = 25; i < 100; i++) {
    redBuffer[i - 25] = redBuffer[i];
    irBuffer[i - 25] = irBuffer[i];
  }

  for (byte i = 75; i < 100; i++) {
    while (particleSensor.available() == false)
      particleSensor.check();

    digitalWrite(readLED, !digitalRead(readLED));

    redBuffer[i] = particleSensor.getRed();
    irBuffer[i] = particleSensor.getIR();
    particleSensor.nextSample();

    Serial.print(F("red="));
    Serial.print(redBuffer[i], DEC);
    Serial.print(F(", ir="));
    Serial.print(irBuffer[i], DEC);

    Serial.print(F(", BPM="));
    Serial.print(heartRate, DEC);

    Serial.print(F(", BPMvalid="));
    Serial.print(validHeartRate, DEC);

    Serial.print(F(", SPO2="));
    Serial.print(spo2, DEC);

    Serial.print(F(", SPO2Valid="));
    Serial.println(validSPO2, DEC);
  }

  maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);

  long irValue = particleSensor.getIR();

  if (checkForBeat(irValue) == true) {
    long delta = millis() - lastBeat;
    lastBeat = millis();

    beatsPerMinute = 60 / (delta / 1000.0);

    if (beatsPerMinute < 255 && beatsPerMinute > 20) {
      rates[rateSpot++] = (byte)beatsPerMinute;
      rateSpot %= RATE_SIZE;

      beatAvg = 0;
      for (byte x = 0; x < RATE_SIZE; x++)
        beatAvg += rates[x];
      beatAvg /= RATE_SIZE;
    }
  }

  Serial.print("IR=");
  Serial.print(irValue);
  Serial.print(", BPM=");
  Serial.print(beatsPerMinute);
  Serial.print(", Avg BPM=");
  Serial.print(beatAvg);

  if (irValue < 50000)
    Serial.print(" No finger?");

  Serial.println();

  int gsrValue = analogRead(gsrPin);

  Serial.print("GSR/EDA Değeri: ");
  Serial.println(gsrValue);

  delay(1000);
}

Your topic has been moved. Please do not post in "Uncategorized"; see the sticky topics in https://forum.arduino.cc/c/using-arduino/uncategorized/184.


With the information that you have provided I doubt very much that people will be able to assist. Please provide a schematic/wiring diagram. Please show your code; don't forget to use code tags as described in How to get the best out of this forum.

The GSR sensor should not create a problem here. GSR sensor just gives analog output. I think there is a conflict between the MPU6050 and MAX30102 since both are using I2C communiation. Check your code including the library to see if there is a pin conflict.

I actually ran all the sensors separately. I can measure. But due to the project, I need to receive data at the same time. But even though I check all the links, I cannot reach the result. Anyway, thank you for your support.

Why do you only occasionally use the F macro in Serial.print("some text");?

Here's your same code with it used throughout, does it fit on the Uno now?

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include "MAX30105.h"
#include "spo2_algorithm.h"
#include "heartRate.h"

Adafruit_MPU6050 mpu;
MAX30105 particleSensor;

#define MAX_BRIGHTNESS 255

#if defined(AVR_ATmega328P) || defined(AVR_ATmega168)
uint16_t irBuffer[100];
uint16_t redBuffer[100];
#else
uint32_t irBuffer[100];
uint32_t redBuffer[100];
#endif

int32_t bufferLength;
int32_t spo2;
int8_t validSPO2;
int32_t heartRate;
int8_t validHeartRate;

byte pulseLED = 11;
byte readLED = 13;
const byte RATE_SIZE = 4;
byte rates[RATE_SIZE];
byte rateSpot = 0;
long lastBeat = 0;

float beatsPerMinute;
int beatAvg;

const int gsrPin = A0;

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }

  // MPU6050 Başlatma
  if (!mpu.begin(0x68)) {
    Serial.println(F("Failed to find MPU6050 chip at address 0x68"));
    if (!mpu.begin(0x69)) {
      Serial.println(F("Failed to find MPU6050 chip at address 0x69"));
      while (1) {
        delay(10);
      }
    }
  }
  Serial.println(F("MPU6050 found!"));

  mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
  mpu.setGyroRange(MPU6050_RANGE_250_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
  delay(100);

  pinMode(pulseLED, OUTPUT);
  pinMode(readLED, OUTPUT);

  // MAX30105 Başlatma
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
    Serial.println(F("MAX30105 was not found. Please check wiring/power."));
    while (1);
  }

  Serial.println(F("Attach sensor to finger with rubber band. Press any key to start conversion"));
  while (Serial.available() == 0);
  Serial.read();

  byte ledBrightness = 60;
  byte sampleAverage = 4;
  byte ledMode = 2;
  byte sampleRate = 100;
  int pulseWidth = 411;
  int adcRange = 4096;

  particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange);
  Serial.println(F("Initializing..."));

  Serial.println(F("Place your index finger on the sensor with steady pressure."));
  particleSensor.setPulseAmplitudeRed(0x0A);
  particleSensor.setPulseAmplitudeGreen(0);

  Serial.println(F("GSR/EDA sensörü hazır."));
}

void loop() {
  // MPU6050'dan Veri Okuma
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  Serial.print(F("AccelX:"));
  Serial.print(a.acceleration.x);
  Serial.print(F(","));
  Serial.print(F("AccelY:"));
  Serial.print(a.acceleration.y);
  Serial.print(F(","));
  Serial.print(F("AccelZ:"));
  Serial.print(a.acceleration.z);
  Serial.print(F(", "));
  Serial.print(F("GyroX:"));
  Serial.print(g.gyro.x);
  Serial.print(F(","));
  Serial.print(F("GyroY:"));
  Serial.print(g.gyro.y);
  Serial.print(F(","));
  Serial.print(F("GyroZ:"));
  Serial.print(g.gyro.z);
  Serial.println("");

  delay(10);

  bufferLength = 100;

  for (byte i = 0; i < bufferLength; i++) {
    while (particleSensor.available() == false)
      particleSensor.check();

    redBuffer[i] = particleSensor.getRed();
    irBuffer[i] = particleSensor.getIR();
    particleSensor.nextSample();

    Serial.print(F("red="));
    Serial.print(redBuffer[i], DEC);
    Serial.print(F(", ir="));
    Serial.println(irBuffer[i], DEC);
  }

  maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);

  for (byte i = 25; i < 100; i++) {
    redBuffer[i - 25] = redBuffer[i];
    irBuffer[i - 25] = irBuffer[i];
  }

  for (byte i = 75; i < 100; i++) {
    while (particleSensor.available() == false)
      particleSensor.check();

    digitalWrite(readLED, !digitalRead(readLED));

    redBuffer[i] = particleSensor.getRed();
    irBuffer[i] = particleSensor.getIR();
    particleSensor.nextSample();

    Serial.print(F("red="));
    Serial.print(redBuffer[i], DEC);
    Serial.print(F(", ir="));
    Serial.print(irBuffer[i], DEC);

    Serial.print(F(", BPM="));
    Serial.print(heartRate, DEC);

    Serial.print(F(", BPMvalid="));
    Serial.print(validHeartRate, DEC);

    Serial.print(F(", SPO2="));
    Serial.print(spo2, DEC);

    Serial.print(F(", SPO2Valid="));
    Serial.println(validSPO2, DEC);
  }

  maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);

  long irValue = particleSensor.getIR();

  if (checkForBeat(irValue) == true) {
    long delta = millis() - lastBeat;
    lastBeat = millis();

    beatsPerMinute = 60 / (delta / 1000.0);

    if (beatsPerMinute < 255 && beatsPerMinute > 20) {
      rates[rateSpot++] = (byte)beatsPerMinute;
      rateSpot %= RATE_SIZE;

      beatAvg = 0;
      for (byte x = 0; x < RATE_SIZE; x++)
        beatAvg += rates[x];
      beatAvg /= RATE_SIZE;
    }
  }

  Serial.print(F("IR="));
  Serial.print(irValue);
  Serial.print(F(", BPM="));
  Serial.print(beatsPerMinute);
  Serial.print(F(", Avg BPM="));
  Serial.print(beatAvg);

  if (irValue < 50000)
    Serial.print(F(" No finger?"));

  Serial.println();

  int gsrValue = analogRead(gsrPin);

  Serial.print(F("GSR/EDA Değeri: "));
  Serial.println(gsrValue);

  delay(1000);
}

I will try it and report back. Thanks.

image

Currently, we can receive data from all 3 sensors at the same time, but the bpm value is not stable. If we solve this problem, it will be completed.

A quick glance at your code shows you do NOT have a constant time value for computing BPM. Without that time interval being a constant value, the result will always wander about.

So, how can I set this time interval to a constant value?

Perhaps a real time clock? Or use 1000 millis to give a 1 second time when you can compute the number of beats per minute.

Sure, can you suggest a code block for this?

No. You do know how to use millis to make a timer, don't you? If not, it's time for more learning.

Well, thank you for your interest. I will continue my experiments.

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