Monday, May 15, 2017

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"

6 comments:

prasangika jayawickrama 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................

iotsharing dotcom said...

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.

:)

Sergey Nikulin 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?

Sergey Nikulin 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.

Sergey Nikulin 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.

iotsharing dotcom said...

Hi friend, you can connect OLED VCC to 3.3V pin on esp32. :)