Maker.io main logo

Arduino OLED display Interfacing

2024-05-13 | By Jobit Joseph

License: Attribution-NonCommercial-ShareAlike Arduino

Most of us would be familiar with the 16×2 Dot matrix LCD display that is used in most of the projects to display some information to the user. However, these LCD displays have a lot of limitations in what they can do. In this tutorial, we are going to learn about OLED displays and how to use them with Arduino. There are many types of OLED displays available in the market and there are lots of ways to get them working. In this tutorial, we will discuss its classifications and also which will be best suited for your project.

I2C OLED Module Pinout

This OLED module leverages I2C for communication with the microcontroller. You can also find similar displays with either I2C or SPI interface or even with both in the same PCB. The module we are using has 4 pins in total. The pinout of an I2C OLED Display Module is shown below-

 I2C oled Module Pinout

GND Ground connection for the module. Connect to the GND pin of the Arduino.VCC Provides power for the module. Connect to the 5V pin of the Arduino.SCL Serial Clock pin. Used for providing clock pulse for I2C Communication.SDA Serial Data pin. Used for transferring Data through I2C communication.

I2C OLED Module Parts

The below image shows the components on the I2C OLED display module PCB.

i2C OLED Module Parts Marking

The XC6206P332 Voltage regulator steps down the input voltage to 3.3V. The inclusion of this voltage regulator allows us to interface the OLED module to even 5.5V microcontrollers or circuits. We can also set the OLED module I2C address by changing the position of the address select resistor. The I2C default address is 0x78 (0x3C in 7-bit) and can be changed to 0x7A (0x3D in 7-bit).

OLED Module Interfacing Connection Diagram

The following image shows how to connect an I2C OLED module with the Arduino board.

Circuit Diagram

The connections are very simple, connect the GND pin to the GND pin of the Arduino and VCC to the 5V pin. The SCL is connected to the A5 and the SDA is connected to the A4 pin of the Arduino.

Arduino Code with Basic Text and Graphics Functions

Once the connections are ready you can start programming the Arduino. Whether we use I2C or SPI interface for the OLED display, the code is almost the same. Let's start with installing the necessary libraries. For this tutorial, we will need to install two Arduino libraries, Adafruit SSD1306 and Adafruit GFX libraries. The easiest to way to install them is to use the Arduino IDE library manager, just search for these libraries and install them. Once the libraries are successfully installed, copy and paste the following code to the Arduino IDE. Make the changes depending on whether you are using I2C or SPI interface and then compile and upload it to the Arduino. For more detailed explanation Please checkout Interfacing OLED Display with Arduino.

Copy Code
#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 SSD1306 display connected using I2C

#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)

#define SCREEN_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Declaration for SSD1306 display connected using software SPI:

//#define OLED_MOSI 9

//#define OLED_CLK 10

//#define OLED_DC 11

//#define OLED_CS 12

//#define OLED_RESET 13

//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
void setup()

{

Serial.begin(9600);

// initialize the OLED object

if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {

Serial.println(F("SSD1306 allocation failed"));

for(;;); // Don't proceed, loop forever

}
// Uncomment this if you are using SPI

//if(!display.begin(SSD1306_SWITCHCAPVCC)) {

// Serial.println(F("SSD1306 allocation failed"));

// for(;;); // Don't proceed, loop forever

//}
// Clear the buffer.
display.clearDisplay();

// Display Text

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(0, 28);

display.println("Hello world!");

display.display();

delay(2000);

display.clearDisplay();
// Display Inverted Text

display.setTextColor(BLACK, WHITE); // 'inverted' text

display.setCursor(0, 28);

display.println("Hello world!");

display.display();

delay(2000);

display.clearDisplay();
// Changing Font Size

display.setTextColor(WHITE);

display.setCursor(0, 24);

display.setTextSize(2);

display.println("Hello!");

display.display();

delay(2000);

display.clearDisplay();) {

}
// Display Numbers

display.setTextSize(1);

display.setCursor(0, 28);

display.println(123456789);

display.display();

delay(2000);

display.clearDisplay();
// Specifying Base For Numbers

display.setCursor(0, 28);

display.print("0x"); display.print(0xFF, HEX);

display.print("(HEX) = ");

display.print(0xFF, DEC);

display.println("(DEC)");

display.display();

delay(2000);

display.clearDisplay();
// Display ASCII Characters

display.setCursor(0, 24);

display.setTextSize(2);

display.write(1);

display.display();

delay(2000);

display.clearDisplay();
// Scroll full screen

display.setCursor(0, 0);

display.setTextSize(1);

display.println("Full");

display.println("screen");

display.println("scrolling!");

display.display();

display.startscrollright(0x00, 0x07);

delay(4500);

display.stopscroll();

delay(1000);

display.startscrollleft(0x00, 0x07);

delay(4500);

display.stopscroll();

delay(1000);

display.startscrolldiagright(0x00, 0x07);

delay(4500);

display.startscrolldiagleft(0x00, 0x07);

delay(4500);

display.stopscroll();

display.clearDisplay();
//draw rectangle

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(0, 0);

display.println("Rectangle");

display.drawRect(0, 15, 60, 40, WHITE);

display.display();

delay(2000);

display.clearDisplay();
//draw filled rectangle

display.setCursor(0, 0);

display.println("Filled Rectangle");

