How to Use the 0.96 OLED I2C Display with Arduino
OLED (Organic Light-Emitting Diode) displays offer vibrant, high-contrast visuals, making them ideal for various projects ranging from simple data displays to intricate graphical interfaces. With its compact size and easy interfacing capabilities, the 0.96 OLED I2C Display module has become a popular choice among hobbyists, makers, and professionals alike. In this guide, we will walk you through the process of setting up the 0.96 OLED I2C Display with your Arduino board, from installing the necessary libraries to displaying text, drawing shapes, and even showcasing bitmap images.
Whether you’re a beginner exploring the world of Arduino or an experienced enthusiast looking to expand your project possibilities, this guide will provide you with the essential knowledge and skills to leverage the full potential of the 0.96 OLED I2C Display module.
If you’re new to Arduino, why not take a look at our Getting Started with Arduino guides. These guides are designed for beginners to learn the fundamental basics of Arduino programming.
Necessary Equipment:
- Arduino (e.g., Arduino Uno)
- 0.96 OLED I2C Display
- Jumper wires
- Breadboard (optional)
Pin Configuration
Connecting the 0.96 OLED I2C Display Module to an Arduino is fairly simple. The connections are as follows:
- VCC on the OLED to the 5V on the Arduino.
- GND on the OLED to GND on the Arduino.
- SCL (Serial Clock) on the OLED to A5 on the Arduino.
- SDA (Serial Data) on the OLED to A4 on the Arduino.
A really simple way to remember which way round SCL and SDA connect to the Arduino is clock has 5 letters, data has 4, so use pins A4 for data and A5 for clock.
Required Libraries
Before we can begin you will need to install a couple of libraries for the 0.96 OLED I2C Display to function correctly. Just follow these simple steps:
Installing the OLED Display Library
- Open the Arduino IDE.
- Go to “Sketch” -> “Include Library” -> “Manage Libraries…”.
- In the Library Manager, search for the SSD1306 OLED.
- Click on the library and click “Install”.
Installing the Adafruit GFX Library
Similarly, you need to install the Adafruit GFX library. This library is often required for OLED displays as it provides graphics functions that many OLED display libraries rely on.
- Open the Arduino IDE.
- Go to “Sketch” -> “Include Library” -> “Manage Libraries…”.
- In the Library Manager, search for “Adafruit GFX”.
- Click on the library and click “Install”.
Verify Installation
After installation, you can verify that both libraries are installed correctly by checking if they appear under “Sketch” -> “Include Library”. You should see the libraries listed there.
Hello World Print Statement
First, we are going to display a basic print statement, a simple message, “Hello World”. In the world of programming, the hello world program is commonly used to test programming environments, compilers, and development tools. Running a “Hello, World!” program is a quick way to verify that your setup is functioning correctly.
#include <Wire.h> // Include the Wire library for I2C communication
#include <Adafruit_GFX.h> // Include the Adafruit GFX library
#include <Adafruit_SSD1306.h> // Include the specific OLED display library
#define OLED_RESET -1 // Define the reset pin for the OLED display (if applicable, -1 if unused)
Adafruit_SSD1306 display(OLED_RESET);
void setup() {
// Initialize the OLED display with the appropriate I2C address (0x3C for SSD1306)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Clear the display buffer.
display.clearDisplay();
}
void loop() {
// Clear the previous contents of the display buffer.
display.clearDisplay();
// Set text size and color
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
// Print "Hello, World!" text
display.setCursor(10, 10);
display.println("Hello, World!");
// Display the contents of the buffer
display.display();
// Delay before repeating
delay(1000);
}
Scrolling Text
Now we are going to take the original print statement and continuously scroll it across the screen.
#include <Adafruit_GFX.h> // Include the Adafruit GFX library
#include <Adafruit_SSD1306.h> // Include the specific OLED display library
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
char message[] = "Hello, World!";
int x = SCREEN_WIDTH; // Initial x position
int minX; // Minimum x position for scrolling
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setTextWrap(false);
// Calculate minimum x position for scrolling
minX = -12 * strlen(message); // 12 = 6 pixels/character * text size 2
}
void loop() {
display.clearDisplay();
display.setCursor(x, 20);
display.print(message);
display.display();
// Update x position for scrolling
x -= 8; // Scroll speed, make more positive to slow down the scroll
// Reset x position if it goes beyond minX
if (x < minX) {
x = SCREEN_WIDTH;
}
delay(100); // Adjust delay for scrolling speed
}
Shapes
In this section, we’ll explore the exciting world of drawing shapes on your OLED display. The Adafruit_GFX library provides a set of functions to easily create various shapes such as lines, rectangles, circles, and triangles. These shapes can be used to add visual elements to your projects, create user interfaces, or display graphical data.
//Include necessary libraries #include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #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); if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for (;;); } display.clearDisplay(); display.setTextSize(1); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text } void loop() { display.clearDisplay(); // Draw a rectangle display.drawRect(10, 10, 30, 20, SSD1306_WHITE); display.display(); delay(1000); // Pause for 1 second display.clearDisplay(); // Draw a filled rectangle display.fillRect(50, 10, 30, 20, SSD1306_WHITE); display.display(); delay(1000); // Pause for 1 second display.clearDisplay(); // Draw a circle display.drawCircle(30, 40, 15, SSD1306_WHITE); display.display(); delay(1000); // Pause for 1 second display.clearDisplay(); // Draw a filled circle display.fillCircle(80, 40, 15, SSD1306_WHITE); display.display(); delay(1000); // Pause for 1 second display.clearDisplay(); // Draw a triangle display.drawTriangle(10, 40, 40, 10, 70, 40, SSD1306_WHITE); display.display(); delay(1000); // Pause for 1 second display.clearDisplay(); // Draw a filled triangle display.fillTriangle(10, 40, 40, 10, 70, 40, SSD1306_WHITE); display.display(); delay(1000); // Pause for 1 second }
Bitmap Images
Bitmap images, also known as raster graphics, are digital images composed of individual pixels arranged in a grid. Each pixel contains color information, allowing bitmap images to represent complex graphics, photographs, and artwork. In this section, we’ll explore how to display bitmap images on your OLED display using Arduino and the Adafruit_GFX library.
What is a Bitmap Image?
A bitmap image consists of a grid of pixels, where each pixel’s color value is stored in memory. The size of the bitmap corresponds directly to the number of pixels in the image, with each pixel containing information about its color and position. Bitmap images can be black and white (1-bit), grayscale (8-bit), or full color (24-bit), depending on the color depth of the image.
How to Display a Bitmap Image
To display bitmap images on your OLED display, you’ll first need to convert the image into a format compatible with your display and Arduino. Bitmap images are commonly stored as arrays of pixel data, where each byte represents the color value of a pixel. Using the Adafruit_GFX library, you can then draw these pixel data onto your OLED display to recreate the original image.
Upload the code below to your Arduino to see a picture of my old dog, Scrappy, or replace the bitmap image inside the PROGMEM
array
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
Adafruit_SSD1306 display(128, 64, &Wire); const unsigned char myDog[] PROGMEM = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x3C,0x03,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00,0x00,0x00, 0x60,0x00,0x00,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x05,0x00,0x00,0x00,0x18,0x00,0x02,0x04,0x47,0x20,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x03,0xFF,0xB8,0x84,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x03,0xFE,0x45,0xFD,0x80, 0xC0,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x83,0xFF,0xFE,0x02,0x80, 0xD3,0x08,0x00,0x06,0x00,0x00,0x08,0x00,0x03,0xFF,0xFF,0xFE,0x00, 0x82,0x38,0x00,0x07,0x00,0x00,0x08,0x00,0x03,0xBF,0xFF,0xF9,0xFE, 0x87,0xC0,0x00,0x07,0x00,0x10,0x0C,0x00,0x03,0xFF,0xFF,0xFF,0xFE, 0x9F,0xA0,0x00,0x07,0x00,0x00,0x1C,0x00,0x01,0xFF,0xFF,0xFF,0xFE, 0xF6,0x00,0x00,0x07,0x00,0x00,0x0E,0x00,0x01,0xFF,0xFF,0xFF,0xFE, 0xF8,0x00,0x00,0x07,0x00,0x00,0x0E,0x00,0x03,0xFF,0xFF,0xFF,0xFE, 0x70,0x00,0x00,0x07,0x00,0x00,0x0E,0x00,0x03,0xFF,0xFF,0xFF,0xFE, 0x80,0x00,0x00,0x07,0x00,0x00,0x0E,0x00,0x03,0xFF,0xFF,0xFF,0xF4, 0x00,0x00,0x00,0x07,0x80,0x00,0x0C,0x00,0x07,0xFF,0xFF,0xFF,0xFE, 0x00,0x00,0x00,0x07,0x80,0x00,0x1E,0x00,0x1F,0xFF,0xFF,0xFF,0xBE, 0x00,0x00,0x00,0x03,0xC0,0x00,0x3F,0x00,0x0F,0xFF,0xFF,0xFF,0xFE, 0x00,0x00,0x00,0x03,0xE0,0x00,0x7F,0x80,0x1F,0xBF,0xFF,0xFF,0xFE, 0x04,0x00,0x00,0x07,0xF0,0x00,0xFF,0xF0,0x3F,0xFF,0xFF,0xFF,0xFE, 0x00,0x00,0x00,0x03,0xF8,0x01,0xFF,0xF8,0x3F,0xFF,0xFF,0xFF,0xFE, 0x00,0x00,0x00,0x07,0xF8,0x01,0xFF,0x9E,0x7F,0xFF,0xDF,0xBF,0xFE, 0x00,0x00,0x00,0x03,0xFE,0x03,0xC0,0x07,0x7F,0xFF,0xFE,0xFF,0xFE, 0x00,0x00,0x00,0x03,0xE3,0x01,0x80,0x03,0xBF,0xFF,0xFB,0xFF,0xFE, 0x00,0x00,0x00,0x01,0xE3,0x01,0x00,0x03,0xBF,0xFF,0xF7,0xFF,0xFE, 0x00,0x00,0x00,0x00,0xF3,0x01,0x00,0x03,0xBF,0xFF,0xDF,0xFF,0xFE, 0x02,0x00,0x00,0x00,0x79,0x00,0x00,0x03,0xBF,0xFF,0xBF,0xFF,0xFE, 0x00,0x00,0x00,0x00,0x7E,0x00,0x00,0x03,0xBF,0xFE,0x7F,0xFF,0xFE, 0x00,0x00,0x00,0x00,0x7C,0xC0,0x00,0x07,0x3F,0xFD,0xFF,0xFF,0x8E, 0x00,0x00,0x20,0x00,0x31,0xC0,0x00,0x0F,0x7F,0xFB,0xFF,0xFF,0xDA, 0x00,0x00,0x08,0x00,0x33,0xE0,0x00,0x1F,0xFF,0xF7,0xFF,0xFF,0xEC, 0x00,0x00,0x00,0x00,0x3F,0xE0,0x01,0x0F,0xFF,0xDF,0xFF,0xFF,0x9C, 0x00,0x00,0x06,0x02,0x1F,0xE0,0x0F,0xFF,0xFF,0xBF,0xFF,0xFF,0xF0, 0x00,0x00,0x0D,0x80,0x0F,0xF3,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE0, 0x00,0x10,0x07,0xC2,0x0F,0xF3,0xFF,0xFF,0xFB,0xFF,0xFF,0xFF,0xC2, 0x00,0x10,0x07,0xE3,0x07,0xFF,0xFF,0xFF,0xEF,0xFF,0xFF,0xFF,0x86, 0x00,0x31,0x07,0xE3,0x87,0xFF,0xFF,0xFF,0x9F,0xFF,0xFF,0xFF,0x06, 0x00,0x42,0x87,0xE3,0xC7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x0E, 0x00,0x41,0x87,0xF3,0xE3,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF8,0x1E, 0x00,0xC7,0x03,0xF3,0xE3,0xFF,0xFF,0xDF,0xFF,0xFF,0xFF,0xE0,0x3E, 0x01,0x8F,0x03,0xF3,0xE3,0xFF,0xFF,0xBF,0xFF,0xFF,0xFF,0xA0,0x22, 0x00,0x0E,0x03,0xF1,0xE1,0xCF,0xFF,0x7F,0xFF,0xFF,0xFC,0x01,0x80 }; void setup() { //Start Serial for debugging Serial.begin(9600); //Initialize the display if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64 Serial.println(F("SSD1306 allocation failed")); } display.display(); //display Adafruit boot screen delay(2000); } void loop() { display.clearDisplay(); // Clear display buffer display.drawBitmap(0, 0, myDog, 103, 64, WHITE); display.display(); }
Other Useful Commands
- Draw Pixels:
display.drawPixel(x,y, color);
- Scroll Right:
display.startscrollright(start, stop);
- Scroll Left:
display.startscrollleft(start, stop);
- Start Diagonal Scroll from Top-right to Bottom-left:
display.startscrolldiagleft(start, stop);
- Start Diagonal Scroll from Top-left to Bottom-right:
display.startscrolldiagright(start, stop);
- Stop Scroll:
display.stopscroll();
- Invert Display Color:
display.invertDisplay(true);
OLED Vs LCD Displays
OLED (Organic Light Emitting Diode) displays, and LCD (Liquid Crystal Display) displays are both popular choices for displaying information in Arduino projects. An OLED display with Arduino has some advantages over a traditional LCD display, making it a preferred choice for many applications.
Here are some reasons why you might consider using a mini-OLED display with Arduino:
Better Contrast and Brightness:
They emit their own light, so each pixel can be individually controlled for both color and brightness. This results in high contrast ratios and vibrant colors. LCDs, on the other hand, require a backlight, which can sometimes lead to lower contrast and duller colors.
Faster Response Time:
OLEDs have a much faster response time compared to LCDs. This means they can display moving content, such as animations or fast-changing data, more smoothly without ghosting or blurring.
Wide Viewing Angle:
Typically have a wider viewing angle compared to LCDs. This makes them easier to read from different angles without distortion or color shift.
Lower Power Consumption:
They are power-efficient because they only consume energy for the pixels that are turned on. In contrast, LCDs require a constant backlight, which consumes more power, especially in applications that require the display to be on for extended periods.
Thinner and Lighter:
Generally thinner and lighter than LCDs, which can be advantageous in projects where size and weight are critical.
Higher Resolution:
OLED displays often have a higher pixel density and resolution compared to similarly sized LCDs, providing finer detail in your graphics or text.
Simplified Wiring:
They usually require fewer connections to an Arduino compared to character LCDs or graphical LCDs, which simplifies wiring and makes the overall setup cleaner.
Great for Small Screens:
OLED displays are ideal for projects that require compact and small screens, such as wearables, IoT devices, or small gadgets.
Disadvantages of the OLED Display
However, it’s worth noting that OLED displays may have some drawbacks as well, including the potential for screen burn-in (though this is less of an issue with modern displays), and higher costs compared to LCDs. Additionally, OLEDs may not be as sunlight readable as certain types of reflective LCDs.
Conclusion
OLED displays offer several advantages over traditional LCD displays, including better contrast, brightness, response time, and power efficiency. The choice between OLED and LCD depends on your specific project requirements, but for many makers and hobbyists, 0.96 OLED I2C Display are an attractive option, especially for small-scale applications where visual quality and power efficiency are important.
Congratulations on completing this guide to using the 0.96 OLED I2C display with Arduino! Throughout this guide, you’ve learned how to set up and interface with the OLED display, display text, draw shapes, and even showcase bitmap images. Let’s recap what you’ve accomplished and look ahead to future possibilities.
Recap:
-
Getting Started: You started by setting up your Arduino IDE and connecting the 0.96 OLED I2C Display to your Arduino board using I2C communication.
-
Displaying Text: You learned how to display text on the OLED screen using the Adafruit_GFX library, including customizing text size, color, and position.
-
Drawing Shapes: You explored drawing basic shapes such as pixels, lines, rectangles, circles, and triangles on the OLED display to add visual elements to your projects.
-
Working with Bitmap Images: You delved into displaying bitmap images on the OLED screen, converting image data into arrays of pixel data, and drawing custom graphics and logos.
Looking Ahead:
Now that you’ve mastered the basics of using the 0.96 OLED I2C display with Arduino, there are endless possibilities for further exploration and experimentation. Here are some ideas to inspire your next projects:
-
Interactive Interfaces: Create interactive user interfaces by combining text, shapes, and bitmap images to design menus, buttons, and status displays for your Arduino projects.
-
Sensor Readouts: Integrate sensor data with the OLED display to visualize real-time measurements, such as temperature, humidity, light intensity, and more.
-
Custom Graphics: Design and display custom graphics, icons, logos, and animations to personalize your projects and make them stand out.
-
Notifications and Alarms: Use the OLED display to provide visual feedback, notifications, and alarms for events and conditions detected by your Arduino sensors and modules.
Experiment and Innovate:
As you continue your journey with Arduino and OLED displays, don’t be afraid to experiment, innovate, and push the boundaries of what’s possible. Whether you’re building prototypes, creating art installations, or developing practical solutions, the 0.96 OLED I2C Display offers a versatile and powerful platform for bringing your ideas to life.
Thank you for exploring this guide, and best of luck with your future projects!
Discover the endless possibilities for Arduino projects with more of our Sensors and Modules guides.