ESP32-CAM I2C OLED display Tutorial

In this tutorial, we will guide you through interfacing an I2C OLED display with the ESP32-CAM. We'll use the Adafruit SH1106 library to display text on the OLED screen. The ESP32-CAM is a versatile and powerful microcontroller, and integrating an OLED display can add a visual element to your projects.

What You Will Need

  • ESP32-CAM module
  • 0.96 inch I2C OLED display
  • Breadboard and jumper wires
  • USB to TTL converter (to program the ESP32-CAM)
  • Arduino IDE installed on your computer

Setting Up the Hardware

The following shows the circuit diagram of connecting I2C OLED display to ESP32-CAM module.

I2C OLED display ESP32-CAM circuit diagram

 

  1. Connect the OLED Display to the ESP32-CAM:

    • VCC to 3.3V on the ESP32-CAM
    • GND to GND on the ESP32-CAM
    • SDA to GPIO 21
    • SCL to GPIO 22
  2. Prepare the ESP32-CAM for Programming:

    • Connect the USB to TTL converter to your ESP32-CAM.
    • Connect the TX pin of the converter to U0RXD (GPIO3) of the ESP32-CAM.
    • Connect the RX pin of the converter to U0TXD (GPIO1) of the ESP32-CAM.
    • Connect the 5V and GND pins of the converter to the 5V and GND pins of the ESP32-CAM, respectively.
    • Connect GPIO0 to GND to put the ESP32-CAM into programming mode.

Installing the Required Libraries

Before writing the code, ensure you have the necessary libraries installed in the Arduino IDE. You will need the Adafruit GFX and Adafruit SH1106 libraries.

  1. Go to Sketch > Include Library > Manage Libraries.
  2. Search for Adafruit GFX and install it.
  3. Search for Adafruit SH1106 and install it.

Writing the Code

Open the Arduino IDE and follow these steps:

  1. Configure the Board:

    • Go to Tools > Board and select ESP32 Wrover Module.
    • Go to Tools > Port and select the COM port your USB to TTL converter is connected to.
  2. Write the OLED Display Code:

#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 initialized with an Adafruit splash screen
  // internally, this will display the splash screen.
  display.display();
  delay(2000);

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

  // text display tests
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(30, 30);
  display.println("ee-diary.com");
  display.display();
  delay(2000);
}

void loop() {
  // You can add more functionality here
}

Uploading the Code

  1. Upload the Code:

    • Click the upload button in the Arduino IDE to compile and upload the code to your ESP32-CAM.
    • When you see the Connecting... message in the IDE, press the reset button on the ESP32-CAM to start the upload process.
  2. Disconnect GPIO0:

    • After the code is uploaded, disconnect GPIO0 from GND to run the program.

Testing the OLED Display

Once the code is uploaded and GPIO0 is disconnected from GND, the OLED display should show the text "ee-diary.com" in the center of the screen.

Troubleshooting

  • No Display:

    • Ensure all connections are correct.
    • Verify the I2C address (0x3C) is correct for your OLED display.
    • Check if the necessary libraries are installed correctly.
  • Upload Issues:

    • Make sure GPIO0 is connected to GND during the upload.
    • Press the reset button when you see the Connecting... message.

Conclusion

Congratulations! You have successfully interfaced an I2C OLED display with the ESP32-CAM and displayed text on it. This basic project can be expanded by adding more features such as displaying sensor data, images, or other information. The combination of the ESP32-CAM and an OLED display opens up numerous possibilities for your IoT projects. Happy coding!

See other ESP32-CAM tutorials:

Post a Comment

Previous Post Next Post