This tutorial goes through the steps of using an Arduino Uno to read the inputs of a PS/2 barcode scanner.
Identify the individual pins in the ps/2 connector
There are only 4 pins that matter:
1. power (+5V)
2. ground
3. clock / interrupt
4. data
When the scanner outputs a character, it first sends a signal to the "clock" pin to indicate a character is being sent. Then the character data itself is sent through the "data" pin.
Note that the order of the pins are flipped horizontally, depending on whether the connector is male or female.
Note that if you cut open the connector, the color of the wires do not mean anything. Different websites/barcode-scanners claims different coloring systems, so do not assume "red" wire is power. "red" wires can be ground, power, data, or clock depending on the actual scanner.
For my barcode scanner, this were the order of the pins:
female
red power 4
yellow data 1
brown ground 3
orange clock 5
male
blue clock 5
brown gnd 3
green data 1
red power
Upload the code to your arduino Uno.
#define WAITLOW(pin) while (digitalRead(pin) != 0);
#define WAITHIGH(pin) while (digitalRead(pin) != 1);
int clockPin = 3; int dataPin = 4; static volatile uint8_t head; #define BUFFER_SIZE 45 static volatile uint8_t buffer[BUFFER_SIZE]; unsigned long lastScan = 0; boolean scanCorrect = true; int scannedInt = 0;
byte keymap[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '`', 0, 0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'q', '1', 0, 0, 0, 'z', 's', 'a', 'w', '2', 0, 0, 'c', 'x', 'd', 'e', '4', '3', 0, 0, ' ', 'v', 'f', 't', 'r', '5', 0, 0, 'n', 'b', 'h', 'g', 'y', '6', 0, 0, 0, 'm', 'j', 'u', '7', '8', 0, 0, ',', 'k', 'i', 'o', '0', '9', 0, 0, '.', '/', 'l', ';', 'p', '-', 0, 0, 0, '\'', 0, '[', '=', 0, 0, 0 /*CapsLock*/, 0 /*Rshift*/, 0 /*Enter*/, ']', 0, '\\', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '1', 0, '4', '7', 0, 0, 0, '0', '.', '2', '5', '6', '8', 0, 0 /*NumLock*/, 0, '+', '3', '-', '*', '9', 0, 0, 0, 0, 0, 0 };
void setup() { pinMode(clockPin, INPUT_PULLUP); pinMode(dataPin, INPUT_PULLUP); Serial.begin(9600); Serial.println("smart kart"); delay(2000); }
void loop() { WAITLOW(clockPin); WAITHIGH(clockPin); unsigned char keycode = 0; for (uint8_t i = 0; i < 8; i++) { WAITLOW(clockPin); keycode >>= 1; if (digitalRead(dataPin)) { keycode |= 0x80; } WAITHIGH(clockPin); } buffer[head++] = keycode; WAITLOW(clockPin); WAITHIGH(clockPin); WAITLOW(clockPin); WAITHIGH(clockPin);
unsigned long time = millis(); scanCorrect = true; if (head == 5 && lastScan - time > 2000) { scannedInt = keymap[buffer[3]] - '0'; if (scannedInt > 0) { Serial.println(); Serial.println("***** Detected Scan *******"); Serial.println(scannedInt); Serial.println("*******"); } else { scanCorrect = false; } head = 0; lastScan = time; for (int i = 0; i < 5; i++) buffer[i] = 0; } }
Go to an online barcode generator. Example:
Open the Serial Monitor on your arduino IDE.
Use the barcode scanner to scan the barcode
You should now see the scanned characters displayed on your Serial Monitor.
Have fun!