A script for using the M5Stack CardKB with Raspberry Pi
In order for buttons to return the correct symbols, the keyboard layout will need to be set to us on you pi. You can do this by running:
sudo nano /etc/default/keyboard
and changing XKBLAYOUT
to us
:
# KEYBOARD CONFIGURATION FILE
# Consult the keyboard(5) manual page.
XKBMODEL="pc105"
XKBLAYOUT="us"
XKBVARIANT=""
XKBOPTIONS=""
BACKSPACE="guess"
The cardKB communicates over i2C, make sure this is enabled on your raspberry pi. You can find a tutorial on how to do so here.
Connect the wires on the CardKB JST connector to the appropriate pin on the Raspberry Pi.
You may need to improvise a connection solution with breadboard wires like so:
You will need to load the uinput module to allow python-uinput to input key presses. You can check if it is loaded with:
lsmod | grep uinput
If nothing is displayed, then the module is not loaded. To load the module, run:
modprobe uinput
To load the module automatically on startup, run:
sudo nano /etc/modules
add uinput
at the bottom of the file. Save and then reboot.
Install smbus and python-uinput:
sudo apt install python3-smbus
sudo pip3 install python-uinput
clone this repository:
git clone https://github.com/strpotowl/cardkb-updated.git
Run the script and check buttons return expected characters:
sudo python3 cardkb &
By default, the python script listens to /dev/i2c-1
, you can change this by adding an argument to the start command.
sudo python3 cardkb 11 &
We can use systemd to run the CardKB script as a service. To do so, create a unit file:
sudo nano /lib/systemd/system/cardkb.service
Add the following:
[Unit]
Description=Service for using CardKB with Raspberry Pi
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python3 /home/pi/cardkb-updated
[Install]
WantedBy=multi-user.target
This service file assumes that you have cloned the cardkb repo to /home/pi. If this is not the case, you will need to change the file path.
...
ExecStart=/usr/bin/python3 /home/strpotowl/cardkb-updated
...
Likewise, if you are running cardkb on a i2c bus other than one, then you will need to add the bus number to the end of the ExecStart
line like so:
...
ExecStart=/usr/bin/python3 /home/pi/cardkb-updated 11
...
Save the file and exit. Now run the following commands to reload the systemctl daemon, enable the cardkb service and restart the pi:
sudo systemctl daemon-reload
sudo systemctl enable cardkb.service
sudo reboot
When your Pi restarts, your cardkb should be working, allowing you to log in.
Full credit to Ian Antking (https://github.com/ian-antking) and his original project (https://github.com/ian-antking/cardkb) which has helped me so much.