Introduction: Guide for I2C OLED Display With Bharat Pi

About: Bharat Pi is an IoT prototyping platform for students, innovators, startups and developers. A simplified board with an all in one compute network and storage on a single board to build rapid prototypes, colleg…

An OLED (Organic Light-Emitting Diode) display is a type of flat-panel display technology that uses organic compounds to produce light when an electric current is applied. Unlike traditional LCD displays, OLEDs do not require a backlight because each pixel emits its own light. This allows OLED displays to achieve deeper blacks, higher contrast ratios, faster response times, and wider viewing angles compared to LCDs.

OLED displays are widely used in various electronic devices, including smartphones, tablets, televisions, computer monitors, and wearable devices, due to their flexibility, thinness, and energy efficiency. They are also favored for their ability to produce vibrant colors and sharp images.

There are different types of OLED displays, such as passive-matrix OLEDs (PMOLEDs) and active-matrix OLEDs (AMOLEDs). AMOLEDs are more commonly used in modern consumer electronics because they offer faster refresh rates and higher resolution compared to PMOLEDs.

Overall, OLED displays offer several advantages over traditional display technologies, making them a popular choice for manufacturers and consumers alike.

Supplies

Oled display.

Bharat Pi

Jumper Wires

Step 1: Connection

Connecting an OLED display with an Bharat Pi is a popular project among hobbyists and developers due to the versatility and capabilities of both components. Here's a basic guide on how to connect an OLED display to an Bharat pi

Connect the OLED display to the ESP32 using jumper wires. Here's a typical pin configuration:

  • OLED VCC to 3.3V or VIN pin (depending on the display's voltage requirement)
  • OLED GND to Bharat pi GND
  • OLED SDA (Serial Data) to SDA pin
  • OLED SCL (Serial Clock) to SCL pin


Step 2: Code

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

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

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println("Hello, world!");
display.display();
}

void loop() {

}

Here's a breakdown of what each part of the code does:

  • #include <Wire.h>: This includes the Wire library, which is necessary for I2C communication.
  • #include <Adafruit_GFX.h> and #include <Adafruit_SSD1306.h>: These include the Adafruit graphics library and SSD1306 display library, respectively.
  • #define SCREEN_WIDTH 128 and #define SCREEN_HEIGHT 64: These macros define the width and height of the OLED display in pixels.
  • Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);: This line initializes an instance of the Adafruit_SSD1306 class for the OLED display, specifying its dimensions and I2C interface.
  • void setup() {...}: This is the setup function, where the initialization code is placed. In this case, it initializes serial communication and the OLED display. If the display initialization fails, it prints an error message and enters an infinite loop.
  • void loop() {...}: This is the loop function, which is empty in your code. Since you're displaying static text, there's no need for continuous updates in the loop.