OVERVIEW
I’ve used OLED displays in many tutorials in the past, but I only connected a single one.
For most projects that’s ok, but sometimes you might want to use and connect more than one.
In this quick tutorial we will see how to connect and create the Arduino code to be able to address two SPI OLED display separately.
I will make other tutorials later on how to connect and use more than one “I2C” OLED display as well.
PARTS USED
OLED Display 1.5"
Arduino MEGA R3
These are Amazon affiliate links...
They don't cost you anything and it helps me keep the lights on
if you buy something on Amazon. Thank you!
QUICK OVERVIEW HOW IT WORKS
The Serial Peripheral Interface (SPI) includes a Cable Select (CS) also called Slave Select (SS) pin that lets the micro controller, in our case the Arduino, select the one you want to talk to when you have more than one SPI device connected.
We will be using the 1.5inc OLED display we used in the last tutorial, but of course we will have two of them instead of just one.
We will also be using the U8g2 library to communicate with both of them.
The U8g2 library requires separate Cable Select (CS) pins for each of the displays, but you also need separate RESET pins.
The other pins are shared between each displays.
So for two displays we need a total of seven pins on the Arduino, two more than when using only one OLED display.
CONNECTIONS
As you can see from the connection diagram all the SPI pins are shared between both OLED Displays except the CS and RESET pins.
We are using the hardware SPI pins of the Arduino MEGA which are:
Pin 51 = MOSI
Pin 52 = SCK
When looking at the back legend of the OLED we see that the corresponding pins are:
CLK pin of the OLED is equal to SCK
and the DIN pin is equal to MOSI.
Here are the connections for the first OLED:
VCC is connected to 5V of the Arduino
GND is connected GND of the Arduino
CLK is connected to Pin 52
DIN is connected to Pin 51
CS is connected to Digital Pin 10
DC is connected to Digital Pin 9
and RST is connected to Digital Pin 8
Here are the connections for the second OLED:
VCC is connected to 5V of the Arduino
GND is connected GND of the Arduino
CLK is connected to Pin 52
DIN is connected to Pin 51
CS is connected to Digital Pin 11
DC is connected to Digital Pin 9
and RST is connected to Digital Pin 7
The two tact switches are using INPUT_PULLUP and are connected:
Tact switch 1 for OLED 1 is connected to pin 4 of the Arduino Mega
Tact switch 2 for OLED 2 is connected to pin 5
THE CODE
To use more than one OLED Display with the U8g2 library you create separate instances with different pins for the CS and RESET.
U8G2_SSD1327_MIDAS_128X128_F_4W_HW_SPI OLED1(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8); U8G2_SSD1327_MIDAS_128X128_F_4W_HW_SPI OLED2(U8G2_R0, /* cs=*/ 11, /* dc=*/ 9, /* reset=*/ 7);
So when we want to display stuff on the first OLED we use the instance named OLED1 and we use OLED2 for the second one.
And as always, don’t forget to watch the tutorial video for more information.
/* Multiple SSD1327 1.5 Inch Oled with Arduino Mega
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!
*/
#include <U8g2lib.h> // U8g2 Library for Oled https://github.com/olikraus/u8g2
#define switch_pin1 4 // Tact Switch for 1st OLED connected to pin 8
#define switch_pin2 5 // Tact Switch for 2nd OLED connected to pin 9
int counter1=0; // Score counter for Player 1
int counter2=0; // Score counter for Player 2
int center1=47; // Used to Center value for counter display on OLED 1
int center2=47; // Used to Center value for counter display on OLED 2
// OLED 1 init
U8G2_SSD1327_MIDAS_128X128_F_4W_HW_SPI OLED1(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
// OLED 2 init
U8G2_SSD1327_MIDAS_128X128_F_4W_HW_SPI OLED2(U8G2_R0, /* cs=*/ 11, /* dc=*/ 9, /* reset=*/ 7);
void setup(void) {
pinMode(switch_pin1, INPUT_PULLUP); // using Input_Pullup resistor of the Arduino
pinMode(switch_pin2, INPUT_PULLUP); // using Input_Pullup resistor of the Arduino
OLED1.begin(); // Start OLED 1
OLED1.setContrast(200); // Brightness setting from 0 to 255
OLED2.begin(); // Start OLED 2
OLED2.setContrast(50); // Brightness setting from 0 to 255
// Choose small font for Player 1 text display
OLED1.setFont(u8g2_font_fub17_tr);
OLED1.clearBuffer(); // Clear OLED 1
OLED1.setCursor(10, 32);
OLED1.print("PLAYER 1");
OLED1.drawRFrame(0, 7, 128, 32, 7); // Draw frame around Player 1 text
OLED1.setFont(u8g2_font_fub42_tr); // Choose bigger font for score display
OLED1.setCursor(center1, 117);
OLED1.print(counter1); // Display counter 1 value
OLED1.drawRFrame(0, 65, 128, 63, 7);
OLED1.sendBuffer(); // Send to OLED 1
// Same for OLED 2
OLED2.setFont(u8g2_font_fub17_tr);
OLED2.clearBuffer();
OLED2.setCursor(10, 32);
OLED2.print("PLAYER 2");
OLED2.drawRFrame(0, 7, 128, 32, 7);
OLED2.setFont(u8g2_font_fub42_tr);
OLED2.setCursor(center2, 117);
OLED2.print(counter2);
OLED2.drawRFrame(0, 65, 128, 63, 7);
OLED2.sendBuffer();
}
void loop() {
// If tact switch 1 is clicked
if (digitalRead(switch_pin1) == LOW) {
counter1++; // increase counter
switch (counter1) {
case 0 ... 9: // if counter < 10
center1=47; // used to center score in OLED display
break;
case 10 ... 99: // if counter between 10 and 99
center1=32;
break;
case 100 ... 999: // if counter > 100
center1=12;
break;
}
OLED1.clearBuffer();
OLED1.setFont(u8g2_font_fub17_tr);
OLED1.setCursor(10, 32);
OLED1.print("PLAYER 1");
OLED1.drawRFrame(0, 7, 128, 32, 7);
OLED1.setFont(u8g2_font_fub42_tr);
OLED1.setCursor(center1, 117);
OLED1.print(counter1);
OLED1.drawRFrame(0, 65, 128, 63, 7);
OLED1.sendBuffer();
delay(10);
}
if (digitalRead(switch_pin2) == LOW) {
counter2++;
switch (counter2) {
case 0 ... 9:
center2=47;
break;
case 10 ... 99:
center2=32;
break;
case 100 ... 999:
center2=12;
break;
}
OLED2.clearBuffer();
OLED2.setCursor(10, 32);
OLED2.setFont(u8g2_font_fub17_tr);
OLED2.setFontMode(0);
OLED2.setDrawColor(1);
OLED2.print("PLAYER 2");
OLED2.drawRFrame(0, 7, 128, 32, 7);
OLED2.setFont(u8g2_font_fub42_tr);
OLED2.setCursor(center2, 117);
OLED2.print(counter2);
OLED2.drawRFrame(0, 65, 128, 63, 7);
OLED2.sendBuffer();
delay(10);
}
}
CONCLUSION
As you can see it’s pretty easy to connect and use more than one OLED Display in projects.
I will be using two of these OLED in a future project to display Player scores and information.
Hope to see you then!
TUTORIAL VIDEO
DOWNLOAD
Copy and Paste the above code/sketch in your Arduino IDE software.
Link to the U8g2 library here: