How to connect OLED I2C display to ARDUINO

Wiring diagram for OLED I2C display to Arduino and example sketch. Description of work and application.

How to connect OLED I2C display to ARDUINO
How to connect OLED I2C display to ARDUINO

Step 1: Basic concepts

We will connect it to the Arduino UNO / NANO, well, and program it, respectively, we will also use it through the Arduino.

  • OLED is an Organic Light-Emitting Diode, i.e. a semiconductor device made of organic compounds that starts 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. It is a free Arduino programming software.
  • I2C - Inter-Integrated Circuits, inter-circuit communication line.
  • Sketch, aka code, aka program - Arduino terminology.

Step 2: accessories

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

Step 3: Connecting the display

The display is controlled by the SSD1306 chip, which supports five communication protocols, one of which is I2C. Data on this protocol is transmitted over only two wires, and the fewer wires in the case, the better, so it is quite suitable for us. But! There are modules with the SPI protocol and even with protocol switching, so be careful when purchasing this module.

Conclusions used:
OLED display - SCL / SCK (clock) and SDA (data), Power Plus (VCC) and Power Minus (GND).


Conclusions used:
Arduino UNO - SCL / SCK on A5 and SSD on A4, Power Plus (+ 5V) and Power Minus (GND).
We connect the display to the Arduino:
Vcc - 5V
GND - GND
SDA - A4
SCL - A5
 

Step 4:

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.

Step 5: Loading and Including Libraries

In order for everything to work properly, and you do 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 the Arduino to be able to communicate with the OLED display on its own.
You can connect these libraries by following the steps below.
1. In ArduinoIDE go to the Sketch menu.
2. Select "Include Libraries" / Include Libraries.
3.Select "Managed Libraries".
4. Find ADAFRUIT GFX and install them.
5. Find ADAFRUIT SSD1306 and install them.

Step 6: test the display

To test if everything works as expected, run the test example from ArduinoIDE.
For this:
Go to FILE> EXAMPLES> SSD 1306> Select 128x64 i2c
If you get an "Error" try selecting SSD 1306> 128x32 i2c.
If again "Error" try changing the I2C address in line 61, demo code, and replace it with the address of your display that you defined in step # 4.
If the error is again, you can try editing the Adafruit_SSD1306.h file, it is located in the Arduino libraries folder.

Step 7: writing your own message

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

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Then we write the reset protocol:

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

In VOID SETUP we indicate the hexadecimal address of our display 0x3C, which we learned in "Step # 4".
Then, we 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 that we want to display on the display.
To do this, we describe the size of the text, the color of the text, the position of the cursor, and finally, display the message using the println command:

display.setTextSize(2); 
display.setTextColor(WHITE); 
display.setCursor(0,0); 
display.println("Well done!"); 
display.display();

Don't forget to write display.display at the end so that the image is displayed on the screen, otherwise you will get empty.
If you did everything correctly, a message will appear on the displays.

 


Find out about the update of this script first in our telegram channel: https://t.me/proweblabxyz