Problem coding a MAX30100 and an SPI 128*64 Display

Hey! First time ever writing in a forum, i am really in a pickle...
It's my first project with an Arduino UNO and im trying to build a heart rate reader that would then display messages depending on the heart rate values, but first im just trying to program it so it gives the normal heart rate, i have tested both devices on separate examples from libraries and they work , the OLED Display 7 pin SPI , and the MAX30100 Pulse Oximeter I2C , my problem is in the code, i have gotten to this point were i got this code to work whenever i take out line 67 : Pox.update(). Leaving me with a screen stuck on "Initializing..." . But whenever i run the full code which i would expect wo work i get the message "SSD1306 Allocation Failed"
Any clue why this happens ? im very lost.

#include <SPI.h>
#include <Wire.h> // For I2C communication
#include <Adafruit_GFX.h> // Graphics library
#include <Adafruit_SSD1306.h> // OLED display library
#include <CircularBuffer.h>
#include <MAX30100.h>
#include <MAX30100_BeatDetector.h>
#include <MAX30100_Filters.h>
#include <MAX30100_PulseOximeter.h>
#include <MAX30100_Registers.h>
#include <MAX30100_SpO2Calculator.h>





#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_MOSI   SDA
#define OLED_CLK   SCL
#define OLED_DC    9
#define OLED_CS    10
#define OLED_RESET 8

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI,
  OLED_DC, OLED_RESET, OLED_CS);


PulseOximeter pox;
uint32_t tsLastReport = 0;
#define REPORTING_PERIOD_MS     1000

void onBeatDetected() {
  Serial.println("Beat!!!");
}

void setup() {
 
  Serial.begin(115200);

  // Initialize the OLED display
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3d)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println("Initializing...");
  display.display();

  if (!pox.begin()) {
    Serial.println("FAILED");
    for (;;);
  } else {
    Serial.println("SUCCESS");
  }

  pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);

  pox.setOnBeatDetectedCallback(onBeatDetected);
}

void loop() {
  pox.update();
  if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.print("BPM : ");
    display.print(pox.getHeartRate());
    display.setCursor(0, 10);
    display.print("SpO2: ");
    display.print(pox.getSpO2());
    display.print("%");
    display.display();
    

    tsLastReport = millis();
  }
}

The Adafruit_SSD1306 tries to allocate a 1K screen buffer at runtime. You likely don't have that much left over (the Uno R3 only starts with 2K). Check how much dynamic memory the compiler says you have left.

Your options are:

a) reduce the number of #include'd libraries and hope you gain enough free RAM,
b) use another board with more RAM or,
c) use a text only SSD1306 library that doesn't need that extra 1K of RAM.

hey! first of all thanks a lot for your time answering and helping me out, i have reduced the number of libraries to the minimum, dont have access to another board, and tried using the library you kindly addressed me to, yet trying to use its spi 128*64 example i couldnt get it to work with my display, defined pins and checked everything but i dont seem to be able to use this library, dont know if its because my display is a 7 pin one, probably something else, yet all these advices already lowered the ram usage quite a lot from where it was, i will paste the compiler output below in hopes it helps somehow, still getting allocation failed

Sketch uses 20474 bytes (63%) of program storage space. Maximum is 32256 bytes.
Global variables use 833 bytes (40%) of dynamic memory, leaving 1215 bytes for local variables. Maximum is 2048 bytes.

indeed after a lot more time of trying to solve and understand whats wrong, its exactly that , i got the text based lib to work, linked it to the max30100 and got the screen to show initializing but doesnt do anything besides that, but putting things befor the if that begins the max30100, the display works, so essentially after the max begins the arduino has no more ram for the display ... i guess im going to try to find a lighter max30100 lib aswell ._.

Even if you find lighter library you'll be "on the edge" with memory. Use ESP32 it's cheep and RAM is MUCH bigger.

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