Demo 6: How to use Arduino ESP32 to display information on OLED

1. Introduction
OLED screen have many “dots” on screen. So using OLED screen, we can display more complicated thing than LCD and LED matrix which have limited “dots” on screen. Currently there are 2 popular OLED screen. They are SH1106 and SSD1306, which have 128x64 dots on screen and using I2C or SPI for connection.



Figure: OLED screen with I2C connection.
2. Hardware

In this demo we will connect Arduino ESP32 to I2C SH1106 OLED screen (I2C address is 0x3C) to display a text “Hello world”. Here is the hardware connection:


Figure: ESP32 connect to I2C SH1106 OLED
Here we connect:
[ESP32 3.3V – OLED VCC]
[ESP32 GND – OLED GND]
[ESP32 IO12– OLED SDA]
[ESP32 IO14 – OLED SCL]
NOTE: while flashing SW for ESP32 if you see md5 error, please detach OLED VCC from ESP32 3.3V, after finishing flashing, attach again (I put delay(2000) in setup() so we have enough time to attach) or you can use external power for OLED.
3. Software

In order to communication with I2C SH1106 OLED screen, we will use the ssh1106-oled library. This library depend on Adafruit-GFX so you also need to download and install . With support from 2 libraries you can draw not only text but also geometry on OLED screen. You can download them here:
After downloading, unzip them and copy unzipped folders to libraries folder under Arduino folder: 

This library supplies some classes and interfaces that you should know:
  • Adafruit_SH1106 display(sda, scl): this will create an instance of OLED screen with ESP32 I2C pins
  • begin(uint8_t switchvcc = SH1106_SWITCHCAPVCC, uint8_t i2caddr = SH1106_I2C_ADDRESS, bool reset=true): initialize OLED screen, with SH1106_SWITCHCAPVCC we connect OLED VCC with ESP 3.3V, the address of I2C OLED screen (in this case it is 0x3C)
  • display(): you calculate dots behind the scene and using this function to bring them on screen
  • width(): to get width of screen
  • height(): to get height of screen
  • clearDisplay(): to clear the scrren
  • println(text): to draw text on screen
  • setCursor(x,y): set cursor to location (x, y)
  • drawCircle(): to draw a circle on screen
You can refer more functions in “Adafruit_GFX.h”
Create an Arduino project and Save as esp32oled with code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>

#define OLED_SDA 12
#define OLED_SCL 14

Adafruit_SH1106 display(12, 14);


void setup()   {                
  Serial.begin(115200);
  /* initialize OLED with I2C address 0x3C */
  display.begin(SH1106_SWITCHCAPVCC, 0x3C); 
  display.clearDisplay();

}
void loop() { 
  /* set text size, color, cursor position, 
  set buffer with  Hello world and show off*/
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Hello, world!");
  display.display();
  delay(2000);
  display.clearDisplay();
}

4. Result


Figure: ESP32 connect to OLED to display "Hello World"

Post a Comment

9 Comments

Unknown said…
rst:0x10 (RTCWDT_RTC_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0x00
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
cmd len -1
Falling back to built-in command interpreter.
OK
getting this error................
There is 1 tip, when flashing firmware for ESP32, un-connect ESP32 VCC from OLED VCC. After ESP32 finished starting then connect again. So you need a small delay in setup() function so that you have time to connect them again.

:)
Unknown said…
I cannot overcome this obstacle... Pl, give it with more details. It finishes to flash...then automatic hard reset and then I put in VCC pin? Or what?
Unknown said…
I have just found the solution to fix the problem. Simply change pins assignment.
#define OLED_SDA 16
#define OLED_SCL 17

Adafruit_SH1106 display(OLED_SDA, OLED_SCL);

and you don't need to delay(2000); Your script will start automatically right after a hard reset.
Unknown said…
I have just found the solution to fix the problem. Simply change pins assignment.
#define OLED_SDA 16
#define OLED_SCL 17

Adafruit_SH1106 display(OLED_SDA, OLED_SCL);

and you don't need to delay(2000); Your script will start automatically right after a hard reset.
Hi friend, you can connect OLED VCC to 3.3V pin on esp32. :)
mehmetgok said…
Thanks for the tutorial.

ESP32 boots from SPI flash. IO12 pin conflict with an hSPI pin on my ESP board. So pin assignment is necessary for some boards. Did you try to use other fonts?

I needed to make a change for "glcdfont.c" line 10 as :

#elif defined(ESP8266) || defined(ESP32)

I also had problems with some external 5V power supplies interestingly.
Jaystings said…
"HELLO WOR
LD!"

That made me chuckle. Thanks a lot for letting me know the proper library folder, I thought it was under, "ArduinoData/libraries."
lalimalapera said…
There’s also a refer-a-friend bonus, whereas Cafe Casino VIPs can get pleasure from 15% every day cashback. Cafe Casino has turn into well-known over time for having some pretty sweet bonuses. Things kick off with the selection of two welcome bonuses – either a 250% a lot 1xbet korea as} $1,500 fiat currency on line casino bonus, or a 350% a lot as} $2,500 Bitcoin welcome bonus. Cafe Casino has a modest number of video games general, with its complete number at a hundred and sixty.