DIY do it yourself
Master classes, instructions, useful tips, recipes.
» » »How to connect OLED I2C display to ARDUINO
Introduction: Introducing the 128 * 64/128 * 32 OLED I2C Display.
How to connect OLED I2C display to ARDUINO

Hello! Being an amateur - an electronics enthusiast, I am sure that all we do is electronic toys - homemade products, or large projects, all from curiosity and laziness. Curiosity seeks to understand and comprehend the immense, not known, to understand how it works there, what it does, how it moves. And laziness invents, in order to come up with this, so as not to get up, not fit, not lift, not get dirty or something else important.

Since seeing information is better than understanding what is about to happen in our device, or has already happened or is happening, we will definitely want to get this very useful information from our microcontrollers, sensors, or other devices. And I, in any case, want to receive any messages, such as questions, warnings, reminders, emoticons, asterisks, hearts and the like.

For those who also have a similar desire, here is a quick guide to connecting and checking small and inexpensive OLED displays.
Next, we will talk about one of the widely available for ham radio models of OLED displays controlled by the SSD1306 chip, with a screen size of 0.96 inches and a resolution of 128 * 64 or 128 * 32 pixels. These displays are ideal for small amateur radio designs and homemade products.
How to connect OLED I2C display to ARDUINO

Step 1: Key Concepts


We will connect it to Arduino UNO / NANO, well, and to program, respectively, we will also connect it through Arduino.
  • OLED is an Organic Light-Emitting Diode, i.e., a semiconductor device made from organic compounds, which begins to emit light when an electric current passes through it.
  • ARDUINO is a platform for training and building automation and robotics systems.
  • ArduinoIDE is a development environment. This is a free Arduino programming tool.
  • I2C - Inter-Integrated Circuits, inter-microcircuit communication line.
  • A sketch, it's a code, it's a program - Arduino terminology.

Follow the instructions to understand how to properly connect and configure an OLED display to the Arduino and how to display your own message on its display.
We do everything in steps.

Step 2: Accessories


We will need only four things:
  • 1. The OLED display itself is 0.96 ”(can be bought on Aliexpress or Ebay, - long, but cheap!).
  • 2. Arduino UNO / Nano (in the same place as the display).
  • 3. Connecting wires (ibid.).
  • 4. A computer or laptop with ArduinoIDE installed.

How to connect OLED I2C display to ARDUINO

In general, buying parts for various projects on Aliexpress and Ebay is a very cool thing, while you are doing one project, parts for another are already on the way. The main thing is not to forget to place an order.

Step 3: Connect the display


The display is controlled by the SSD1306 chip, which supports five communication protocols and one of which is I2C. Data on this protocol is transmitted in just two wires, and the fewer wires in the case, the better, so it suits us well. But! There are modules with SPI protocol and even protocol switching, so be careful when buying this module.
How to connect OLED I2C display to ARDUINO

Used conclusions:
OLED display - SCL / SCK (clock) and SDA (data), "Plus" power (VCC) and "Minus" power (GND).
How to connect OLED I2C display to ARDUINO

Used conclusions:
Arduino UNO - SCL / SCK on A5 and SSD on A4, “Plus” power (+ 5V) and “Minus” power (GND).
Connect the display to the Arduino:
  • Vcc - 5V
  • GND - GND
  • SDA - A4
  • SCL - A5

How to connect OLED I2C display to ARDUINO

Step 4: I2C Scanner


Each device on the I2C bus has a hexadecimal address, you can not change it, it is sewn tightly, each responsible manufacturer must indicate it somewhere on the case or in the instructions. There are modules with switches and jumpers with which you can change the address, but ... if the devices are cheap, to the ugliness, then the manufacturer may not bother to deal with such a trifle, so you will have to determine it yourself.

In total, up to 127 addresses can be used on the bus - 119 for devices and 8 service addresses. Communication is conducted at these addresses.There is a master, he is a Master, but there is a slave, he is also a Slave - Masters ask, the followers respond, everything is simple.
Since our OLED display uses the I2C communication protocol, and the address may not be indicated, we will try to find out this very address.

This can be done by downloading a short sketch to your Arduino board with OLED connected. BUT!
Take your time to upload the sketch to Arduino right away! Let's start by downloading the “driver”, i.e. we’ll connect the libraries, and for this we’ll go straight to “Step 5,” and then we’ll go back and continue.

Step 4: Continued:


