DFrobot oxygen sensor with OLED screen

Hello everyone,

I am working on a project of connecting DFrobot Oxygen sensor with Arduino Nano and showing the results on OLED screen 128x64.

Link of the sensor: https://wiki.dfrobot.com/Gravity_I2C_Oxygen_Sensor_SKU_SEN0322

The problem is when I upload the code to operate the sensor and the screen, the OLED failed to configure the address, However, when I operate each one separately, it works fine.

The address of OLED screen is 0x3c, and for oxygen sensor is 0x73.

Here is the code I used,

#include "RTClib.h"
#include "DFRobot_OxygenSensor.h"
/**
 * i2c slave Address, The default is ADDRESS_3.
 * ADDRESS_0   0x70  i2c device address.
 * ADDRESS_1   0x71
 * ADDRESS_2   0x72
 * ADDRESS_3   0x73
 */
 
#define Oxygen_IICAddress ADDRESS_3
#define COLLECT_NUMBER  10             // collect number, the collection range is 1-100.
DFRobot_OxygenSensor oxygen;

RTC_DS1307 rtc;
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//************variables*****************//
int page = 0; 

//**************Alarm***************//
int buzzer = 2;
int vibrator = 3;
//*********************************//

void setup () {
  Serial.begin(9600);
  
  if(!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
  }

  while(!oxygen.begin(Oxygen_IICAddress ADDRESS_3)){
    Serial.println("I2c device number error !");
    delay(1000);
  }
  Serial.println("I2c connect success !");

  oled.display();
  oled.clearDisplay();
  oled.setTextSize(1);
  oled.setTextColor(WHITE);
  oled.setCursor(5,18);
  oled.print("Smart Gas Analyzer");
  oled.display();
  delay(3000);

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    delay(10);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    //rtc.adjust(DateTime(2024, 3, 24, 4, 46, 0));
  }


}

void loop () {
    float oxygenData = oxygen.getOxygenData(COLLECT_NUMBER);
    Serial.print(" oxygen concentration is ");
    Serial.print(oxygenData);
    Serial.println(" %vol");
    delay(1000);

  
    DateTime now = rtc.now();
    if (page ==0){
      oled.clearDisplay();
      oled.setTextSize(2);
      oled.setTextColor(WHITE);
      oled.setCursor(8,0);
      oled.print("Time ");
      oled.print(now.hour(), DEC);           
      oled.print(":");
      oled.print(now.minute(), DEC);
      oled.display();
   }
}

Thank you,

What are the values of the pullup resistors on SDA and SCL? The total resistance of each should be in the range of 2K to 5K.

Another potential problem is running out of dynamic memory, as the OLED takes half of it.
To save dynamic memory, use the F macro in print statements, for example replace all lines like this

   Serial.print(" oxygen concentration is ");

with constructions like this

   Serial.print(F(" oxygen concentration is "));

Your sketch as shown does not compile due to the following line:

while(!oxygen.begin(Oxygen_IICAddress ADDRESS_3)){

After changing that line to

while(!oxygen.begin(Oxygen_IICAddress)){

It did compile, with the following memory usage shown

Global variables use 1090 bytes (53%) of dynamic memory, leaving 958 bytes for local variables. Maximum is 2048 bytes.

The Adafruit_SSD1306 library allocates a 1024 byte buffer at runtime. Clearly, your sketch does not have enough RAM left over for this buffer and the stack.

Even after using the F macro in all your print statements, RAM is still very very tight.

Global variables use 944 bytes (46%) of dynamic memory, leaving 1104 bytes for local variables. Maximum is 2048 bytes.

That gets the 1024 byte buffer allocated, with 80 bytes left over for stack. Depending on what the other libraries allocate at runtime, it might run then. Maybe.

Since the sketch is only printing text, using a text only library for the SSD1306 that doesn't allocate the 1K screen buffer would be one approach to getting things running.

Thank you for your help, @jremington @van_der_decken :innocent:

You are right the problem due to running out of dynamic memory. I used the Text only library for SSD 1306 OLED and it works fine, however, the percentage of dynamic memory usage is still high (45%).

You are unlikely to have a memory problem with only 45% usage.

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