Hardware Connection
(I) Module Pin Definition
VCC: Positive power supply terminal, generally connectable to 3.3V or 5V (depending on the module specifications; some modules only support 3.3V. Ensure that the supply voltage matches, otherwise damage to the module may occur)
GND: Negative power supply terminal, ground pin, provides a reference potential for the circuit.
SCL: The I2C clock line input pin, used to synchronize data transmission. During I2C communication, the master device (such as a microcontroller) generates a clock signal and transmits it to the OLED module through this pin.
SDA: The I2C data line input/output pin, used to transmit data and commands between the master device and the OLED module (slave device). During data transmission, changes in the level of this pin represent the corresponding data value.
(II) Microcontroller Connection Example (Using the STM32F103C8T6 as an Example)
VCC: Connects to the 3.3V power output pin of the STM32F103C8T6 development board to power the OLED module.
GND: Connects to the GND pin of the STM32F103C8T6 development board for common ground.
SCL: Connects to the PB6 pin of the STM32F103C8T6 (the I2C1 clock pin of the STM32).
SDA: Connect to the PB7 pin of the STM32F103C8T6 (the I2C1 data pin on the STM32)
Different microcontrollers have different I2C pin definitions. For example, on the ESP32, you can connect SCL to GPIO22 and SDA to GPIO21 or other pins that support I2C communication
I2C Communication Principles
(I) I2C Bus Basics
I2C is a multi-master serial bus protocol that allows multiple master and slave devices to connect on the same bus. Its physical layer consists of two lines, SDA and SCL, which connect multiple devices using wired-AND logic. Multiple devices can drive the bus simultaneously without conflict. I2C supports standard mode (100 kbps) and fast mode (400 kbps). In 0.96-inch OLED display module applications, standard mode is typically sufficient for display data transmission.
(II) Communication Process
Start Signal: When a master device initiates communication, it pulls SDA from high to low while SCL is high. This signal indicates the start of I2C communication, and the master device is then ready to transmit data.
Address Transmission: After the start signal, the master device first sends a 7-bit slave address (for OLED modules, different modules may have different default addresses, such as the common 0x3C or 0x78), followed by a 1-bit read/write bit (0 indicates a write operation, 1 indicates a read operation). After receiving the address, the OLED module checks whether it matches its own address. If so, it pulls the SDA line low while the SCL line is high, sending an acknowledgement (ACK) to the master device.
Data Transmission: After the address is transmitted and an ACK is received, the master device begins transmitting data. After each byte of data is transmitted, the master device releases the SDA line and waits for an ACK from the slave device (OLED module). If the slave device successfully receives the data, it pulls SDA low as an ACK; if it fails, it keeps SDA high (a non-acknowledgement, NACK). In OLED display applications, the master device sends a series of control commands and display data to the OLED module. For example, commands to set the display mode and contrast, as well as image data to illuminate specific pixels on the screen. Stop Signal: When all data transfers are complete, the master device pulls SDA from low to high while SCL is high. This signal indicates the end of I2C communication and releases the bus for other devices.
Common Problems and Solutions
(1) OLED Screen Does Not Display
Check the hardware connections: Confirm that VCC and GND are connected correctly and power is being supplied, and that SCL and SDA are connected correctly and are not short-circuited or open-circuited. Use a multimeter to measure the voltage and continuity of each pin.
Check the initialization code: Ensure that the OLED initialization command is sent correctly. Check whether I2C communication initialization is successful, such as whether the I2C clock configuration and address settings are correct. Add debug print statements to the code to verify the execution of each step in the initialization process.
(2) Displaying Garbled or Abnormal Characters
Check the font library and data format: If the displayed characters are abnormal, confirm that the font library is compatible with the OLED module and that the character encoding conversion is correct. For abnormal image display, check whether the image data format meets the requirements of the OLED module, such as whether it is correctly converted to 128*64 resolution dot matrix data.
Check communication stability: Instable I2C communication may cause data transmission errors. Appropriately reduce the I2C clock speed to improve data transmission stability. At the same time, check whether there is electromagnetic interference, such as moving the OLED module away from other