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,