Saturday, 24 September 2016

Using an ST7735 LCD screen with NodeMCU

On a previous post Somchai asked how to hook up an ST7735 LCD screen to his WeMos D1 Mini and use it with the NodeMCU firmware.

The NodeMCU platform (and compatibles like the WeMos) are a great way to get started in the Internet of Things.

See my Introductory Article for how to get started in IoT with NodeMCU.

Here's how to connect and use this inexpensive LCD screen:.

Connecting an ST7735 LCD to NodeMCU

From your du Pont cable set peel off nine male-to-female cables, I suggest brown,red,orange,yellow,green,blue,purple,white,brown (these are contiguous in my set).

Connect like this at the screen:

  • brown to GND
  • Red to VCC
  • Blue to RESET
  • Purple to A0
  • Yellow to SDA
  • Orange to SCK
  • Green to CS
  • White to LED+
  • Grey to LED-

Connecting an ST7735 LCD to NodeMCU

At the NodeMCU connect like this:

  • Brown to GND
  • Red to 3V3
  • Blue to D0
  • Purple to D4
  • Yellow to D7
  • Orange to D5
  • Green to D8
  • White to 3V3
  • Grey to GND

Connecting an ST7735 LCD to NodeMCU

Building your firmware

Using the NodeMCU cloud build service I made a firmware image that contained the modules I needed:

building nodeMCU firwmare for LCD

I installed it using esptool:

chris$ --port /dev/tty.nodemcu-DevB write_flash -fs 32m 0x00000 nodemcu-master-17-modules-2016-09-23-22-47-56-float.bin v1.1
Running Cesanta flasher stub...
Flash params set to 0x0040
Writing 520192 @ 0x0... 520192 (100 %)
Wrote 520192 bytes at 0x0 in 59.1 seconds (70.4 kbit/s)...

And when booted it shows the modules present. You must have SPI, ucg and bit modules, the others are up to you.

NodeMCU custom build by
 branch: master
 commit: 8e48483c825dea9c12b37a4db3d034fccbcba0bf
 SSL: true
 modules: bit,cjson,file,gpio,http,mqtt,net,node,pwm,rtcmem,rtctime,spi,tmr,uart,ucg,wifi,ws2812
 build  built on: 2016-09-23 22:46
 powered by Lua 5.1.4 on SDK

Testing your screen

To test your screen, upload all the files from the NodeMCU ucglib example directory. You can use the GUI ESPlorer tool or the command-line NodeMCU-uploader tool to copy the files.

chris$ nodemcu-uploader --port /dev/tty.nodemcu-DevB upload *
opening port /dev/tty.nodemcu-DevB with 115200 baud
Preparing esp for transfer.
Transferring GT_box.lua as GT_box.lua
Transferring GT_clip.lua as GT_clip.lua
Transferring GT_color_test.lua as GT_color_test.lua
Transferring GT_cross.lua as GT_cross.lua
Transferring GT_fonts.lua as GT_fonts.lua
Transferring GT_gradient.lua as GT_gradient.lua
Transferring GT_graphics_test.lua as GT_graphics_test.lua
Transferring GT_pixel_and_lines.lua as GT_pixel_and_lines.lua
Transferring GT_text.lua as GT_text.lua
Transferring GT_triangle.lua as GT_triangle.lua
Transferring GraphicsTest.lua as GraphicsTest.lua
Transferring HelloWorld.lua as HelloWorld.lua
Transferring UcgLogo.lua as UcgLogo.lua
All done!

Then run the demo by typing dofile("GraphicsTest.lua"), or using the GUI or the CLI to invoke it

chris$ nodemcu-uploader --port /dev/tty.nodemcu-DevB file do GraphicsTest.lua
opening port /dev/tty.nodemcu-DevB with 115200 baud
Executing GraphicsTest.lua

It happens that the GraphicsTest.lua file defaults to using using the ST7735 screen. If you have a different supported screen, make sure that you select that screen when you build your firmware, and that you edit GraphicsTest.lua to call the setup routine for your screen.

Connecting a WeMos D1 Mini

If you have a WeMos D1 Mini, the connections are the same:

Connecting an ST7735 LCD to a WeMos D1 Mini

Using the screen in your own code

In your own code, I've found you can connect blue (reset) to the NodeMCU reset and save a pin.

My setup routine looks like this:

-- setup SPI and connect display
function screen_init()
    -- Hardware SPI CLK  = GPIO14 = D5 orange
    -- Hardware SPI MISO = GPIO12 = D6 (not used)
    -- Hardware SPI MOSI = GPIO13 = D7 yellow
    -- CS, D/C, and RES can be assigned freely to available GPIOs
    local cs  = 8 -- GPIO15 = D8 green (pull-down 10k to GND)
    local dc  = 4 -- GPIO2  = D4 purple
    -- local res = NC -- RST (blue)

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

    -- initialize the matching driver for your display
    -- see app/include/ucg_config.h
    --disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res)
    disp = ucg.st7735_18x128x160_hw_spi(cs, dc)

    disp:setColor(255, 255, 255)
    disp:setColor(1, 255, 0,0)

No comments: