Arduino Code and Video for SSD1306 OLED 128 x 64 Display

Arduino Code and Video for SSD1306 OLED 128 x 64 Display

In this tutorial, we will explore how to use the SSD1306 OLED 128 x 64 display with an Arduino. The SSD1306 is a versatile display that allows you to show text, graphics, and various shapes easily. By the end of this article, you will be able to set up the display and run code that demonstrates its capabilities.

SSD1306 OLED displaying text

We will guide you through the necessary hardware connections and provide code snippets to help you understand the key identifiers used in the program. For a more visual explanation, be sure to watch the associated video (in video at 00:00).

Hardware Explained

The main components of this project include the Arduino board and the SSD1306 OLED display module. The SSD1306 display utilizes the I2C communication protocol, which simplifies wiring by requiring just two data lines: SDA for data and SCL for the clock signal. The display operates at voltages between 3.3V and 5V, making it compatible with most Arduino boards.

In addition to the display, you will need jumper wires to connect the module to your Arduino. The connections include VCC to power, GND for ground, SDA to the Arduino's data pin, and SCL to the clock pin. This setup allows for easy communication between the Arduino and the OLED display.

Datasheet Details

ManufacturerAdafruit
Part numberSSD1306
Logic/IO voltage3.3 V - 5 V
Supply voltage3.3 V - 5 V
Display resolution128 x 64 pixels
I2C address0x3C
InterfaceI2C
PackageModule

  • Ensure proper wiring: VCC to 3.3V or 5V, GND to ground, SDA to A4, SCL to A5 (for Arduino Uno).
  • The display requires pull-up resistors on the SDA and SCL lines if not already included.
  • Verify the I2C address is set correctly in the code, typically 0x3C for this display.
  • Use a suitable library such as Adafruit_SSD1306 for easy integration.
  • Clear the display before drawing new graphics to avoid overlapping artifacts.

Wiring Instructions

Arduino wiring for SSD1306 OLED
Arduino wiring for SSD1306 OLED

To wire the SSD1306 OLED display to your Arduino, follow these steps:

Connect the VCC pin of the OLED display to the 5V (or 3.3V) pin on the Arduino. Next, connect the GND pin of the display to the ground pin on the Arduino. For the I2C communication, connect the SDA pin of the display to the A4 pin on the Arduino and the SCL pin to the A5 pin. This setup will enable the Arduino to communicate with the OLED display using the I2C protocol.

Make sure that all connections are secure, and double-check that you are using the correct voltage for your specific display model. If you are using a different Arduino model, the SDA and SCL pins may vary (e.g., for Arduino Mega, SDA is on pin 20 and SCL is on pin 21).

Code Examples & Walkthrough

In the code, we initialize the display and set up the parameters like I2C address and dimensions. One key identifier is display, which represents the instance of the SSD1306 display. Here’s a snippet from the setup function:

void setup() {                
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C
}

In this snippet, the display.begin function initializes the display with the specified I2C address. It is essential to match the address with your display to ensure proper communication.

Next, in the loop function, we use the display.clearDisplay() method to clear the screen before drawing new content. We can set the text size and position using the setTextSize and setCursor methods:

void loop() {
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(2,1); // set cursor at top left corner
  display.println("Robojax"); // display text
}

Here, the text "Robojax" is displayed at the coordinates (2,1) on the screen. This allows for precise placement of text on the OLED display.

Finally, to render the changes on the display, we call display.display(). This function sends all the buffered commands to the display to update its content:

display.display();

This should be the last line in your loop function to ensure that all drawing commands are executed. If you forget this step, nothing will appear on the display.

Remember, the full code loads below the article for your reference.

Demonstration / What to Expect

After uploading the code to your Arduino, you should see the text "Robojax" displayed on the OLED screen. The code also demonstrates scrolling text and drawing lines, shapes, and other graphics on the display. Watch for any common pitfalls, such as incorrect wiring or mismatched I2C addresses, which can prevent the display from functioning correctly (in video at 10:00).

Video Timestamps

  • 00:00 - Introduction to SSD1306 OLED Display
  • 02:30 - Wiring the Display
  • 05:00 - Code Walkthrough
  • 08:00 - Demonstration of Display Features
  • 10:00 - Common Issues and Troubleshooting

Images

SSD1306 OLED screen 128x32 module
SSD1306 OLED screen 128x32 module
SSD1306 OLED displaying text
SSD1306 OLED displaying text
SSD1306 OLED displaying text
SSD1306 OLED displaying text
Arduino wiring for SSD1306 OLED
Arduino wiring for SSD1306 OLED
38-This is the Arduino code for the PCA9685 16-channel 12-bit servo controller.
Language: C++
39-This is Arduino code for an SSD1306 OLED 128 x 64 display without custom methods.
Language: C++
40-This is the Arduino code for the SSD1306 OLED 128 x 64 display without custom methods.
Language: C++
Copied!

Files📁

No files available.