The OLED display has become a popular choice for integration into Arduino projects, mainly because it is compact, has low power consumption, and allows for exceptional visibility even in brightly lit environments. Furthermore, thanks to its easy connection, it is within the reach of any electronics enthusiast, regardless of their experience. In this article, we will take an in-depth look at how to connect and program a 0.96-inch OLED display with Arduino, detailing both the technical aspects and offering practical code examples.
If you've never worked with an OLED display, there are a few key points you should know before you begin your project. OLEDs (Organic Light Emitting Diodes) have fundamental differences from other types of displays such as LCDs. For example, OLED doesn't require a backlight, which significantly reduces power consumption. For displays as small as the 0.96-inch one, this can be essential if your project is battery-powered. Now, let's dive deeper into its features.
What is an OLED screen?
An OLED display is a type of display that uses an organic compound that emits light when an electric current is applied. This makes them ideal for many electronics projects, as their technology allows each pixel to light up on its own, which also improves visibility outdoors. Most OLED displays sold for Arduino have an SSD1306 controller, which can manage the sending of signals to the display. In fact, the SSD1306 is one of the most common in Arduino projects, and we will see it in the examples below.
One of the main advantages of OLED displays is their low power consumption. On average, a small 0.96″ display can consume around 20mA. Why is this important? Well, if you’re using a battery to power your Arduino project, reducing power consumption is always a significant plus. Plus, its 128x64 pixel resolution can render images with pretty good sharpness considering its size.
On the other hand, one of the problems that could arise with this type of screen is that its size is really small. Although they offer good visibility, in some projects where a lot of information needs to be displayed, this size may not be enough.
Connecting the OLED display to Arduino
The OLED display is easily connected to the Arduino board using an I2C or SPI bus, depending on the model. For this tutorial, we will focus on the I2C connection, as it is one of the most common and simple.
You need to connect the pins of the OLED display to the corresponding pins on your Arduino as follows:
- GND (Ground) with the Arduino GND pin
- VCC with the Arduino 5V or 3.3V pin
- SDA to Arduino pin A4
- SCL to Arduino pin A5
As you can see, the connection is pretty simple: you only need four wires. Then, whether you use the SPI or I2C bus, the connection process is similar, although the pins vary depending on the type of communication you choose.
Code example for OLED display
To run the OLED display with Arduino, one of the best options is to use the libraries developed by Adafruit. The SSD1306 controller, as we mentioned before, is compatible with the library Adafruit SSD1306, which makes our lives easier when creating graphics and text on the screen.
Below is a basic code that will allow you to display text on an OLED screen with an I2C connection:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
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.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.println(F("Hola, Mundo!"));
display.display();
}
void loop() {}
This code initializes the display, clears the screen, and then writes "Hello, World!" to the OLED screen. You can use various functions from the Adafruit GFX library to create graphics, draw lines, circles, or even display images on the screen.
Other useful examples for OLED display
The example above is just a basic introduction, but you can do a lot more with OLED displays. For example, you can draw different geometric shapes, make animations, or even create small graphics.
One of the coolest features offered by the Adafruit libraries is the ability to draw multiple pixels, which means you can create scrolling animations. An additional example would be scrolling text, which is very useful if you plan to display dynamically changing messages.
Another use for these displays is to show real-time data in interactive projects, such as a temperature or humidity sensor. The display can be updated as new sensor readings are obtained, making any project much more visual.
Common problems when using OLED displays
One of the most common problems when using OLED displays with Arduino is the lack of memory. The Adafruit libraries, although very complete, can consume a considerable amount of memory on the Arduino processor, especially in versions such as the Arduino UnoIf you're running out of space, you can try optimizing your code, removing features you don't need, or even using a more capable board, such as an Arduino Mega.
Another common problem is the initial setup of the I2C connection. If you are not using the correct SDA and SCL pins, the display may not work or show connection errors. Make sure you are using the correct pins for your Arduino model.
Finally, some users also report a blank screen or one that does not respond to any commands. This can be resolved by ensuring that the power supply voltage is correct (3.3V or 5V depending on the display model) and that the cables are properly connected.