Download CODE Finder_I2C_Hex_Address.ino, upload to Arduino.
Open the “Port Monitor”, set the speed to 9600 and if everything is connected correctly, the program will show the device address, in my case OLED with the address 0x3F.
How to connect OLED I2C display to ARDUINO

Step 5: Download and connect libraries


In order for everything to work correctly, and you did not have to reinvent the wheel, you need to connect a couple of libraries to the ArduinoIDE environment, namely: ADAFRUIT GFX and ADAFRUIT SSD1306, they are necessary for Arduino to be able to independently communicate with the OLED display.
You can connect these libraries by following these steps.
  • 1. In ArduinoIDE, go to the Sketch menu.
  • 2. Select “Include Libraries”.
  • 3. Select "Manage Libraries" / Managed Libraries.
  • 4. Find ADAFRUIT GFX and install them.
  • 5. Find ADAFRUIT SSD1306 and install them.

With fresh versions of libraries, the display worked crookedly for me, maybe of course this is due to the curvature of the hands, but after I installed the very first versions of these libraries, everything began to appear smoothly and beautifully. For some reason, Pushkin’s lines pop up in his memory:
... and experience, the son of difficult mistakes,
and genius, friend paradoxes.

Another way to install libraries is to find, download and install these libraries yourself.
For an example of Github:
https://github.com/adafruit/Adafruit-GFX-Library
https://github.com/adafruit/Adafruit_SSD1306

These are the same Adafruit libraries, but similar libraries are easily searched in search engines, at the request of OLED I2C. Many enthusiasts write libraries "for themselves", and then put them on the web. Which library to use is up to you.
In order for the display to display Russian letters, special libraries are also needed, but this is a separate topic for a separate article.
After the libraries are installed, you can return to step 4 and finally find out the hexadecimal address of your display.

Step 6: Testing the Display


To check if everything is working as expected, run the test example from ArduinoIDE.
For this:
Go FILE> EXAMPLES> SSD 1306> Select 128x64 i2c
If you get an “Error”, try choosing the SSD 1306> 128x32 i2c.
If Error again, try changing the I2C address on line 61 of the demo code, and replace it with the address of your display, which you determined in step # 4.
If the error occurs again, you can try editing the Adafruit_SSD1306.h file, it is located in the folder with the Arduino libraries.
How to connect OLED I2C display to ARDUINO

Open the Adafruit_SSD1306.h file in a text editor and find the lines:
// # define SSD1306_128_64
#define SSD1306_128_32
// #define SSD1306_96_16 

Uncomment the line:
#define SSD1306_128_64. 

It should be like this:
#define SSD1306_128_64
// # define SSD1306_128_32
// #define SSD1306_96_16

If the error is again, it is necessary to check the correct connections.
After the download is complete, you will see a test animation on the screen, which means that you have successfully configured your OLED display.

When you enjoy this animation, you can proceed to the next step.

Step 7: Writing Your Own Message


To write your own message, first create a new sketch in the ArduinoIDE programming environment.
In the header, we connect 4 libraries:
#include 
#include 
#include 
#include 

Then we write the reset protocol:
#define OLED_RESET 4
Adafruit_SSD1306 display (OLED_RESET);

In the VOID SETUP, specify the hexadecimal address of our 0x3C display, which we found out in “Step No. 4”.
Then, initialize the display and clear it:
display.begin (SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay ();

Next, in VOID LOOP, we write the main code, that is, our message, which we want to display on the display.
To do this, we describe the text size, text color, cursor position, and finally, display a message using the println command:
display.setTextSize (2);
display.setTextColor (WHITE);
display.setCursor (0,0);
display.println ("Well done!");
display.display ();

At the end, do not forget to write display.displayso that the image is displayed on the screen, otherwise you will get a void.
If you did everything correctly, a message will appear on the displays.
How to connect OLED I2C display to ARDUINO

Here is the CODE Starting_with_Arduino_OLED.ino of our program:
[946 b] (downloads: 1100)
come back
Comments (3)
  1. Tatoshka
    #1 Tatoshka Guests 14 november 2018 21:18
    0
    Thank you friend! This is my first uploaded sketch and my introduction to Arduino. Not counting Blink’s blinking))
  2. alisher
    #2 alisher Guests July 28, 2019 19:35
    0
    And where to download this Finder_I2C_Hex_Address.ino code?
    1. Al
      #3 Al Guests August 14, 2019 12:51
      0
      under the picture there is a link "Here is the CODE Starting_with_Arduino_OLED.ino of our program:"

Read also

Error codes for washing machines