ps2io – Support for PS/2 protocol

The ps2io module contains classes to provide PS/2 communication.


This module is not available in some SAMD21 builds. See the Module Support Matrix - Which Modules Are Available on Which Boards for more info.

All classes change hardware state and should be deinitialized when they are no longer needed if the program continues after use. To do so, either call deinit() or use a context manager. See Lifetime and ContextManagers for more info.

Available on these boards
  • 01Space 0.42 OLED ESP32C3
  • AITHinker ESP32-C3S_Kit
  • AITHinker ESP32-C3S_Kit_2M
  • ATMegaZero ESP32-S2
  • Adafruit EdgeBadge
  • Adafruit Feather ESP32 V2
  • Adafruit Feather ESP32-S2 Reverse TFT
  • Adafruit Feather ESP32-S2 TFT
  • Adafruit Feather ESP32-S3 Reverse TFT
  • Adafruit Feather ESP32-S3 TFT
  • Adafruit Feather ESP32S2
  • Adafruit Feather ESP32S3 4MB Flash 2MB PSRAM
  • Adafruit Feather ESP32S3 No PSRAM
  • Adafruit Feather HUZZAH32
  • Adafruit Feather M4 CAN
  • Adafruit Feather M4 Express
  • Adafruit FunHouse
  • Adafruit Grand Central M4 Express
  • Adafruit HUZZAH32 Breakout
  • Adafruit Hallowing M4 Express
  • Adafruit ItsyBitsy M4 Express
  • Adafruit MagTag
  • Adafruit Matrix Portal M4
  • Adafruit MatrixPortal S3
  • Adafruit Metro ESP32S2
  • Adafruit Metro ESP32S3
  • Adafruit Metro M4 Airlift Lite
  • Adafruit Metro M4 Express
  • Adafruit Monster M4SK
  • Adafruit PyGamer
  • Adafruit PyPortal
  • Adafruit PyPortal Pynt
  • Adafruit PyPortal Titano
  • Adafruit Pybadge
  • Adafruit QT Py ESP32 PICO
  • Adafruit QT Py ESP32-S3 4MB Flash 2MB PSRAM
  • Adafruit QT Py ESP32-S3 no psram
  • Adafruit QT Py ESP32C3
  • Adafruit QT Py ESP32S2
  • Adafruit Trellis M4 Express
  • AloriumTech Evo M51
  • Arduino Nano ESP32
  • Artisense Reference Design RD00
  • BLING!
  • BPI-Bit-S2
  • BPI-Leaf-S3
  • BPI-PicoW-S3
  • BastWiFi
  • Bee-Data-Logger
  • Bee-Motion-S3
  • Bee-S3
  • BlizzardS3
  • CP32-M4
  • CRCibernetica IdeaBoard
  • Capable Robot Programmable USB Hub
  • CircuitBrains Deluxe
  • CrumpS2
  • Cytron Maker Feather AIoT S3
  • DFRobot Beetle ESP32-C3
  • Deneyap Kart
  • Deneyap Kart 1A
  • Deneyap Kart 1A v2
  • Deneyap Kart G
  • Deneyap Mini
  • Deneyap Mini v2
  • ESP 12k NodeMCU
  • ESP32 Devkit V1
  • ESP32-C3-DevKitM-1
  • ESP32-S2-DevKitC-1-N4
  • ESP32-S2-DevKitC-1-N4R2
  • ESP32-S2-DevKitC-1-N8R2
  • ESP32-S3-Box-2.5
  • ESP32-S3-Box-Lite
  • ESP32-S3-DevKitC-1-N32R8
  • ESP32-S3-DevKitC-1-N8
  • ESP32-S3-DevKitC-1-N8R2
  • ESP32-S3-DevKitC-1-N8R8
  • ESP32-S3-DevKitM-1-N8
  • ESP32-S3-EYE
  • ESP32-S3-USB-OTG-N8
  • Espressif ESP32-EYE
  • Espressif ESP32-LyraT
  • FeatherS2
  • FeatherS2 Neo
  • FeatherS2 PreRelease
  • FeatherS3
  • Franzininho WIFI w/Wroom
  • Franzininho WIFI w/Wrover
  • Gravitech Cucumber M
  • Gravitech Cucumber MS
  • Gravitech Cucumber R
  • Gravitech Cucumber RS
  • HMI-DevKit-1.1
  • Hardkernel Odroid Go
  • HexKyS2
  • IoTs2
  • Kaluga 1
  • LILYGO TTGO T8 ESP32-S2 w/Display
  • LOLIN S3 16MB Flash 8MB PSRAM
  • Lilygo T-watch 2020 V3
  • LoC BeR M4 base board
  • Luatos Core-ESP32C3
  • M5Stack Atom Echo
  • M5Stack Atom Lite
  • M5Stack Atom Matrix
  • M5Stack Atom U
  • M5Stack AtomS3 Lite
  • M5Stack Core Basic
  • M5Stack Core Fire
  • M5Stack Core2
  • M5Stack Stick C
  • M5Stack Timer Camera X
  • MORPHEANS MorphESP-240
  • MagiClick S3 N4R2
  • Maker badge by Czech maker
  • MicroDev microC3
  • MicroDev microS2
  • Mini SAM M4
  • MixGo CE
  • NanoS3
  • Neuron
  • Oak Dev Tech PixelWing ESP32S2
  • ProS3
  • Robo HAT MM1 M4
  • S2Mini
  • S2Pico
  • SAM32v26
  • Saola 1 w/Wroom
  • Saola 1 w/Wrover
  • Seeed Studio XIAO ESP32C3
  • Seeeduino Wio Terminal
  • Silicognition LLC M4-Shim
  • SparkFun MicroMod SAMD51 Processor
  • SparkFun Thing Plus - SAMD51
  • TG-Boards' Datalore IP M4
  • Targett Module Clip w/Wroom
  • Targett Module Clip w/Wrover
  • The Open Book Feather
  • TinyPICO
  • TinyPICO Nano
  • TinyS2
  • TinyS3
  • TinyWATCH S3
  • UARTLogger II
  • VCC-GND YD-ESP32-S3 (N16R8)
  • VCC-GND YD-ESP32-S3 (N8R8)
  • Waveshare ESP32-S2-Pico
  • Waveshare ESP32-S2-Pico-LCD
  • Waveshare ESP32-S3-Pico
  • Wemos Lolin C3 Mini" // from Wemos MP
  • Wemos Lolin C3 Pico" // from Wemos MP
  • nanoESP32-S2 w/Wrover
  • nanoESP32-S2 w/Wroom

