Introduction: Arduino PS/2 Barcode Scanner

This tutorial goes through the steps of using an Arduino Uno to read the inputs of a PS/2 barcode scanner.

Step 1: Parts

You will need:

1. an Arduino Uno

2. breadboard + wires

3. two 2.2k ohms resistors

4. ps/2 barcode scanner that reads keyboard characters

http://www.amazon.com/gp/product/B00OBD0J1C

Step 2: Identify Barcode Scanner Pins

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

Step 3: Connect the Circuit

Connect up the circuit based on this schematic on Arduino website

http://playground.arduino.cc/ComponentLib/BarcodeScanner

Step 4: Verify Circuit

Make sure the pins are connected correctly

In my case,

-the 2 orange wires are power (left) and ground (right)

-the 2 yellow wires are data (green wire, arduino pin 4) and clock (blue wire, arduino pin 3)

I also added a simple LED that lights up just to make sure power and ground are active

Step 5: Verify the Scanner Is Working

Press the button at the bottom of the scanner to start scanning

If the power and ground pins are connected correctly to the scanner, you should hear "beeps", and see red light coming out from the scanner

Step 6: Upload the Code

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; } }

Step 7: Scan and Actual Barcode

Go to an online barcode generator. Example:

http://barcode.tec-it.com/

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!