I am supported by our readers and may earn a commission if you buy through our Affiliate Links at no cost to you. Thank you so much for your support. Read More
OLED Displays
Adding a display to your Arduino projects is a great way to show information in real time. You can buy a display for under $10 and they are easy to use. I bought a 128×96 pixel monochrome display for around $6. The screen size is 1.3″ and big enough for small projects where I just need to display some basic information.
Getting Started
When you buy your display, take note of the library needed to use the board. We will be using this information in our sketch. I bought mine in 2/pack and the displays used the SH1106 library.
Install the Library
Open up the Arduino program and navigate to Tools->Manage Libraries.
Enter in the library needed for your particular display board. In my case I typed in 1106 and the suggested library was SH110X. This library includes the 1106 needed for my board. If you have not already installed it, press “Install”
Route Wires To Your Arduino
There are 2 protocols for OLED display boards I2C and SPI. I bought the I2C protocol since it has fewer inputs than the SPI.
I2C – this has 4 wires
- Ground
- VCC 3 to 5V
- SCL – Clock
- SDA – Data
Note that the order I have shown here may differ on the board that you have. Route your hookup wires carefully and always use the actual pins shown on your board.
After you soldier on the header pins, run the wires to your Arduino.
- Ground – Arduino Ground
- VCC – 5V Pin
- SCL – A5
- SDA – A4
Upload A Test Sketch
The easiest way to do this is to use an example sketch. Typically, the OLED libraries will include example code and that it what I used for testing purposes. I navigated to File->Examples->Adafruit SH110X (or the library you used) and then pick an example that fits your particular board.
Troubleshooting
I2C Address
If nothing appears, make sure that you have the correct I2C address.
In the example sketch, you will see something like this.
/* Uncomment the initialize the I2C address , uncomment only one, If you get a totally blank screen try the other*/
#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED’s
//#define i2c_Address 0x3d //initialize with the I2C addr 0x3D Typically Adafruit OLED’s
For my board, the correct address is the 0X3C. Yours may be different.
Pins Incorrect
Display issues can also be a results of incorrect pins. Be sure to reference your particular board and install according to the layout described above.
Wrong Library
The first time I tried the test code, the entire screen was garbled. The supplier I bought from apparently changed the library needed. Originally it was the 1306 library, but my version needed the 1106. Check your specifications and make sure you are using the right libary
Quick Reference – 1106 Library
Here are some basic commands that you will frequently use.
Setup Section
/********************************************************************* This is an example for our Monochrome OLEDs based on SH110X drivers This example is for a 128x64 size display using I2C to communicate 3 pins are required to interface (2 I2C and one reset) Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Limor Fried/Ladyada for Adafruit Industries. BSD license, check license.txt for more information All text above, and the splash screen must be included in any redistribution i2c SH1106 modified by Rupert Hirst 12/09/21 *********************************************************************/ #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SH110X.h> /* Uncomment the initialize the I2C address , uncomment only one, If you get a totally blank screen try the other*/ #define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's //#define i2c_Address 0x3d //initialize with the I2C addr 0x3D Typically Adafruit OLED's #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET -1 // QT-PY / XIAO Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #define NUMFLAKES 10 //used in the example sketch #define XPOS 0 //cursor position #define YPOS 1 //cursor position #define DELTAY 2 //adafruit logo static const unsigned char PROGMEM logo16_glcd_bmp[] = { B00000000, B11000000, B00000001, B11000000, B00000001, B11000000, B00000011, B11100000, B11110011, B11100000, B11111110, B11111000, B01111110, B11111111, B00110011, B10011111, B00011111, B11111100, B00001101, B01110000, B00011011, B10100000, B00111111, B11100000, B00111111, B11110000, B01111100, B11110000, B01110000, B01110000, B00000000, B00110000 }; void setup() { Serial.begin(9600); // Show image buffer on the display hardware. // Since the buffer is intialized with an Adafruit splashscreen // internally, this will display the splashscreen. delay(250); // wait for the OLED to power up display.begin(i2c_Address, true); // Address 0x3C default display.setContrast (0); // dim display display.display(); delay(2000); // Clear the buffer. display.clearDisplay(); }
Print Commands
void loop() { //small font display.setTextSize(1); //size display.setTextColor(SH110X_WHITE); //text color display.setCursor(0, 0); //position the cursor display.println("This is text that will be displayed"); //show font with a background - this is the "inverted" option where the background is highlighted and the text is shown as black inside the highlighted section display.setTextColor(SH110X_BLACK, SH110X_WHITE); // 'inverted' text display.println(3.141592); //print a number //larger font display.setTextSize(2); display.setTextColor(SH110X_WHITE); display.print("0x"); display.println(0xDEADBEEF, HEX); display.display(); delay(2000); display.clearDisplay(); }