class ps2io.Ps2(data_pin: microcontroller.Pin, clock_pin: microcontroller.Pin)

Communicate with a PS/2 keyboard or mouse

Ps2 implements the PS/2 keyboard/mouse serial protocol, used in legacy devices. It is similar to UART but there are only two lines (Data and Clock). PS/2 devices are 5V, so bidirectional level converters must be used to connect the I/O lines to pins of 3.3V boards.

Create a Ps2 object associated with the given pins.

  • data_pin (Pin) – Pin tied to data wire.

  • clock_pin (Pin) – Pin tied to clock wire. This pin must support interrupts.

Read one byte from PS/2 keyboard and turn on Scroll Lock LED:

import ps2io
import board

kbd = ps2io.Ps2(board.D10, board.D11)

while len(kbd) == 0:

deinit() None

Deinitialises the Ps2 and releases any hardware resources for reuse.

__enter__() Ps2

No-op used by Context Managers.

__exit__() None

Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.

popleft() int

Removes and returns the oldest received byte. When buffer is empty, raises an IndexError exception.

sendcmd(byte: int) int

Sends a command byte to PS/2. Returns the response byte, typically the general ack value (0xFA). Some commands return additional data which is available through popleft().

Raises a RuntimeError in case of failure. The root cause can be found by calling clear_errors(). It is advisable to call clear_errors() before sendcmd() to flush any previous errors.


byte (int) – byte value of the command

clear_errors() None

Returns and clears a bitmap with latest recorded communication errors.

Reception errors (arise asynchronously, as data is received):

0x01: start bit not 0

0x02: timeout

0x04: parity bit error

0x08: stop bit not 1

0x10: buffer overflow, newest data discarded

Transmission errors (can only arise in the course of sendcmd()):

0x100: clock pin didn’t go to LO in time

0x200: clock pin didn’t go to HI in time

0x400: data pin didn’t ACK

0x800: clock pin didn’t ACK

0x1000: device didn’t respond to RTS

0x2000: device didn’t send a response byte in time

__bool__() bool
__len__() int

Returns the number of received bytes in buffer, available to popleft().