Introduction: TTGO (color) Display With Micropython (TTGO T-display)

The TTGO T-Display is a board based on the ESP32 that includes a 1.14 inch color display. The board can be bought for a prize of less than 7$ (including shipping, prize seen on banggood). That's an incredible prize for an ESP32 including a display.

This could be the perfect base for your next project. Unfourtunatly, there is only offical support for arduino.

Micropython has several advantages, it makes developping a project a lot easier. I won't explain them all here. There are a lot of nice youtube videos about how micropyhton makes your life easier and the cool things you can do with it.

Unfortunatly, the offical support covers only the "arduino language".

In this tutorial I will show you how to use the board with micropython.

Step 1: Install Loboris Firmware on the Board

The offical micropython has no support for this kind of display. You have to install loboris micropython. This is a very cool modified micropython with additonal libaries.

To install, simply follow my (easy) Step-by-Step tutorial on

https://www.instructables.com/id/Installing-Loboris-lobo-Micropython-on-ESP32-With-/

Step 2: Load the Sample Code

Connect your board with uPyCraft as described in the installation tutorial. So as described, when you have started uPyCraft, you click tools->Serial->COM7 (replace COM7 with the port you found during the installation tutorial). Now there might opens a window with "Flash firmware" or something like that. Don't do that! This is an error. Simply close the "flash firmware" window and connect again with tools->Serial->COM7. Now you should see ">>>" in your command window. This means you connected sucessfully.

Now you open device (on the left side) and doubleclick on "main.py". If this files dosen't exist, create it. Paste the following code in the window:

import machine, display, time, math, network, utime


tft = display.TFT() tft.init(tft.ST7789,bgr=False,rot=tft.LANDSCAPE, miso=17,backl_pin=4,backl_on=1, mosi=19, clk=18, cs=5, dc=16)

tft.setwin(40,52,320,240)

for i in range(0,241):

color=0xFFFFFF-tft.hsb2rgb(i/241*360, 1, 1)

tft.line(i,0,i,135,color)

tft.set_fg(0x000000)

tft.ellipse(120,67,120,67)

tft.line(0,0,240,135)

text="ST7789 with micropython!"

tft.text(120-int(tft.textWidth(text)/2),67-int(tft.fontSize()[1]/2),text,0xFFFFFF)


wifi=network.WLAN(network.STA_IF) wifi.active(True) wifi.connect("yourWlan","yourPassword") utime.sleep_ms(3000) network.telnet.start(user="m",password="m")

It is important to have the indentation for the two lines under the "for" statment. Check the picture to see if it looks similar.

Now it's time to run it! Save the file, and press F5 (or click on the Tools->downloadAndRun). You should now see something on your display. If it is something strange, you have to press the reset button on the side of the board. Your display should now look like on the picture.

In the next steps I will explain the code.

Step 3: Initialize

Ok, you have already everything you need. Now I will go step by step trough the code and explain it.

Please look also on the very nice documentation on

https://github.com/loboris/MicroPython_ESP32_psRAM...

for additional info

import machine, display, time, math, network, utime

tft = display.TFT()

tft.init(tft.ST7789,bgr=False,rot=tft.LANDSCAPE, miso=17,backl_pin=4,backl_on=1, mosi=19, clk=18, cs=5, dc=16)

tft.setwin(40,52,320,240)

The imports should be clear. The third line intitialises the display.

If you want to turn off the display, simply run the tft.init again with backl_on=0

The fourth line sets the display border. This is necessary because the implementation isn't for this exact display. If you don't do this, you draw outside of the display.

The area you can use is (0,0,135,240)

Step 4: Drawing and Color

The important thing first: The colors are inverted! You propably now RGB (how colors are expressed with numbers). Red would normally be 0xFF0000. But here, for red you have to use 0x00FFFF. For blue you have to use 0xFFFF00 etc.

Loboris has color constants. They are inverted too. If you want to use them you can convert them:

tft.set_bg(0xFFFFFF - tft.BLUE)

tft.clear()

This fills the screen blue. Subtracting the colors you want from 0xFFFFFF converts them and you get the color you want.


for i in range(0,241):
color=0xFFFFFF-tft.hsb2rgb(i/241*360, 1, 1)

tft.line(i,0,i,135,color)

tft.set_fg(0x000000)

tft.ellipse(120,67,120,67)

tft.line(0,0,240,135) text="ST7789 with micropython!" tft.text(120-int(tft.textWidth(text)/2),67-int(tft.fontSize()[1]/2),text,0xFFFFFF)

The for-loop creates the hue color gradient. The we select the color we want to draw with (white) and we draw a ellipse, a line and a text.

Step 5: Telnet


wifi=network.WLAN(network.STA_IF)
wifi.active(True)

wifi.connect("yourWlan","yourPassword")

utime.sleep_ms(3000)

network.telnet.start(user="m",password="m")

This starts the telnet sever. You can acces the board over your WLAN without connecting it via USB! This is very useful if you have a project where you can't esaly access your board.

To access over WLAN you have to first find out what the IP of the board is. Connect your board via USB. Change "yourWlan" with your the name of your WLAN and "yourPassword" with your password. Download and run. Enter "network.telnet.status()" in the command window and press enter. It should return the IP of the board.

I recommand you the tool "Putty" to access via telnet. It's freeware. Download, install and open it. Select telnet for connection type, enter the IP you found and click open. A window opens. Login as "m", press enter. It asked for the password. Press m and enter. Now you should be connected via telnet.

Step 6: Do Your Own Amazing Project

That's it! With this code as base you can build your own amazing project.

A few things to mention:

-Loboris offers also the possibility to set up an ftp server. With this and telnet you can upload or modify code and run it. Completly manage the code from your outside weather station from your couch without any physical connection. Amazing!

-Loboris has a very nice Wiki:

https://github.com/loboris/MicroPython_ESP32_psRAM...

Direct link to the display Wiki:

https://github.com/loboris/MicroPython_ESP32_psRAM...

Direct link to the telnet Wiki:

https://github.com/loboris/MicroPython_ESP32_psRAM...

I hope that you found this tutorial useful. Leave your comment and show us what you did with the TTGO and micropython