How to Display Text on OLED with Arduino: Simple Guide
To display text on an OLED with Arduino, use the
Adafruit_SSD1306 library to control the screen. Initialize the display, clear it, set the text size and color, then use display.print() to show your text and display.display() to update the screen.Syntax
Here is the basic syntax to display text on an OLED screen using the Adafruit_SSD1306 library:
display.clearDisplay();clears the screen.display.setTextSize(size);sets the size of the text.display.setTextColor(color);sets the text color (usuallySSD1306_WHITE).display.setCursor(x, y);sets the position where text starts.display.print("text");writes the text to the buffer.display.display();sends the buffer to the screen to show the text.
arduino
display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(0,0); display.print("Hello, OLED!"); display.display();
Example
This example shows how to initialize the OLED display and print "Hello, OLED!" on the screen.
arduino
#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() { if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while(true); // Stop if OLED not found } display.clearDisplay(); display.setTextSize(2); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 10); display.print("Hello, OLED!"); display.display(); } void loop() { // Nothing here }
Output
The OLED screen shows the text: Hello, OLED! in large white letters on a black background.
Common Pitfalls
Common mistakes when displaying text on OLED with Arduino include:
- Not initializing the display properly with
display.begin(). - Forgetting to call
display.display()after printing text, so nothing shows. - Setting the cursor outside the screen bounds, causing text not to appear.
- Using wrong I2C address (usually 0x3C or 0x3D).
- Not including required libraries
Adafruit_GFXandAdafruit_SSD1306.
arduino
/* Wrong way: Missing display.display() */ display.clearDisplay(); display.setCursor(0,0); display.print("Oops!"); // Missing display.display() so screen stays blank /* Right way: */ display.clearDisplay(); display.setCursor(0,0); display.print("Fixed!"); display.display();
Quick Reference
| Function | Purpose |
|---|---|
| display.begin() | Initialize the OLED display |
| display.clearDisplay() | Clear the screen buffer |
| display.setTextSize(size) | Set text size (1=small, 2=medium, etc.) |
| display.setTextColor(color) | Set text color (usually SSD1306_WHITE) |
| display.setCursor(x, y) | Set text start position |
| display.print(text) | Write text to buffer |
| display.display() | Show buffer content on screen |
Key Takeaways
Always initialize the OLED with display.begin() before use.
Clear the display and set cursor before printing text.
Call display.display() to update the screen after printing.
Use correct I2C address and include required libraries.
Set text size and color to make text visible and readable.