Kuman MHS-3.5inch touchscreen with Pygame on Raspberry Pi on command-line

The Kuman MHS-3.5inch is a resistive touchscreen for the Raspberry Pi. It’s a cheap alternative to the more expensive touchscreens from Adafruit. This tutorial is about how to install it correctly on your Raspberry Pi, so that you can use it with the library Pygame. Pygame allows you to create graphical user interfaces with Python which can be used with the touchscreen from command-line. Let’s start with it.

Display Setup

install it

git clone https://github.com/goodtft/LCD-show.git
chmod -R 755 LCD-show
cd LCD-show/
sudo ./MHS35-show

edit file

sudo nano /etc/rc.local

delete or comment out line with: fbcp &

for more information: https://github.com/goodtft/LCD-show/issues/107

ctrl + x to exit and y to save changes and reboot

sudo reboot

Touchscreen Setup

edit or create file

sudo nano /usr/share/X11/xorg.conf.d/99-calibration.conf

add lines to file

Section "InputClass"
Identifier "calibration"
MatchProduct "ADS7846 Touchscreen"
Option "Calibration" "160 3723 3896 181"
Option "SwapAxes" "1"
EndSection

ctrl + x to exit and y to save changes

edit or create file

sudo nano /etc/udev/rules.d/95-ads7846.rules

add line to file

SUBSYSTEM=="input", KERNEL=="event[0-9]*", ATTRS{name}=="ADS7846 Touchscreen", SYMLINK+="input/touchscreen"

ctrl + x to exit and y to save changes and reboot

sudo reboot

calibrate touchscreen

sudo bash
apt-get install libts-bin
TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen ts_calibrate

test touchscreen

TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen ts_test

Hint: if screen remains black, check if correct screen was chosen (e.g. replace fb1 with fb0)

That’s it basically but it wouldn’t work with Pygame correctly. We need to do a few more steps.

Pygame Setup

install pygame

sudo apt install python3-pygame

If there is an error message where it says something with “.. fix broken install”, just do the following

sudo apt --fix-broken install

Pygame uses SDL which has some serious incompatibilities with touchscreens when newer versions of it (SDL 2.x, SDL 1.2.15-10) are used. It works with SDL 1.2 and you can install it with the following script (install_sdl12.sh). It will get the older SDL version from raspbian wheezy.

create file

sudo nano install_sdl12.sh

add lines to file

#enable wheezy package sources
echo "deb http://legacy.raspbian.org/raspbian wheezy main
" > /etc/apt/sources.list.d/wheezy.list

#set stable as default package source (currently stretch)
echo "APT::Default-release \"stable\";
" > /etc/apt/apt.conf.d/10defaultRelease

#set the priority for libsdl from wheezy higher then the stretch package
echo "Package: libsdl1.2debian
Pin: release n=stretch
Pin-Priority: -10
Package: libsdl1.2debian
Pin: release n=wheezy
Pin-Priority: 900
" > /etc/apt/preferences.d/libsdl

#install
apt-get update
apt-get -y --force-yes install libsdl1.2debian/wheezy

ctrl + x to exit and y to save file and run it

sudo chmod +x installsdl.sh
sudo ./installsdl.sh

Please notice: Now this script is for Raspbian Stretch. If you are using a different version, just change the name of your version in the script.

If this script can’t find the location of wheezy, the url might have changed and in this case you need to change it as well.

Currently all newer versions of raspbian can be found under archive.raspbian.org while wheezy can be found under legacy.raspbian.org

Also: if you do sudo apt-get update and sudo apt-get upgrade afterwards, it will override the SDL version and you will need to run the script again. You will notice it, if your cursor in pygame is going crazy.

Pygame Script

This is a simple script which demonstrates, how to access the cursor position of the touchscreen.

import pygame
import os

os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')
pygame.init()

SCREEN_WIDTH = 480
SCREEN_HEIGHT = 320

lcd = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.mouse.set_visible(True)
lcd.fill((255, 0, 0))
pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEMOTION and pygame.mouse.get_pressed()[0]:
            print(pygame.mouse.get_pos())

run it

sudo python3 cursor.py

Hint: settings of my display in boot/config.txt dtoverlay=mhs35:rotate=90. If you have set it to 270, the axes of your touchscreen might be swapped.

cursor.py in action

That’s it!

useful links:

https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi/pitft-pygame-tips

3.2″ Touch Display Quick Guide

1 thought on “Kuman MHS-3.5inch touchscreen with Pygame on Raspberry Pi on command-line

Leave a Reply

Your email address will not be published. Required fields are marked *