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.
Components Required
- Arduino(Arduino Uno,Meg, Nano etc)
- 1.3-inch OLED Display (SH1106, 128x64)
- Connecting Wires
- 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:
- VCC: Connect to the 5V pin on the Arduino Mega.
- GND: Connect to the GND pin on the Arduino Mega.
- SCL: Connect to pin 21 (SCL) on the Arduino Mega.
- SDA: Connect to pin 20 (SDA) on the Arduino Mega.
The following shows the circuit diagram.
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.
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.
- Open the Arduino IDE.
- Go to Sketch > Include Library > Add .ZIP Library.
- 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.
- 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:
- Open the Arduino IDE.
- Go to Sketch > Include Library > Manage Libraries.
- In the Library Manager, search for "GFX" and click search.
- 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:
Step-by-Step Explanation of the Code:
- Library Inclusions: Includes the necessary libraries for the OLED display.
- Define Screen Dimensions: Specifies the width and height of the OLED screen.
- Display Initialization: Initializes the display and checks for connection issues.
- Clear Display: Clears the display buffer.
- Set Text Properties: Sets the text size and color.
- Display Text: Positions the cursor and prints "Hello, world!".
- Update Display: Updates the display to show the new text.
Step 4: Uploading the Code to Arduino Mega
- Connect your 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 Mega or Mega 2560", Port: corresponding port Like COM 5 here).
- 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.
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.