OLED displays are widely used in electronics projects due to their high contrast, wide viewing angles, and low power consumption. This guide will walk you through the process of testing a 1.3-inch OLED display (SH1106, 128x64) with an Arduino Uno. By the end, you'll be able to display text on the OLED and build a foundation for more complex projects.
Components Required
- Arduino (Uno, Mega, Nano, etc.)
- 1.3-inch OLED Display (SH1106, 128x64)
- Connecting wires
- Breadboard (optional)
Step 1: Connecting the OLED Display to Arduino Mega
Most OLED displays use the I2C communication protocol, requiring only four connections:
OLED Pin | Arduino Mega Pin |
---|---|
VCC | 5V |
GND | GND |
SCL | Pin 21 (SCL) |
SDA | Pin 20 (SDA) |
Here's the circuit diagram for the wiring:
Step 2: Installing Necessary Libraries
To enable communication between the Arduino and the OLED, you'll need the following libraries:
a. Adafruit SH1106 Library
The SH1106 library is used because some older OLED modules, like the one in this guide, do not work with the SSD1306 library. Using the SSD1306 driver may result in a blank or garbled screen.
- Download the SH1106 library from GitHub.
- Install the library in the Arduino IDE:
- Open Arduino IDE.
- Go to Sketch > Include Library > Add .ZIP Library.
- Locate the downloaded SH1106 ZIP file and click Open.
Verify the library installation by checking Sketch > Examples > Adafruit_SH1106-master.
b. Adafruit GFX Library
The GFX library provides graphics functions for the OLED display.
- To install:
- Open Arduino IDE.
- Go to Sketch > Include Library > Manage Libraries.
- Search for Adafruit GFX Library and click Install.
Step 3: Writing and Uploading the Test Code
Here’s a sample code to display "Hello, World!" on the OLED:
-------------------------------------------
Explanation of the Code:
- Library Inclusions: Adds required libraries for the OLED.
- Initialization: Sets up the OLED with the I2C address
0x3C
. - Text Configuration: Specifies text size, color, and position.
- Display Update: Outputs "Hello, World!" on the OLED screen.
Step 4: Uploading the Code to Arduino Mega
- Connect the Arduino Mega to your computer using a USB cable.
- Open the Arduino IDE and paste the above code into a new sketch.
- Select the correct board and port from the Tools menu:
- Board: "Arduino Uno".
- Port: The COM port associated with your Arduino.
- Click the Upload button.
Step 5: Observing the Output
After uploading the code, you should see "Hello, World!" displayed on the OLED screen, confirming that the display is functioning correctly.
Troubleshooting Tips
- Check Connections: Ensure wires are securely connected to the correct pins.
- Address Issues: If the display doesn’t work, run an I2C scanner sketch to identify the correct I2C address.
- Reinstall Libraries: If errors persist, reinstall the SH1106 and GFX libraries.
Conclusion
This guide demonstrates how to test a 1.3-inch OLED display with an Arduino Uno. Once your setup is functional, you can expand it to display graphics, sensor data, or other dynamic content. For more inspiration, check out our LCD Display of Temperature and Humidity Tutorial, where a 16x2 LCD is used for sensor data display.