Kedei 2 touch screen

Something I bought in 2015, 2 Kedei 2 lcd 3.5 inch touch screens. Worked, albeit slow.
With a Wheezy image, not to be updated due to the kernel doing its SPI thing to copy the framebuffer to the LCD screen via fbcopy.

March 2020, installed the Kedei screen on a Raspberry Pi V2 and inserted the archived SD card. It works!

Update? No, the official repository moved on. But there is a legacy repository!

Replace this line in /etc/apt/sources.list

deb wheezy main contrib non-free rpi

and teh apt-get update works (be it with some warnings).

To test I installed MC and that went allright (be it with some warnings).

Screen quality is not too bad, screen refreshes are slow. In console mode it reminds me of the 9600 baud serial terminals!

Now I want autologon, no option in old raspiconfig!


sudo nano /etc/inittab
Scroll down to:

1:2345:respawn:/sbin/getty 115200 tty1
and change to

#1:2345:respawn:/sbin/getty 115200 tty1
Under that line add:

1:2345:respawn:/bin/login -f pi tty1 /dev/tty1 2>&1

Auto StartX (Run LXDE)
In Terminal:

sudo nano /etc/rc.local
Scroll to the bottom and add the following above exit 0:

su -l pi -c startx


RTK.GPIO protocol

The RTK.GPIO is a microprocessor system with a serial interface, and a RPi 40 pin GIO connector.

USB – CH340 UART – STM cpu – RPi GPIO 40 pin
– power 3.3V via voltage regulator for CH340, STM and RPi GPIO
– power 5V to RPi GPIO

The RPi is electrical identical to the RPi (all signals at 3.3V I/O).

Of the RPi connectors the following is supported:
– GPIO connector, layout identical to Raspberry
– GND, +5V, +3.3V
– GPIO 0-27 I/O set for read, write, pullup pulldown
– I2C on GPIO 2 and 3 100kHZ smbus subset
– No SPI, no ALT functions, no PWM, no edge triggered interrupts, no UART/T etc
– I2C also in separate 4 pin connector with +5V, unsure if 3.3V or 5V I2C signals
– UAR/T connector 4 pin undocumented
– PROG connector undocumented

Serial protocol (based upon studying the firmware source main.cpp version RTk-2016-01-05-FINAL)
A simple and not very robust protocol, no ACK’s, just one error message E1, no complete error checking,
version command returns string without CRLF

– ASCII character based
– The CH340 and STM communicate serial over 230400 baud, no hardware or software handshake
– no block I/O, all GPIO via separate commands
– just one error message ‘E 1 CR LF’
– all can be tested with a serial terminal emulator like Putty etc for GPIO, all is readable ASCII
– I2C commands are readable ASCII, but the data characters are interpreted as 8 bit numbers
(no software handshake allowed!)

– GPIO numbers are the numbers on the RPi I/O connector.
– I2C is on GPIO2, GPIO3 with pullup resistors

After power on
– the RTK.GPIO sends the string ‘RTk.GPIO Ready’ (no CR LF!)
– all GPIO are Digital InOut

The RTK.GPIO accepts the following commands.

A command is one character followed by one or more characters as arguments, everything is in ASCII.


main flow 

  if command char in [A..Z] then 
    read (param_char)
    do_command(command_char, param_char)
  else if command_char in [a..|] then
    gpio(command_char, gpio_char)
  else if command_char in {CR, LF)
    do nothing
    send 'E' '1' CR LF
  until forever
do_commmand(command_char, param_char)

    V : send 'RTk.GPIO Ready' (note V requires a second char, can be any!)
	G : read(gpio_char)
	    do_GPIO(param_Char, GPIO_char)

    I : do_I2C(param_char)

gpio_command(pinc, cmdch)

GPIO Commands are two characters.
  <pinch> <cmdch>
