NodeMCU + OLED SSD1306

NodeMCU (LUA)

This page is to Setup and Config NodeMCU + OLED SSD1306

Hardware and Software

Hardware

NodeMCU + OLED SSD1306

Software

Arduino IDE

Notes

    • Firmware must with PIN

    • PIN CONTROL

    • MISO - GPIO12 (Not Used)

    • CS -- GPIO13, pull-down 10K to GND (ANY PIN)

Examples

This example is lua file for esp01 or nodemcu lua flasher formated

            1. -- ***************************************************************************

            2. -- Graphics Test

            3. --

            4. -- This script executes several features of u8glib to test their Lua bindings.

            5. --

            6. -- Note: It is prepared for SSD1306-based displays. Select your connectivity

            7. -- type by calling either init_i2c_display() or init_spi_display() at

            8. -- the bottom of this file.

            9. --

            10. -- ***************************************************************************

            11. -- setup I2c and connect display

            12. function init_i2c_display()

            13. -- SDA and SCL can be assigned freely to available GPIOs

            14. local sda = 5 -- GPIO14

            15. local scl = 6 -- GPIO12

            16. local sla = 0x3c -- SANKI20160530 0x27 (old 0x3c)

            17. i2c.setup(0, sda, scl, i2c.SLOW)

            18. disp = u8g.ssd1306_128x64_i2c(sla)

            19. end

            20. -- setup SPI and connect display

            21. function init_spi_display()

            22. -- Hardware SPI CLK = GPIO14

            23. -- Hardware SPI MOSI = GPIO13

            24. -- Hardware SPI MISO = GPIO12 (not used)

            25. -- CS, D/C, and RES can be assigned freely to available GPIOs

            26. local cs = 8 -- GPIO15, pull-down 10k to GND

            27. local dc = 4 -- GPIO2

            28. local res = 0 -- GPIO16

            29. spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)

            30. disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res)

            31. end

            32. -- graphic test components

            33. function prepare()

            34. disp:setFont(u8g.font_6x10)

            35. disp:setFontRefHeightExtendedText()

            36. disp:setDefaultForegroundColor()

            37. disp:setFontPosTop()

            38. end

            39. function box_frame(a)

            40. disp:drawStr(0, 0, "drawBox")

            41. disp:drawBox(5, 10, 20, 10)

            42. disp:drawBox(10+a, 15, 30, 7)

            43. disp:drawStr(0, 30, "drawFrame")

            44. disp:drawFrame(5, 10+30, 20, 10)

            45. disp:drawFrame(10+a, 15+30, 30, 7)

            46. end

            47. function disc_circle(a)

            48. disp:drawStr(0, 0, "drawDisc")

            49. disp:drawDisc(10, 18, 9)

            50. disp:drawDisc(24+a, 16, 7)

            51. disp:drawStr(0, 30, "drawCircle")

            52. disp:drawCircle(10, 18+30, 9)

            53. disp:drawCircle(24+a, 16+30, 7)

            54. end

            55. function r_frame(a)

            56. disp:drawStr(0, 0, "drawRFrame/Box")

            57. disp:drawRFrame(5, 10, 40, 30, a+1)

            58. disp:drawRBox(50, 10, 25, 40, a+1)

            59. end

            60. function stringtest(a)

            61. disp:drawStr(30+a, 31, " 0")

            62. disp:drawStr90(30, 31+a, " 90")

            63. disp:drawStr180(30-a, 31, " 180")

            64. disp:drawStr270(30, 31-a, " 270")

            65. end

            66. function line(a)

            67. disp:drawStr(0, 0, "drawLine")

            68. disp:drawLine(7+a, 10, 40, 55)

            69. disp:drawLine(7+a*2, 10, 60, 55)

            70. disp:drawLine(7+a*3, 10, 80, 55)

            71. disp:drawLine(7+a*4, 10, 100, 55)

            72. end

            73. function triangle(a)

            74. local offset = a

            75. disp:drawStr(0, 0, "drawTriangle")

            76. disp:drawTriangle(14,7, 45,30, 10,40)

            77. disp:drawTriangle(14+offset,7-offset, 45+offset,30-offset, 57+offset,10-offset)

            78. disp:drawTriangle(57+offset*2,10, 45+offset*2,30, 86+offset*2,53)

            79. disp:drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset)

            80. end

            81. function ascii_1()

            82. local x, y, s

            83. disp:drawStr(0, 0, "ASCII page 1")

            84. for y = 0, 5, 1 do

            85. for x = 0, 15, 1 do

            86. s = y*16 + x + 32

            87. disp:drawStr(x*7, y*10+10, string.char(s))

            88. end

            89. end

            90. end

            91. function extra_page(a)

            92. disp:drawStr(0, 12, "setScale2x2")

            93. disp:setScale2x2()

            94. disp:drawStr(0, 6+a, "setScale2x2")

            95. disp:undoScale()

            96. end

            97. -- the draw() routine

            98. function draw(draw_state)

            99. local component = bit.rshift(draw_state, 3)

            100. prepare()

            101. if (component == 0) then

            102. box_frame(bit.band(draw_state, 7))

            103. elseif (component == 1) then

            104. disc_circle(bit.band(draw_state, 7))

            105. elseif (component == 2) then

            106. r_frame(bit.band(draw_state, 7))

            107. elseif (component == 3) then

            108. stringtest(bit.band(draw_state, 7))

            109. elseif (component == 4) then

            110. line(bit.band(draw_state, 7))

            111. elseif (component == 5) then

            112. triangle(bit.band(draw_state, 7))

            113. elseif (component == 6) then

            114. ascii_1()

            115. elseif (component == 7) then

            116. extra_page(bit.band(draw_state, 7))

            117. end

            118. end

            119. function graphics_test()

            120. disp:firstPage()

            121. repeat

            122. draw(draw_state)

            123. until disp:nextPage() == false

            124. if (draw_state <= 7 + 8*8) then

            125. draw_state = draw_state + 1

            126. else

            127. print("--- Restarting Graphics Test ---")

            128. draw_state = 0

            129. end

            130. print("Heap: " .. node.heap())

            131. -- retrigger timer to give room for system housekeeping

            132. tmr.start(0)

            133. end

            134. draw_state = 0

            135. --init_i2c_display()

            136. init_spi_display()

            137. -- set up timer 0 with short interval, will be retriggered in graphics_test()

            138. tmr.register(0, 100, tmr.ALARM_SEMI, function() graphics_test() end)

            139. print("--- Starting Graphics Test ---")

            140. tmr.start(0)