ESP8266 OLED Example
OLED Displays are great in many ways. They use very little power, are bright, easy to read with large viewing angle and have high resolution considering their small size.
The OLED we will use today is .96″ inch in size, features 128×64 pixels and uses the SPI Bus.
We will use the u8glib library to communicate with our display. This library has many available parameters, we will use some of them to display a bitmap image and to display some text.
If you would like to experiment and learn more about this library, you can go to the Wiki: U8GLIB Wiki.
In this tutorial we will use the ESP8266 Wifi Module SSD1306 based OLED Display.
Our OLED display uses the SPI Bus.
Since the SPI Bus is faster than I2C, this will make our display very responsive.
Of course there’s a tradeoff, the SPI Bus uses more pins on our ESP8266
Pinout of our OLED Display
GND – Ground
VCC – Power 2.8V to 5.5V
D0 – Clock
D1 – Data
Res – Reset
DC – Data/Command
CS – Chip Select
Connect the OLED to the esp8266 Module
GND = GND
VCC = 3.3V
Hardware SPI CLK/D0 = GPIO14
Hardware SPI MOSI/D1 = GPIO13
RESET = GPIO16
DC = GPIO2
CS = GPIO15, pull-down 10k to GND
Code
Make sure to use the latest nodemcu firmware (greater than 0.9.6)
you can download from here
https://github.com/nodemcu/nodemcu-firmware/releases/download/0.9.6-dev_20150704/nodemcu_float_0.9.6-dev_20150704.bin
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
function init_spi_display() -- Hardware SPI CLK = GPIO14 -- Hardware SPI MOSI = GPIO13 -- Hardware SPI MISO = GPIO12 (not used) -- CS, D/C, and RES can be assigned freely to available GPIOs local cs = 8 -- GPIO15, pull-down 10k to GND local dc = 4 -- GPIO2 local res = 0 -- GPIO16 spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0) disp = u8g.ssd1306_128x64_spi(cs, dc, res) end function prepare() disp:setFont(u8g.font_6x10) disp:setFontRefHeightExtendedText() disp:setDefaultForegroundColor() disp:setFontPosTop() end function stringtest(a) disp:drawStr(30+a, 31, " 0") disp:drawStr90(30, 31+a, " 90") disp:drawStr180(30-a, 31, " 180") disp:drawStr270(30, 31-a, " 270") end function line(a) disp:drawStr(0, 0, "drawLine") disp:drawLine(7+a, 10, 40, 55) disp:drawLine(7+a*2, 10, 60, 55) disp:drawLine(7+a*3, 10, 80, 55) disp:drawLine(7+a*4, 10, 100, 55) end function triangle(a) local offset = a disp:drawStr(0, 0, "drawTriangle") disp:drawTriangle(14,7, 45,30, 10,40) disp:drawTriangle(14+offset,7-offset, 45+offset,30-offset, 57+offset,10-offset) disp:drawTriangle(57+offset*2,10, 45+offset*2,30, 86+offset*2,53) disp:drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset) end function ascii_1() local x, y, s disp:drawStr(0, 0, "ASCII page 1") for y = 0, 5, 1 do for x = 0, 15, 1 do s = y*16 + x + 32 disp:drawStr(x*7, y*10+10, string.char(s)) end end end function extra_page(a) disp:drawStr(0, 12, "setScale2x2") disp:setScale2x2() disp:drawStr(0, 6+a, "setScale2x2") disp:undoScale() end function draw(draw_state) local component = bit.rshift(draw_state, 3) prepare() if (component == 0) then stringtest(bit.band(draw_state, 7)) elseif (component == 1) then line(bit.band(draw_state, 7)) elseif (component == 2) then triangle(bit.band(draw_state, 7)) elseif (component == 3) then ascii_1() elseif (component == 4) then extra_page(bit.band(draw_state, 7)) end end function graphics_test(delay) print("--- Starting Graphics Test ---") local draw_state for draw_state = 0, 4 + 8*8, 1 do disp:firstPage() repeat draw(draw_state) until disp:nextPage() == false --print(node.heap()) tmr.delay(delay) -- re-trigger Watchdog! tmr.wdclr() end print("--- Graphics Test done ---") end --init_i2c_display() init_spi_display() graphics_test(50000) |
Example 2
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
myStr0 = "Hello" myStr1 = "Hello" myStr2 = "Hello" -- place holders for the other lines myStr3 = "Hello" -- place holders for the other lines myStr4 = "Hello" myStr5 = "Hello" myStr6 = "Hello" myStr7 = "Hello" -- setup SPI and connect display function init_spi_display() -- Hardware SPI CLK = GPIO14 5 -- Hardware SPI MOSI = GPIO13 7 -- Hardware SPI MISO = GPIO12 (not used) -- CS, D/C, and RES can be assigned freely to available GPIOs local cs = 8 -- GPIO15, pull-down 10k to GND local dc = 4 -- GPIO2 local res = 0 -- GPIO16 spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0) disp = u8g.ssd1306_128x64_spi(cs, dc, res) end -- graphic test components function prepare() disp:setFont(u8g.font_6x10) disp:setFontRefHeightExtendedText() disp:setDefaultForegroundColor() disp:setFontPosTop() end function draw(void) prepare() disp:drawStr(0,0,myStr0) disp:drawStr(0,8,myStr1) disp:drawStr(0,16,myStr2) disp:drawStr(0,24,myStr3) disp:drawStr(0,32,myStr4) disp:drawStr(0,40,myStr5) disp:drawStr(0,48,myStr6) disp:drawStr(0,56,myStr7) end function loop(void) -- this is the loop that u8g apparently needs for display disp:firstPage() repeat draw() until disp:nextPage() == false end -- below is to set up the timer to keep hitting the display init_spi_display() tmr.alarm(3,1000,1,function() disp:firstPage() repeat draw() until disp:nextPage() == false end) |
Example 3
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 |
function init_spi_display() local cs = 8 -- GPIO15, pull-down 10k to GND local dc = 4 -- GPIO2 local res = 0 -- GPIO16 spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0) disp = u8g.ssd1306_128x64_spi(cs, dc, res) end function draw() disp:setFont(u8g.font_6x10) disp:drawStr( 30, 10, "Hello IoT!") disp:drawStr( 20, 50, "www.htlinux.com") disp:drawLine(0, 25, 128, 25); disp:setFont(u8g.font_chikita) disp:drawStr( 20, 40, "Hello welcome users") end function display() disp:firstPage() repeat draw() until disp:nextPage() == false display(); end init_spi_display() display() |
Have a happy coding!!