pinch is the pin character (a..) where 'a' represents pin 0 (28 I/O's)
character is converted to number (nr = pin character - 'a'
cmdch is the command to perform on that pin:
  I: Set this pin to a digital input,        eg: aI
  O: Set this pin to a digital output,       eg: aO
  0: Write a digital low to this output,     eg: a0
  1: Write a digital high to this output,    eg: a1
  U: Write a pull input high,    eg: aU
  D: Write a pull input down,    eg: aD
  N: write a pull none           eg  aN
  ?: Read the state of this digital input,   eg: a?
     State is returned as pinch + state(0|1) 
     with CR LF added                        eg: a0 CR LF

 error if pin character out of range


in i2c all data chars are interpreted as integers, 
'0' is decimal i2c address 48 for example  
(make sure software handshake is off, otherwise the serial communication eats xon/xoff)

  read(i2c command)
  if i2c command = 'W' then
    read(length of i2c datastring)
    read all i2c datastring into i2c datablock array
    i2c.write(i2caddress, i2c datablock, lenght of i2c datablock)
  else if i2c command = 'R' then
    read(length of i2c datastring)
    read(first datablock data)
    i2c read(i2c addrss, i2c datablock, i2c datablock length)
    for all in i2c datablock do
      write datablock[count] to serial


rpi_hal from is an incredible rich and uptodate unit for Freepascal to use the I/O of the Raspberry.

From Freepascal programs all works as expected when run as root. Otherwise accessing /dev/mem is causing real problems.

The unit can be used from Lazarus also (avoid the test procedures with writeln’ 😉 ) but also require root access and setting cthreads in the program heading.

For X apps  running as root is not possible with just sudo.
Seems I found a solution here:

I applied method 2:

sudo nano /etc/profile

and added this line as last line

export XAUTHORITY=/home/pi/.Xauthority

Now i can do

$ sudo ./testinit (my simple test program with only the init line of rpi_hal)

and this worked without error, init succeeded.

If you want debugging the app in Lazarus, start Lazarus as root (and ignore all the warnings)

xhost +localhost
sudo /usr/bin/startlazarus

Original text from

Method 2: Globally in /etc/profile
Add the following line to /etc/profile:

export XAUTHORITY=/home/username/.Xauthority
This will permanently allow root to connect to a non-root user’s X server.

Or, merely specify a particular app:

XAUTHORITY=/home/username/.Xauthority appname
where appname is the name of the particular app. (e.g. kwrite

Arduino on Raspberry

Install Arduino on Buster

  1. Arduino 1.8.12 (or higher) from – Software – Arduino ARM 32 bits
  2. Download to e.g. Downloads
  3. cd /Downloads
  4. tar -x -f arduino-1.8.5-linuxarm.tar.xz
  5. cd arduino-1.8.12
  6. sudo ./
  7. and Arduino appears in Home – Programming desktop

XRDP revisited

Shutdown menu not working:

You can get the Buttons working, just change the line ‘Logout=lxde-pi-shutdown-helper’ to ‘Logout=sudo lxde-pi-shutdown-helper’
in ‘/home/pi/.config/lxpanel/LXDE-pi/config’.

Shutdown Raspbian with a button

he simplest way to set up a “pushbutton to shut down” is to add “dtoverlay=gpio-shutdown” to /boot/config.txt and rig a NO momentary contact switch across pins 5 and 6. Push once to shut down the system. Push again to reboot it.


To avoid I2c being unusable:GPIO26 is physical pin 37

pi@raspberrypi:~ $ dtoverlay -h gpio-shutdown
Name:   gpio-shutdown

Info:   Initiates a shutdown when GPIO pin changes. The given GPIO pin
        is configured as an input key that generates KEY_POWER events.
        This event is handled by systemd-logind by initiating a
        shutdown. Systemd versions older than 225 need an udev rule
        enable listening to the input device:

                ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", \
                        SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", \
                        ATTRS{keys}=="116", TAG+="power-switch"

        This overlay only handles shutdown. After shutdown, the system
        can be powered up again by driving GPIO3 low. The default
        configuration uses GPIO3 with a pullup, so if you connect a
        button between GPIO3 and GND (pin 5 and 6 on the 40-pin header),
        you get a shutdown and power-up button.

Usage:  dtoverlay=gpio-shutdown,=

Params: gpio_pin                GPIO pin to trigger on (default 3)

        active_low              When this is 1 (active low), a falling
                                edge generates a key down event and a
                                rising edge generates a key up event.
                                When this is 0 (active high), this is
                                reversed. The default is 1 (active low).

        gpio_pull               Desired pull-up/down state (off, down, up)
                                Default is "up".

                                Note that the default pin (GPIO3) has an
                                external pullup.
The default GPIO is 3 (which appears on pin 5 of the 40-pin header). The pin is configured to pull high by default (which is also the SoC's default), and the GPIO is marked as active low (the opposite to the pull, for obvious reasons).

If you are having trouble, you can debug using raspi-gpio on Raspbian. Assuming for now that the overlay applied correctly (lsmod should show gpio-keys as being loaded), raspi-gpio get 3 will show the pin level (which should be 1) and function (INPUT). Now run it in a loop:

pi@raspberrypi:~ $ while true; do raspi-gpio get 3; done
Attach one end of a patch lead to pin 5 (GPIO3), and touch the other end on pin 6 (GND) - the Pi should shut down immediately, but if not you will see the pin level change to 0.

Ultibo installation





1. Install from distribution on
2. Next rebuild RTL to latest and greatest version
– download sources
– download from github
– extract in {location}
– Lazarus tools Build Ultibo

Raspberry Pi

The next stepts, including the IDE (Lazarus for Ultibo) work file from the RPi console or over a XRDP or VNC connection.

Use the ultibo installer script, initially and to update to the latest and greatest version of Ultibo on github.

1. Copy the installer script over to the Pi, in the user pi home directory.


2. Make the script executable

chmod +X

2. Install dependencies (only needed on a fresh system)
Do the next steps in superuser mode. Convenient and otherwise it fails on an XRDP session

sudo su
apt-get update
apt-get upgrade

apt-get install -y libgtk2.0-dev libcairo2-dev libpango1.0-dev libgdk-pixbuf2.0-dev libatk1.0-dev libghc-x11-dev
apt-get install -y libx11-dev libgdk-pixbuf2.0-dev  gir1.2-coglpango-1.0  xorg-dev
apt-get install -y binutils-arm-none-eabi 

3. Increase swapfile

Because the Raspberry Pi only has 1024MB of memory at most, installation on Raspbian requires a swap file size of at least 1000MB, anything less may result in an error while building Lazarus.

nano /etc/dphys-swapfile

Edit the CONF_SWAPSIZE value to be 1000 or greater

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
ls -lh /var

And check that the file “swap” is now the size specified above

5. Now run script in user mod, the ‘exit’ in step 3 takes care of that. Takes a while!

6. After completion

Change swap back to 100, see step 3, in superuser mode!

The Lazarus IDE will now show up in Programming on the desktop.