720×720 LCD screen using DPI on Raspberry Pi 4

Some weeks ago, I bought a 4inch square LCD screen with 720×720 pixels from Waveshare. However, they did not provide any documentation at all for this specific display, but there was more than enough documentation for all the other displays, e.g. this rectangular one. Thus, I decided to document here how to get this display running in the correct configuration.

Some basics

In context with LCDs on Linux and specifically the Raspberry Pi, you will come across many abreviations and terms such as DPI, dtbo files, IPS, LCD.

Fortunately, Waveshare makes it quite easy to setup the display and you don’t need to know anything about these terms. However, for those that want to know the background, you can read about these details later. I will first show how to get the LCD running.

Getting the LCD to work on Raspberry Pi 4

  1. First, you need to download some files provided by Waveshare for their 4inch displays. These are device tree blob overlays modifying the kernel device tree to set the GPIO pins in the required state for the LCD to work.
  2. All three .dtbo files need to be copied to /boot/overlays/
  3. Now comes the interesting part: Telling the Raspberry Pi to actually use the display. For this, edit /boot/config.txt with the nano editor using
sudo nano /boot/config.txt

4. Input the following configuration:

gpio=0-9=a2
gpio=12-17=a2
gpio=20-25-=a2
dtoverlay=dpi24
enable_dpi_lcd=1
display_default_lcd=1
extra_transpose_buffer=2
dpi_group=2
dpi_mode=87
dpi_output_format=0x7f216
dpi_timings=720 0 40 48 128 720 0 13 3 15 0 0 0 60 0 41000000 4
dtoverlay=waveshare-4dpib-3b-4b
dtoverlay=waveshare-4dpib-3b
dtoverlay=waveshare-4dpib-4b
  1. Save the file using Ctrl+O and exit the editor using CTRL+X. Reboot using sudo reboot. Now you should already see the startup messages coming through on the screen. Nice!

Background on DPI, dtbo files, IPS, LCD

What is DPI?

DPI is short for Display Parallel Interface. As the name suggests, it allows to connect a display to a computer using many parallel wires for data transmission.

On the Raspberry Pi, the DPI allows for up to 24 bit of parallel RGB data transmission using the 40-pin header. The following kinds of displays can be attached to this interface:

  • RGB24 (3*8 bits in total, 8 bits or each color of red, green, blue)
  • RGB666 (3*6 bits for RGB)
  • RGB565 (5 red, 6 green, 5 blue)

To activate this interface, the Raspberry Pi GPIOs have to be configured to use an alternate function (ALT2). This is achieved using the entries in the /boot/config.txt as mentioned above, using the lines

gpio=0-9=a2
gpio=12-17=a2
gpio=20-25-=a2

More details can be found here: https://www.raspberrypi.org/documentation/hardware/raspberrypi/dpi/

What do these dpi_timings mean that we just configured?

dpi_timings=720 0 40 48 128 720 0 13 3 15 0 0 0 60 0 41000000 4

These values describe the properties of the diplay attached:

720 = h_active_pixels = width of the display (number of horizontal pixels)
0 = h_sync_polarity = Polarity of horizontal sync (0 = invert hsync polarity)
40 = h_front_porch = horizontal front porch (how many pixels are padded after the active horizontal edge and before the actual display data starts)
48 = h_sync_pulse = width of the horizontal sync pulse (in pixel clocks)
128 = h_back_porch = horizontal back porch (padding from the active edge)
720 = v_active_lines = number of vertical pixel rows (lines)
0 = v_sync_polarity = polarity of the vertical sync signal (0 = invert vsync polarity)
13 = v_front_porch = vertical forward padding from the active edge
3 = v_sync_pulse = width of the vertical sync pulse (in pixel clocks)
15 = v_back_porch = vertical back padding from the active edge
0 = v_sync_offset_a = offset of the vertical sync A
0 = v_sync_offset_b = offset of the vertical sync B
0 = pixel_rep = Number of pixel repetitions
60 = frame_rate = refresh rate of the screen in Hz ("fps")
0 = interlaced = Determines, if interlaced mode shouldbe used
41000000 = pixel_freq = required clock frequency of the display (width*height*framerate)
4 = aspect_ratio = Possible values: 1 = 4:3, 2 = 14:9, 3 = 16:9, 4 = 5:4, 5 = 16:10, 6 = 15:9, 7 = 21:9, 8 = 64:27

Leave a Reply