Connecting 1.8" TFT LCD to Raspberry Pi

Some time ago I stumbled over a little TFT LCD from SainSmart (link) and since it was lying next to a Raspberry Pi stack I decided to buy it. Ever since it was just sitting on my shelf waiting to be used whilst I do my doctorate. But now I found a free night and a free morning in my busy schedule. So I decided to try and connect it to one of my Raspberries and I must say, I got it to work rather quickly (roughly 5h).

First of all, all of the kernel credit must go to Kamal Mostafa and his git repository, since it was him who made the drivers available. Throughout this little explanation I will detail how I acquired, configured, compiled and installed the raspberry-linux kernel. Once that's done I will show you how I got my Raspberry Pi to use the screen as a video output.

First though, you will require a linux distribution to execute all of the commands. You can use an already set up Raspberry Pi, but compilation of the Kernel apparently takes around 5.5h. So unless you are willing to wait that long, you're better of installing Ubuntu (it's free and worked for me).  In my case I have a ubuntu server running on the IP 192.168.0.2 and my Raspberry Pi on the IP 192.168.0.3. My username on the server is root and on the Pi pi (of course these are not the actual credentials, but I list these so it becomes easier to follow the commands). So let's get started.

Get the Kernel

To control the display you will need to recompile the Raspbian Kernel. But before you can do so you will need to install some programs on your linux distribution.

root@ubuntu1210: ~$ sudo apt-get update
root@ubuntu1210: ~$ sudo apt-get install gcc-arm-linux-gnueabi make git-core ncurses-dev

Next you need to download the Kernel's source code. This is where you get Kamal Mostafa's source, since he modified it for the SainSmart TFT LCD (ST7735R).

root@ubuntu1210: ~$ git clone git://github.com/kamalmostafa/raspberrypi-linux.git

Once the download is complete, change into the directory raspberrypi-linux and check out the Kernel's source.

root@ubuntu1210: ~$ cd raspberrypi-linux
root@ubuntu1210: ~/raspberrypi-linux$ git checkout rpi-3.6.y+kamal-st7735fb

Kernel configuration export

Now, you'll need to switch to your Raspberry Pi and export its Kernel configurations. Since my linux distribution and your Raspberry Pi were connected as outlined above, I could simply ssh into my Pi and transfer the files to my linux distribution. In your case simply amend IPs, directories and usernames (incl. their passwords) to suit your setup. Nevertheless, you first need to connect to your Pi:

root@ubuntu1210: ~/raspberrypi-linux$ ssh pi@192.168.0.3

Then export the Kernel's configurations.

pi@raspberrypi ~ $ sudo zcat /proc/config.gz > config

And then transfer the config file directly to the server.

pi@raspberrypi ~ $ scp config root@192.168.0.2:/home/root

For now we are done with the Pi, so simply terminate the connection like so:

pi@raspberrypi ~ $ exit

Modify the Kernel

Back at the linux distribution, you then need to move (or copy if you want to have a backup) the config file into Kernel source code. Since you will have to rename the file to .config let's do that at the same time:

root@ubuntu1210: ~/raspberrypi-linux$ mv ~/config ~/raspberrypi-linux/.config

To copy instead of move simply replace mv with cp.

Next you need to export the ST7735 Chip's properties since they are missing in the original configurations file. This is done with the command make which we installed earlier.

root@ubuntu1210: ~/raspberrypi-linux$ make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig

A lot of options will pop up and ask you how you want to setup the configurations. Simply hit enter (i.e. accept the default) for all of them, until you reach the configurations for the SainSmart module. Answer these questions as follows:

ST7735 LCD panel framebuffer support (FB_ST7735) [N/m/y/?] (NEW) y
ST7735 'red tab' panel type? (FB_ST7735_PANEL_TYPE_RED_TAB) [Y/n/?] (NEW) y
ST7735 panel has reversed RGB? (FB_ST7735_RGB_ORDER_REVERSED) [N/y/?] (NEW) y
ST7735 framebuffer mapping to GPIO/SPI (FB_ST7735_MAP) [Y/n/m/?] (NEW) y
ST7735 RST gpio pin number (FB_ST7735_MAP_RST_GPIO) [-1] (NEW) 25
ST7735 D/C gpio pin number (FB_ST7735_MAP_DC_GPIO) [-1] (NEW) 24
ST7735 SPI bus number (FB_ST7735_MAP_SPI_BUS_NUM) [0] (NEW) 0
ST7735 SPI bus chipselect (FB_ST7735_MAP_SPI_BUS_CS) [0] (NEW) 0
ST7735 SPI bus clock speed (Hz) (FB_ST7735_MAP_SPI_BUS_SPEED) [4000000] (NEW) 32000000
ST7735 SPI bus mode (0, 1, 2, or 3) (FB_ST7735_MAP_SPI_BUS_MODE) [0] (NEW) 0

The configuration has now been enhanced with make, but you still need to do some manual changes. To do so, open the .config file with nano.

root@ubuntu1210: ~/raspberrypi-linux$ nano .config

Use ctrl+w to search strings in the file and look for CONFIG_SPI_BCM2708. Ensure this setting is set to yes i.e.:

CONFIG_SPI_BCM2708=y

Next search for the following configurations and change them accordingly. Initially they were commented out, so just search for the parameters names and change the rows to look like so:

CONFIG_FRAMEBUFFER_CONSOLE=y
# CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY is not set
CONFIG_FRAMEBUFFER_CONSOLE_ROTATION=y
CONFIG_FONTS=y
CONFIG_FONT_8x8=y
CONFIG_FONT_8x16=y
CONFIG_LOGO=y
<span style="line-height: 1.5em;">

Compile the Kernel

Now you are ready to compile the Kernel. As I said, doing this on a Pi will take more than 5.5h (even when overclocked). If you have a single core system then use the following command for compilation:

root@ubuntu1210: ~/raspberrypi-linux$ make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k

On a multicore system, you can append the -j flag to utilise as many resources as possible for compiling; hence speeding up the process. I have a dual-core server, so the command I used was:

root@ubuntu1210: ~/raspberrypi-linux$ make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k -j 3

If you have a quad-core system change the -j 3 to -j 5. And now wait... (It took me 17 minutes to compile)

Compile Kernel Modules

First create a directory for your modules.

root@ubuntu1210: ~/raspberrypi-linux$ mkdir ./modules

Then compile the modules into this directory.

root@ubuntu1210: ~/raspberrypi-linux$ make modules_install ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- INSTALL_MOD_PATH=./modules/

And then clean the directory by removing all temporary files.

root@ubuntu1210: ~/raspberrypi-linux$ rm -rf ./modules/lib/modules/3.6.11+/build ./modules/lib/modules/3.6.11+/source

Install the Kernel and its modules

Just like before, connect to your Raspberry Pi and delete the currently installed modules and firmware.

pi@raspberrypi ~ $ sudo rm -rf /lib/modules /lib/firmware

Then you need to copy the previously compiled modules ti your Pi. I did this just like before but instead of scping from my Pi to linux, I copied from linux to my Pi.

pi@raspberrypi ~ $ scp root@192.168.0.2:/home/root/raspberrypi-linux/modules/lib /

And finally, copy over the compiled Kernel. This needs to be copied into the Raspberry's boot directory.

pi@raspberrypi ~ $ scp root@192.168.0.2:/home/root/raspberrypi-linux/arch/arm/boot/Image /boot/kernel.img

Load the Kernel

After having swapped the Kernel as well as its modules, a reboot is required to load them into your OS. When doing so (as you may expect) the connection to the Pi will be lost. Hence reconnect once the Pi rebooted. You can trigger a reboot with the following command.

pi@raspberrypi ~ $ sudo shutdown -t now

Once Raspbian has rebooted and you reconnected to your Pi, you can check whether the frame buffer device-files have been generated.

pi@raspberrypi ~ $ ls -la /dev/fb*
crw-rw---T 1 root video 29, 0 Jan 1 1970 /dev/fb0
crw-rw---T 1 root video 29, 1 Jan 1 1970 /dev/fb1

The hardware

Now it's time to connect the SainSmart TFT LCD module to the Pi. I recommend you shut down the Pi before connecting the screen to avoid damaging the hardware. Then connect the screen as follows:

SainSmart-TFT-to-PI

When you reboot the Pi you should see the standard 2x2 image like so:

IMG_1221

 

Applications

Linux Terminal

To get a terminal output to your SinSmart 1.8" TFT LCD module, you need to edit the cmdline.txt file of the raspberry. Either log in via ssh (like above) or modify the file on the SD-Card. I chose the former and did this:

pi@raspberrypi ~ $ nano /boot/cmdline.txt

At the end of the 1st (and only) line in this document you need to add the following parameters:

fbcon=map:10 fbcon=rotate:1 fbcon=font:ProFont6x11

The parameter rotate can be modified to adjust the screen rotation like so:

  • 0: Normal orientation
  • 1: 90° rotation
  • 2: 180° rotation
  • 3: 270° rotation

After you saved the file with ctrl+o you need to reboot the Pi with:

pi@raspberrypi ~ $ sudo shutdown -r now

IMG_1222

Display graphics

First you need to update the Raspberry's packages:

pi@raspberrypi ~ $ sudo apt-get update

Next, you can install the Linux frame-buffer image viewer:

pi@raspberrypi ~ $ sudo apt-get install fbi

And finally display a graphic of resolution 128x160 with the following command:

pi@raspberrypi ~ $ sudo fbi -noverbose -T 1 -a -d /dev/fb1 raspberry-test-image.jpg

The image I choose is this one:

raspberry-test-image

And the result is:

IMG_1223

Python clock

First you need to create a new Python script file.

pi@raspberrypi ~ $ nano clock.py

Then write the following lines into the file.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import time
import pygame

time_stamp_prev=0

os.environ["SDL_FBDEV"] = "/dev/fb1"
os.environ['SDL_VIDEODRIVER']="fbcon"

def displaytext(text,size,line,color,clearscreen):
    if clearscreen:
        screen.fill((0,0,0))

    font = pygame.font.Font(None,size)
    text = font.render(text,0,color)
    rotated = pygame.transform.rotate(text,90)
    textpos = rotated.get_rect()
    textpos.centery = 80
    if line == 1:
        textpos.centerx = 99
        screen.blit(rotated,textpos)
    elif line == 2:
        textpos.centerx = 61
        screen.blit(rotated,textpos)
    elif line == 3:
        textpos.centerx = 25
        screen.blit(rotated,textpos)

def main():
    global screen

    pygame.init()
    pygame.mouse.set_visible(0)
    size = width,height = 128,160
    screen = pygame.display.set_mode(size)

    while True:
        displaytext(time.strftime("%d.%m.%Y",time.gmtime()),40,1,(255,255,255),True)
        displaytext(time.strftime("%H:%M:%S",time.gmtime()),40,2,(255,255,255),False)
        displaytext("gerfficient.com",20,3,(100,100,255),False)
        pygame.display.flip()
        time.sleep(1)

if __name__ == '__main__':
    main()

Next, you need to make the script executable.

pi@raspberrypi ~ $ chmod 0755 clock.py

And in the end, you can run the script.

pi@raspberrypi ~ $ sudo ./clock.py

The result should look something like this:

IMG_1224

One Response to “ Connecting 1.8" TFT LCD to Raspberry Pi ”

  1. Very instructive I'll try the next weekend. Thak you very much for sharing this information and mostly for spend your time writing this clever tutorial.

Leave a Reply

*
*