Skip to content

Peripherals: Keyboards PS2, ZK81, USB

Stefan Lenz edited this page Dec 26, 2022 · 2 revisions

Keyboard code

A standard PS2 keyboard can be connected using the PS2 library. This is a nice page explaining how it is done https://www.instructables.com/Connect-PS2-Keyboard-to-Arduino/. More infos can be found on this page https://www.pjrc.com/teensy/td_libs_PS2Keyboard.html.

PS2 on an UNO

PS2 keyboard use is tricky. Some newer keyboards PS2 keyboard do not recognise an Arduino as a PS2 host and try to switch to USB mode. Older PS2-only keyboard usually work well. Please make sure that the clock PIN of the keyboard is connected to an interrupt ready pin of the Arduino. See this page for details: https://www.arduino.cc/reference/de/language/functions/external-interrupts/attachinterrupt/

Also, from version 1.3 on, IoT BASIC works best with my fork of the PS2 keyboard library https://github.com/slviajero/PS2Keyboard. I needed to add ESP8266, Mega AVR and RP2040 support and a keyboard peek function. All this is not there in the original.

Using the keyboard with BASIC

The source has to be compiled with the ARDUINOPS2 flag enabled. Please add the right keymap in the function ioinit(). The PS2 keyboard code consumes a hefty 1.9 kB of PROGMEM and 128 bytes of RAM (keymaps and buffers!). Once the program is uploaded it first uses the serial line as an input device. set 4, 1 switched input to the keyboard. set 4, 0 goes back to serial. If the code is compiled with the additional flag STANDALONE, keyboard input is default. This only works with the LCD or TFT option (https://github.com/slviajero/tinybasic/wiki/Using-LCD-displays-with-BASIC).

Look at these project pages for standalone systems with keyboard support: https://github.com/slviajero/tinybasic/wiki/Projects:-How-to-use-BASIC-on-Arduinos

ZX81 Keyboard

Alternatively a ZX81 keyboard can be used. It uses 13 pins for the rows and columns.

ZX81 Keyboard

My ZX81 keyboard library (https://github.com/slviajero/ZX81Keyboard) is needed for it and the macro ARDUINOZX81KBD has to be defined at compile time.

USB Keyboards

Arduino SAMD supports native USB. An experimental ESB keyboard feature was added to BASIC in version 1.4a including a German keymap feature. All this is done quick and dirty and experimental.

USB Keyboard

To use the USB keyboard, compile with the macro ARDUINOUSBKBD activated.

LCD Shield keyboard

The keys of the LCD shield are mapped to the characters 1-4 and LF and can be used as a keyboard just like the other keyboard options.

On keyboard libraries

The basis for this work is Paul Stoffregen's PS2 keyboard library. It doesn't support the ESP8266 platform as the underlying Free RTOS interrupt handling was not implemented in it. I have created a fork from it supporting ESP8266 and a few features I need in BASIC. Version 1.3 needs the patched library.