The PS/2 keyboard is a type of input device that connects to a computer via a PS/2 port, utilizing a 6-pin mini-DIN connector. It was widely used in older computer systems for inputting text and commands. Despite being largely replaced by USB keyboards, PS/2 keyboards are still valued in certain applications due to their low latency and direct hardware-level communication.
The PS/2 connector has six pins, but only four are typically used for communication. Below is the pinout:
Pin Number | Name | Description |
---|---|---|
1 | Data | Serial data line for communication |
2 | Not Connected | Unused |
3 | Ground (GND) | Electrical ground |
4 | VCC | +5V power supply |
5 | Clock | Clock signal generated by the keyboard |
6 | Not Connected | Unused |
Below is an example code snippet for reading data from a PS/2 keyboard using an Arduino UNO:
// PS/2 Keyboard Interface Example for Arduino UNO
// Connect Data to pin 2 and Clock to pin 3 on the Arduino
#define DATA_PIN 2 // PS/2 Data line connected to Arduino pin 2
#define CLOCK_PIN 3 // PS/2 Clock line connected to Arduino pin 3
volatile bool dataAvailable = false; // Flag to indicate data is ready
volatile uint8_t receivedData = 0; // Variable to store received data
void clockISR() {
static uint8_t bitCount = 0; // Tracks the number of bits received
static uint8_t dataBuffer = 0; // Temporary buffer for incoming data
if (bitCount < 8) {
// Read the data bit (LSB first)
if (digitalRead(DATA_PIN)) {
dataBuffer |= (1 << bitCount);
}
bitCount++;
} else if (bitCount == 8) {
// All 8 data bits received, store the data
receivedData = dataBuffer;
dataAvailable = true;
bitCount = 0; // Reset for the next byte
dataBuffer = 0;
}
}
void setup() {
pinMode(DATA_PIN, INPUT_PULLUP); // Set Data pin as input with pull-up
pinMode(CLOCK_PIN, INPUT_PULLUP); // Set Clock pin as input with pull-up
attachInterrupt(digitalPinToInterrupt(CLOCK_PIN), clockISR, FALLING);
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
if (dataAvailable) {
Serial.print("Received Data: ");
Serial.println(receivedData, HEX); // Print received data in hexadecimal
dataAvailable = false; // Reset the flag
}
}
No Data Received:
Garbage Data or Incorrect Characters:
Keyboard Not Responding:
Q: Can I use a PS/2 keyboard with a modern computer?
A: Most modern computers lack PS/2 ports, but you can use a PS/2-to-USB adapter to connect the keyboard.
Q: Why use a PS/2 keyboard instead of USB?
A: PS/2 keyboards offer lower latency and direct hardware-level communication, making them ideal for certain applications.
Q: Can I connect a PS/2 keyboard to a 3.3V microcontroller?
A: Yes, but you will need level shifters to safely interface the 5V signals with the 3.3V microcontroller.