display.fillRect(0, 15, 60, 40, WHITE);

display.display();

delay(2000);

display.clearDisplay();
//draw rectangle with rounded corners

display.setCursor(0, 0);

display.println("Round Rectangle");

display.drawRoundRect(0, 15, 60, 40, 8, WHITE);

display.display();

delay(2000);

display.clearDisplay();
//draw circle

display.setCursor(0, 0);

display.println("Circle");

display.drawCircle(20, 35, 20, WHITE);

display.display();

delay(2000);

display.clearDisplay();
//draw filled circle

display.setCursor(0, 0);

display.println("Filled Circle");

display.fillCircle(20, 35, 20, WHITE);

display.display();

delay(2000);

display.clearDisplay();
//draw triangle

display.setCursor(0, 0);

display.println("Triangle");

display.drawTriangle(30, 15, 0, 60, 60, 60, WHITE);

display.display();

delay(2000);

display.clearDisplay();
//draw filled triangle

display.setCursor(0, 0);

display.println("Filled Triangle");

display.fillTriangle(30, 15, 0, 60, 60, 60, WHITE);

display.display();

delay(2000);

display.clearDisplay();

}
#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 SSD1306 display connected using I2C

#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)

#define SCREEN_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Declaration for SSD1306 display connected using software SPI:

//#define OLED_MOSI 9

//#define OLED_CLK 10

//#define OLED_DC 11

//#define OLED_CS 12

//#define OLED_RESET 13

//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
void setup()

{

Serial.begin(9600);

// initialize the OLED object

if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {

Serial.println(F("SSD1306 allocation failed"));

for(;;); // Don't proceed, loop forever

}
// Uncomment this if you are using SPI

//if(!display.begin(SSD1306_SWITCHCAPVCC)) {

// Serial.println(F("SSD1306 allocation failed"));

// for(;;); // Don't proceed, loop forever

//}
// Clear the buffer.
display.clearDisplay();

// Display Text

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(0, 28);

display.println("Hello world!");

display.display();

delay(2000);

display.clearDisplay();
// Display Inverted Text

display.setTextColor(BLACK, WHITE); // 'inverted' text

display.setCursor(0, 28);

display.println("Hello world!");

display.display();

delay(2000);

display.clearDisplay();
// Changing Font Size

display.setTextColor(WHITE);

display.setCursor(0, 24);

display.setTextSize(2);

display.println("Hello!");

display.display();

delay(2000);

display.clearDisplay();) {

}
// Display Numbers

display.setTextSize(1);

display.setCursor(0, 28);

display.println(123456789);

display.display();

delay(2000);

display.clearDisplay();
// Specifying Base For Numbers

display.setCursor(0, 28);

display.print("0x"); display.print(0xFF, HEX);

display.print("(HEX) = ");

display.print(0xFF, DEC);

display.println("(DEC)");

display.display();

delay(2000);

display.clearDisplay();
// Display ASCII Characters

display.setCursor(0, 24);

display.setTextSize(2);

display.write(1);

display.display();

delay(2000);

display.clearDisplay();
// Scroll full screen

display.setCursor(0, 0);

display.setTextSize(1);

display.println("Full");

display.println("screen");

display.println("scrolling!");

display.display();

display.startscrollright(0x00, 0x07);

delay(4500);

display.stopscroll();

delay(1000);

display.startscrollleft(0x00, 0x07);

delay(4500);

display.stopscroll();

delay(1000);

display.startscrolldiagright(0x00, 0x07);

delay(4500);

display.startscrolldiagleft(0x00, 0x07);

delay(4500);

display.stopscroll();

display.clearDisplay();
//draw rectangle

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(0, 0);

display.println("Rectangle");

display.drawRect(0, 15, 60, 40, WHITE);

display.display();

delay(2000);

display.clearDisplay();
//draw filled rectangle

display.setCursor(0, 0);

display.println("Filled Rectangle");

display.fillRect(0, 15, 60, 40, WHITE);

display.display();

delay(2000);

display.clearDisplay();
//draw rectangle with rounded corners

display.setCursor(0, 0);

display.println("Round Rectangle");

display.drawRoundRect(0, 15, 60, 40, 8, WHITE);

display.display();

delay(2000);

display.clearDisplay();
//draw circle

display.setCursor(0, 0);

display.println("Circle");

display.drawCircle(20, 35, 20, WHITE);

display.display();

delay(2000);

display.clearDisplay();
//draw filled circle

display.setCursor(0, 0);

display.println("Filled Circle");

display.fillCircle(20, 35, 20, WHITE);

display.display();

delay(2000);

display.clearDisplay();
//draw triangle

display.setCursor(0, 0);

display.println("Triangle");

display.drawTriangle(30, 15, 0, 60, 60, 60, WHITE);

display.display();

delay(2000);

display.clearDisplay();
//draw filled triangle

display.setCursor(0, 0);

display.println("Filled Triangle");

display.fillTriangle(30, 15, 0, 60, 60, 60, WHITE);

display.display();

delay(2000);

display.clearDisplay();

}

OLED Display Examples

制造商零件编号 A000066
ARDUINO UNO R3 ATMEGA328P BOARD
Arduino
¥190.97
Details
制造商零件编号 333100
DISPLAY OLED I2C BLUE 0.96"
Soldered Electronics
¥146.52
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum