Skip to content

Instantly share code, notes, and snippets.

@sdalu
Last active August 7, 2022 13:33
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sdalu/4fd108e0a96d6b41d52fde7542cc95ce to your computer and use it in GitHub Desktop.
Save sdalu/4fd108e0a96d6b41d52fde7542cc95ce to your computer and use it in GitHub Desktop.
LCD display HAT for Raspberry Pi

For the LCD/Joystick HAT: https://www.waveshare.com/1.44inch-lcd-hat.htm

PIN Mapping:

Symbol Raspberry Pi PIN (BCM) Description
KEY1 P21 Button 1/GPIO
KEY2 P20 Button 2/GPIO
KEY3 P16 Button 3/GPIO
Joystick Up P6 Joystick Up
Joystick Down P19 Joystick Down
Joystick Left P5 Joystick Left
Joystick Right P26 Joystick Right
Joystick Press  P13 Joystick Press
SCLK P11/SCLK SPI clock input
MOSI P10/MOSI SPI data input
 DC P25 Data/Command selection (high for data, low for command)
CS P8/CE0 Chip selection, low active
RST P27 Reset, low active
BL P24 Backlight

LCD Display

Configuration

  1. Ensure that packages are installed:
apt-get install fbset raspi-config raspi-gpio
  1. Enable SPI:
raspi-config  # Select interface / SPI / Enable
  1. Load the following modules at boot-time, by adding these lines in /etc/modules
spi-bcm2835
fbtft_device
  1. Provide configuration for the fbtft device, by creating the file /etc/modprobe.d/fbtft.conf:
options fbtft_device name=adafruit18_green gpios=reset:27,dc:25,cs:8,led:24 speed=40000000 bgr=1 fps=60 custom=1 height=128 width=128 rotate=180
  1. Reboot

  2. You should get a new device called fb1

ls /dev/fb*

Using it

echo 0 > /sys/class/backlight/fb_st7735r/bl_power  # On
echo 1 > /sys/class/backlight/fb_st7735r/bl_power  # Off
fbset -i -fb /dev/fb1
mode "128x128"
    geometry 128 128 128 128 16
    timings 0 0 0 0 0 0 0
    nonstd 1
    rgba 5/11,6/5,5/0,0/0
endmode

Frame buffer device information:
    Name        : fb_st7735r
    Address     : 0
    Size        : 32768
    Type        : PACKED PIXELS
    Visual      : TRUECOLOR
    XPanStep    : 0
    YPanStep    : 0
    YWrapStep   : 0
    LineLength  : 256
    Accelerator : No
ffmpeg -v 0 -y       \
    -i imagefile.png \
    -vf scale=128:128 -vcodec rawvideo -f rawvideo -pix_fmt rgb565 - > /dev/fb1

Keys / Joystick

With gpio-keys

In file /boot/config.txt

# KEY 1/2/3
dtoverlay=gpio-key,gpio=21,gpio_pull=up,active_low=1,label=key1,keycode=1
dtoverlay=gpio-key,gpio=20,gpio_pull=up,active_low=1,label=key2,keycode=2
dtoverlay=gpio-key,gpio=16,gpio_pull=up,active_low=1,label=key3,keycode=3

# Joystick
dtoverlay=gpio-key,gpio=6,gpio_pull=up,active_low=1,label=up,keycode=103
dtoverlay=gpio-key,gpio=19,gpio_pull=up,active_low=1,label=down,keycode=108
dtoverlay=gpio-key,gpio=5,gpio_pull=up,active_low=1,label=left,keycode=105
dtoverlay=gpio-key,gpio=26,gpio_pull=up,active_low=1,label=right,keycode=106
dtoverlay=gpio-key,gpio=13,gpio_pull=up,active_low=1,label=enter,keycode=28

With sysfs

for pin in 21 20 16 6 19 5 26 13 ; do
  raspi-gpio set $pin pu
  echo $pin > /sys/class/gpio/export
done
cat /sys/class/gpio/gpio$pin/value
Copy link

ghost commented Jun 29, 2018

thx, good work.

@petrmachcz
Copy link

Visible screen is only 128 x 127 pixels.

@Martenz
Copy link

Martenz commented Nov 25, 2020

Mine is still full white .. and no fb1 detected.. using a RASPBERRY PI OS LITE only cmd no ddesktop ... any idea??

@tsbarnes
Copy link

tsbarnes commented Oct 6, 2021

Mine is still full white .. and no fb1 detected.. using a RASPBERRY PI OS LITE only cmd no ddesktop ... any idea??

For a little while now, the Raspberry Pi OS kernel hasn't included the fbtft_device module, so unless you use an older kernel this guide won't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment