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 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).
Once the download is complete, change into the directory
raspberrypi-linux and check out the Kernel’s source.
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:
Then export the Kernel’s configurations.
And then transfer the
config file directly to the server.
For now we are done with the Pi, so simply terminate the connection like so:
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:
To copy instead of move simply replace
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.
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 '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)  (NEW) 0
ST7735 SPI bus chipselect (FB_ST7735_MAP_SPI_BUS_CS)  (NEW) 0
ST7735 SPI bus clock speed (Hz) (FB_ST7735_MAP_SPI_BUS_SPEED)  (NEW) 32000000
ST7735 SPI bus mode (0, 1, 2, or 3) (FB_ST7735_MAP_SPI_BUS_MODE)  (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
w to search strings in the file and look for
CONFIG_SPI_BCM2708. Ensure this setting is set to
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_DETECT_PRIMARY is not set
<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:
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:
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.
Then compile the modules into this directory.
And then clean the directory by removing all temporary files.
Install the Kernel and its modules
Just like before, connect to your Raspberry Pi and delete the currently installed modules and 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.
And finally, copy over the compiled Kernel. This needs to be copied into the Raspberry’s
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.
Once Raspbian has rebooted and you reconnected to your Pi, you can check whether the frame buffer device-files have been generated.
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
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:
When you reboot the Pi you should see the standard 2×2 image like so:
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:
At the end of the 1st (and only) line in this document you need to add the following parameters:
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
o you need to reboot the Pi with:
First you need to update the Raspberry’s packages:
Next, you can install the Linux frame-buffer image viewer:
And finally display a graphic of resolution 128×160 with the following command:
The image I choose is this one:
And the result is:
First you need to create a new Python script file.
Then write the following lines into the file.
# -*- coding: utf-8 -*-
os.environ["SDL_FBDEV"] = "/dev/fb1"
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
elif line == 2:
textpos.centerx = 61
elif line == 3:
textpos.centerx = 25
size = width,height = 128,160
screen = pygame.display.set_mode(size)
if __name__ == '__main__':
Next, you need to make the script executable.
And in the end, you can run the script.
The result should look something like this: