How to Display on I2C OLED with Arduino

In this tutorial, I’ll share with you on how to display on I2C OLED using Arduino. The video will cover on :

  1. Installing library for OLED.
  2. Testing the OLED with example provided.
  3. How to custom an image to display on the OLED.
  4. Display an output on the OLED.

HARDWARE PREPARATION

This tutorial use :

  1. Maker UNO
  2. Temperature Sensor (Celcius)
  3. OLED I2C 
  4. Breadboard 8.5×5.5cm (400 holes)
  5. Acrylic Base Plate For Arduino UNO
  6. Jumper Wires

You can make the hardware connection based on the diagram and table below.

Connection
OLED : VCCMaker UNO: 3.3V
OLED : GNDMaker UNO: GND
OLED : SCLMaker UNO: SCL
OLED : SDAMaker UNO: SDA
LM35 : GNDMaker UNO: GND
LM35 : VOutMaker UNO: Pin A0
LM35 : VCCMaker UNO: 5V

Sample Code

This is the sample code used for this tutorial. Try it!

https://gist.github.com/suadanwar/88cdef31a2fb5a64e130008dfc1f97a3#file-oleddisplay-ino

#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 an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define LM35 A0
float temperature = 0;
float value = 0;
float voltage = 0;
const unsigned char myBitmapMaker [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff,
0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xfe, 0x00, 0x10, 0x07, 0xc1, 0x83, 0xc0, 0x78, 0x00, 0xfc, 0x06, 0x03, 0x80, 0x7f, 0xff,
0xff, 0xfe, 0x00, 0x10, 0x01, 0xc0, 0x83, 0x80, 0x30, 0x00, 0x7c, 0x06, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xfe, 0x00, 0x10, 0x00, 0xc0, 0x83, 0x00, 0x10, 0x00, 0x7c, 0x06, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xfe, 0x00, 0x10, 0x00, 0xc0, 0x83, 0x00, 0x00, 0x00, 0x7c, 0x02, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x00, 0x10, 0x20, 0xc0, 0x83, 0x04, 0x08, 0x00, 0xfc, 0x02, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x04, 0x0f, 0x07, 0xfc, 0x02, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x06, 0x1f, 0x07, 0xfc, 0x00, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x01, 0xff, 0x07, 0xfc, 0x00, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x00, 0xc0, 0x83, 0x00, 0xff, 0x07, 0xfc, 0x00, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x01, 0xc0, 0x83, 0x80, 0x3f, 0x07, 0xfc, 0x00, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x01, 0xc0, 0x83, 0xc0, 0x1f, 0x07, 0xfc, 0x00, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x00, 0xc0, 0x83, 0xe0, 0x1f, 0x07, 0xfc, 0x00, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0xf8, 0x0f, 0x07, 0xfc, 0x00, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x04, 0x0f, 0x07, 0xfc, 0x00, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x06, 0x0f, 0x07, 0xfc, 0x10, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x06, 0x0f, 0x07, 0xfc, 0x10, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x04, 0x0f, 0x07, 0xfc, 0x10, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x03, 0x00, 0x0f, 0x07, 0xfc, 0x10, 0x81, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xe0, 0x07, 0x00, 0x1f, 0x07, 0xfc, 0x10, 0x81, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xf0, 0x0f, 0x80, 0x3f, 0x07, 0xfc, 0x10, 0x81, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xe0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0x04, 0x41, 0x81, 0xfc, 0x0f, 0xe0, 0xc0, 0xe0, 0x70, 0x42, 0x01, 0x00, 0xff, 0xff,
0xff, 0xff, 0x04, 0x40, 0x81, 0xf8, 0x0f, 0xc0, 0x40, 0xc0, 0x70, 0x42, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0x04, 0x40, 0x81, 0xf8, 0x0f, 0xc0, 0x40, 0xc0, 0x30, 0x42, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0x04, 0x40, 0x81, 0xf8, 0x07, 0xc0, 0x40, 0xc0, 0x30, 0x02, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x04, 0xc0, 0x81, 0xf8, 0x07, 0xc0, 0x40, 0xc0, 0x30, 0x06, 0x0f, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x05, 0xc0, 0x01, 0xf8, 0x07, 0xc0, 0x00, 0xc0, 0x30, 0x06, 0x0f, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf8, 0x07, 0xc0, 0x00, 0xc0, 0x30, 0x06, 0x03, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf0, 0x07, 0xc0, 0x00, 0xc0, 0x30, 0x0e, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf0, 0x07, 0xc0, 0x00, 0xc0, 0x30, 0x0e, 0x01, 0x00, 0xff, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf0, 0x87, 0xc0, 0x00, 0x80, 0x10, 0x06, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf0, 0x83, 0xc0, 0x00, 0x80, 0x10, 0x06, 0x0f, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf0, 0x03, 0xc0, 0x00, 0x80, 0x10, 0x06, 0x0f, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf0, 0x03, 0xc0, 0x10, 0x80, 0x10, 0x02, 0x0f, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x21, 0xf0, 0x03, 0xc0, 0x10, 0x80, 0x10, 0x02, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc2, 0x21, 0xf0, 0xc3, 0xc0, 0x10, 0x82, 0x10, 0x42, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc2, 0x21, 0xe0, 0xc3, 0xc1, 0x10, 0x86, 0x00, 0x42, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc2, 0x21, 0xf0, 0xc3, 0xe3, 0x10, 0x86, 0x10, 0x40, 0x01, 0x0c, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff,
0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
void setup() {
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
// Draw the bitmap:
// drawBitmap(x position, y position, bitmap data, bitmap width, bitmap height, color)
display.drawBitmap(0, 0, myBitmapMaker, 128, 64, WHITE);
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
pinMode(LM35, INPUT);
}
void loop() {
displayTemp();
}
void displayTemp() {
value = analogRead(A0); // sensor output to arduino analog A0 pin
voltage = value * 0.00488;
temperature = voltage * 100;
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 10);
display.print("Room Temperature");
display.setCursor(15, 30);
display.setTextSize(3);
display.print(temperature);
display.print("C");
display.display();
delay(1000);
}
view raw OLEDdisplay.ino hosted with ❤ by GitHub

References :

  1. OLED Arduino by Dronebot Workshop
  2. OLED I2C Display by DIY Factory
  3. Bitmap Image

Thank you

Thank you for reading this tutorial and we hope it helps your project development. If you have any technical inquiry, please post at Cytron Technical Forum.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Smart Door Announcer with Raspberry Pi
Why Raspberry Pi is the future of computing devices?
Face Recognition Door Lock System Using OpenCV on Raspberry Pi
Send Data to Firebase Using Arduino Uno WiFi Rev2
Simple Alarm System Using Maker Nano
Scroll to Top