Connecting OLED SSD1306 SPI display and DS3231 RTC to Arduino Nano

I am building a clock using Arduino Nano and OLED SSD1306 SPI display and DS3231 RTC.
My question is regarding connectivity.
When I search for the way each component should be conected I see that

SDA should go to A4
and SCL to A5

The problem I have is that the same connectivity is required for both OLED and RTC. So How to resolve it

Declaration of OLED

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

Funny enough there is no reset pin on the OLED ITC and the SCL and SDA are connected to A5 and A4
The RTC declaration looks like this

#include <Wire.h>
#include <DS3231.h>
DS3231 clock;
RTCDateTime dt;

Please advice how the wiring shuld look like so both components can work together connected to Arduino Nano

I2C is a bus, so several devices can share. They need to have individual addresses. This will be detailed in the respective libraries.

if two devices have two different I2C addresses, they can connect to the same I2C pins of Arduino

Hello,

I think you use I2C and not SPI !!

SDA and SCL are I2C signals.

It is very simple :

for OLED you have to determine the DeviceAdresse by a I2C scanner, if you do not know :

#include <Wire.h>
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}

void loop()
{
  byte error, address;
  int nDevices;
  Serial.println("Recherche en cours...");
  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("Equiment I2C trouve a l'addresse 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Erreur inconnue a l'address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("Aucun Equipement I2C trouve\n");
  else
    Serial.println("Fini\n");

  delay(5000);           
}

and then you use your oled device with this adress, here an example with the display I use :

#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // 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 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


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

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

the adress is : 0x3C

For RTC it is strange, you do not need to define adress, but It works perfectly, so the combined code is like :

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// rtrc + oled
// ***********
DS3231 clock;
RTCDateTime dt;

// oled
// ****
#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 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

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

  // Initialize DS3231
  Serial.println("Initialize DS3231");;
  clock.begin();

 // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

 }
1 Like

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