X
Home > Blog > STEM for ESP32 > HOW to Show Characters / Icons on OLED Display with ESP32

HOW to Show Characters / Icons on OLED Display with ESP32

By r December 30th, 2024 156 views


1. Overview

KidsIOT comes with a 0.96 inch 128x64 pixel OLED display. In this project, we’ll write Arduino code to learn how to use it.

2. Principle

The OLED has been integrated on the development board. Its communication mode is IIC, whose communication address is 0x3C.

How I2C works?

The I2C bus has two signal, as well as power and ground connection.

a30

Two signal lines:

  • SDA – Bidirectional data line

  • SCL – Clock signal line

Each signal line connects to two pull-up resistors that pull the bus up to the supply voltage when the bus is inactive. Please note that the supply voltage is not standard. It can be 3.3 volts or 5 volts. For some high-speed I2C, it may be a lower voltage.

This difference in power supply voltage may cause problems when you connect I2C devices that use different logic levels.

Two types of devices that can connect to I2C bus:

  • Host/Master devices

  • Slave devices

Host controls the bus and provides clock signals. It requests data from the slave station separately. There can be multiple hosts on the bus, but only one can be active at any given moment.

No address is assigned to host, while slave has a sole address on the bus. They adopts 7-bit addressing, so you can have up to 128 slave devices on an I2C bus. In real life, such a large collection has never been used. It is rare to see more than a dozen I2C devices on a single bus.

An updated 10-bit addressing scheme has been implemented, which is backward compatible with the existing 7-bit one.

Commercial I2C devices are assigned addresses by NXP which maintains the bus specification. Although I2C has been open source since 2006, there is a fee to obtain slave addresses from NXP. No fee is charged for hosts or slaves for non-commercial manufacturing.

Some I2C devices are assigned multiple addresses, often varying in low address bits. They can be manually configured with different addresses, so that multiple same-typed devices to be used on a single I2C bus.

For more information about IIC communication, please Google.

3. Test Code

/*  
  Project name: 11_oled
  Function: OLED display characters
  Author: keyestudio    
  Hardware connection:  
    - none
  Library:  
    - Adafruit_SSD1306
    - Adafruit_GFX
  Cautions:  
    - Before uploading test code, please correctly connect to the development board and port
    - Before uploading test code, the library must be imported
*/

#include     // Core graphics library  
#include  // Hardware-specific library for SSD1306  
  
// Creating SSD1306 display objects (using I2C interface) 
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED displays height, in pixels
  
// Declares an SSD1306 display object  
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);  
  
void setup() {  
  // Initialize serial communication (optional, for debugging)  
  Serial.begin(115200);  
    
  // Initialize the display, 0x3C is the I2C address
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));  
    while(1); // quit  
  }  
    
  // Clear display buffer  
  display.clearDisplay();  
    
  // Set the text color to white
  display.setTextColor(SSD1306_WHITE);  
    
  // Set text size (1 indicates normal size)  
  display.setTextSize(1);  
    
  // Sets the start position of the text 
  display.setCursor(0,0);  

  // Displays the first line of text  
  display.println("Hello, World!");  
    
  // Move to next line (Wrap)  
  // Or you can manually set the cursor position 
  // display.setCursor(0,10); // Starting position of the second line (y coordinate can be adjusted according to font height)
    
  // Displays the second line of text 
  display.println("Hello, Keyestudio!");  
    
  // Update display content 
  display.display();  
}  
  
void loop() {  
  // no need to do anything in the loop unless you want to dynamically update the display 
}

4. Test Result

After uploading the test code, the on-board OLED displays “Hello, World!” and “Hello, Keyestudio!” on two lines.

a31

HOW to Get Temperature And Humidity Value with ESP32
Previous
HOW to Get Temperature And Humidity Value with ESP32
Read More
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Next
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Read More
Message Us