OLED
This project reads values from the serial monitor and displays the readings on a 128x64 I2C OLED. An OLED (Organic Light-Emitting Diode) is an alternative to an LCD. While an LCD restricts you to printing characters, an OLED allows you to manipulate individual pixels and display text and graphics.
Components
Component | Purpose | Comm. Protocol | Current | Voltage |
---|---|---|---|---|
Arduino Nano | The microcontroller | - | 500mA | 5V |
128X64 I2C OLED | The OLED display | I2C | 120mA | 3.3V |
Circuit Diagram

Connections
I2C OLED Pin Connections
Nano Pin | I2C OLED Pin |
---|---|
A4 | SDA |
A5 | SCL |
5V | VCC |
GND | GND |
Code
/*
Project: Physical Computing
Circuit: OLED
Boards Supported: UNO R3/R4, Nano, ESP32
Function: This project will display text on an OLED display.
Author: STEMVentor Educonsulting
This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs. This code is for educational purposes only and is not for production use.
*/
// LIBRARIES
#include <Wire.h> //used for communicating with I2C devices
// Install the Adafruit OLED libraries using the IDE library manager.
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// PIN DEFINTIONS
// CONSTANT DEFINITIONS
// Define the OLED display size
const byte screen_width = 128;// width, in pixels
const byte screen_height = 64;// height, in pixels
const String digits_16 = "1234567890123456";
const String chars_16 = "Lorem ipsum dolo.";
const String digits_20 = "12345678901234567890";
const String chars_20 = "Lorem ipsum dolor si.";
const String digits_32 = "12345678901234567890123456789012";
const String chars_32 = "Lorem ipsum dolor sit amet, con.";
const String long_string = "This is an 80-char string that should print across rows and fill the display.";
// GLOBAL VARIABLES
// INITIALIZE OBJECTS
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// -1 specifically indicates that there is no reset pin used for the display.
// Some OLED displays have a reset pin that can be connected to a GPIO pin on the microcontroller
// to manually reset the display.
Adafruit_SSD1306 oled_display(screen_width, screen_height, &Wire, -1);
// LOCAL FUNCTIONS
// Function to print to display on a single row.
// Takes the row number and the text to display on that row (max 20 chars, rest will be truncated).
// The entire display is cleared if the clear_all flag is true, else only the row is cleared (the default).
void printToOLEDDisplayByRow(int row, String text, bool clear_all = false)
{
const char* twenty_spaces = " "; //40 blank spaces
if(clear_all){
oled_display.clearDisplay(); //clears the entire display
}
oled_display.setCursor(0, (row-1)*8);
oled_display.print(twenty_spaces); //clears the row
oled_display.setCursor(0, (row-1)*10); //cursor has to be set again after printing spaces
oled_display.print(text);
}
// Function to display across the OLED.
void printToOLEDDisplay(String text)
{
oled_display.clearDisplay(); //clears the display
oled_display.setCursor(20,10 ); //sets cursor to these pixels to start from
oled_display.println(text); //sends it to the display
oled_display.display(); //displays it on the display
Serial.println("Data string has been displayed on OLED display");
}
void oledSetup()
{
// Initializing wire
Wire.begin();
if(!oled_display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for most OLED I2C.
Serial.println(F("SSD1306 allocation failed"));
}
oled_display.clearDisplay();
oled_display.setTextSize(1); //sets the text size (can be 0,1,2)
oled_display.setTextColor(WHITE); //sets the text colour
// Use the function to display text on each line indiviudally.
printTodisplayByRow(1, "OLED display Ready!", true);
oled_display.display();
}
/* SETUP CODE: runs once when the board starts up or resets */
void setup() {
// Start the serial communication with baud rate suitable for your components.
Serial.begin(9600);
oledSetup();
Serial.println("The OLED display is ready !");
// Use the functions to display text of various lengths.
printToOLEDDisplay(digits_16);
delay(1000);
printToOLEDDisplay(chars_16);
delay(1000);
}
/* MAIN LOOP: runs repeatedly at a very high frequency (1000s of times a second) */
void loop() {
// All test strings will be display during setup, nothing to do in loop.
}