Beginner's Guide to Testing an OLED Display with Arduino

OLED displays are popular in various electronics projects due to their high contrast, wide viewing angles, and low power consumption. This guide will help you get started with testing an 1.3-inch OLED Display with Arduino. We'll walk through wiring the display to the Arduino, installing the necessary libraries, and running a simple test code to display text on the OLED.

oled display with arduino

 

Components Required

  1. Arduino(Arduino Uno,Meg, Nano etc)
  2. 1.3-inch OLED Display (SH1106, 128x64)
  3. Connecting Wires
  4. Breadboard (optional)

Step 1: Wiring the OLED Display to Arduino Mega

Most OLED displays communicate via I2C, which requires only four connections: VCC, GND, SCL, and SDA. Here’s how to connect your OLED display to the Arduino Mega:

  1. VCC: Connect to the 5V pin on the Arduino Mega.
  2. GND: Connect to the GND pin on the Arduino Mega.
  3. SCL: Connect to pin 21 (SCL) on the Arduino Mega.
  4. SDA: Connect to pin 20 (SDA) on the Arduino Mega. 

The following shows the circuit diagram.

 

Arduino OLED display interfacing

Step 2: Installing the Required Libraries

To make your Arduino Mega communicate with the OLED display, you need to install the following libraries in the Arduino IDE:

a. Adafruit SH1106 Library

Here we will use the SH1106 library for the 1.3" OLED display module because the OLED display module did not work with the SSD1306 library. I tried using the SSD1306 driver but I just got a random black and white screen like the one shown below.

no oled display
 After searching for while for solution, I found out that SH1106 driver maybe the solution to the problem. As far as I have understood, the older OLED uses the SH1106 driver. So I downloaded the SH1106 from the following url.

Download Adafruit_SH1106 Library Free

or, download from github:

https://github.com/wonho-maker/Adafruit_SH1106/tree/master

 Once you have downloaded the Adafruit SH1106 library follow the instruction to install the library.

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Add .ZIP Library.
  3. Browse and locate the Adafruit_SH1106-master and click on Open. The library will be installed and you will see a message on the terminal Library Installed.
  4. You can verify this by going the Sketch > Examples > Adafruit_SH1106-master as shown below.


b. Adafruit GFX Library

To install this GFX library following the instruction below:

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. In the Library Manager, search for "GFX" and click search.
  4. Search for "Adafruit GFX Library" and click Install.

Step 3: Writing and Uploading the Test Code

Here's a simple test code to display "Hello, World!" on the OLED display:


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

#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2

#if (SH1106_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SH1106.h!");
#endif

void setup()   {                
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done
 
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);

  // Clear the buffer.
  display.clearDisplay();

  // text display tests
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Hello, World!");
  display.display();
  delay(2000);

}


void loop() {
 
}


Step-by-Step Explanation of the Code:

  1. Library Inclusions: Includes the necessary libraries for the OLED display.
  2. Define Screen Dimensions: Specifies the width and height of the OLED screen.
  3. Display Initialization: Initializes the display and checks for connection issues.
  4. Clear Display: Clears the display buffer.
  5. Set Text Properties: Sets the text size and color.
  6. Display Text: Positions the cursor and prints "Hello, world!".
  7. Update Display: Updates the display to show the new text.

Step 4: Uploading the Code to Arduino Mega

  1. Connect your Arduino Mega to your computer using a USB cable.
  2. Open the Arduino IDE and paste the above code into a new sketch.
  3. Select the correct board and port from the Tools menu (Board: "Arduino Mega or Mega 2560", Port: corresponding port Like COM 5 here).
  4. Click the Upload button to upload the code to the Arduino Mega.

Step 5: Observing the Output

Once the code is uploaded, you should see "Hello, world!" displayed on your OLED screen. This confirms that your OLED display is working correctly with the Arduino Mega. Mine is shown below.

oled display with arduino

Troubleshooting Tips

  • Check Connections: Ensure all the connections are secure and correctly placed.
  • Address Issue: If the display doesn't work, you might need to check the I2C address. Use an I2C scanner code to find the correct address.
  • Library Issues: Make sure the libraries are installed correctly. Reinstall if necessary.

Conclusion

Testing your OLED display with an Arduino Mega is a straightforward process. By following this guide, you should have a functional setup displaying basic text. This setup serves as a foundation for more complex projects involving graphics, sensors, and other inputs. For example see LCD display of Temperature and Humidity tutorial wherein we have used the 16x2 LCD display.

Post a Comment

Previous Post Next Post