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:
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!
18 Comments
Question 4 years ago on Step 7
I'm having a MCR-12 USB Bar Code Reader. I tried to use USB Host Shield with Arduino UNO and Arduino Mega. But it didn't work for me.
Can't I cut the cable and connect it directly to the Arduino Board?
Or Tell me an alternative way to connect this.
Please reply.
Answer 4 years ago
same problem … did you find the solution? plz guide me ..
Reply 1 year ago
did you ever get your MCR-12 to work? i am trying to get one to with with an arduino using PS2 with no luck
Reply 1 year ago
yes my code did work for me.. it was a class project that can read data comming from a scanner and display that on monitor
Answer 1 year ago
did you ever get your MCR-12 to work? i am trying to get one to with with an arduino using PS2 with no luck
Question 4 years ago
can i write this code in code::blocks C++
4 years ago
Hi. My barcode scanner doesn't make a beep sound after plugging in. It doesn't even light up when I start scanning. I have an HID Barcode scanner by Symbol Tech Inc. Your help will be highly appreciated. I followed all your steps but failed to have the same results.
Tip 4 years ago
can you take a full picture include of the wires at scanner. bcs i want to see how you make it please.
Question 4 years ago
I was wondering if I have the code or some recommendation to write it. Thank you
5 years ago
Which programming language has been used?
5 years ago
Thank you for this.
It gave me inspiration to use a barcode scanner in my system. I found another solution which, for me, was simpler - NOTE I am using an Arduino DUE.
I bought a PS/2 barcode scanner and connected it to a PS/2 to USB adapter - the adapter I bought allows for two inputs (the purple and green PS/2 plugs) to one USB male output. The barcode scanner I bought was ZEBEX Z-3100. I have not tested other scanner so have no idea whether it will work but I do not see why it won't.
I connected this adapter to a USB OTG cable and connected it to my Arduino DUE native USB port. The DUE has a built in USB host module. I am sure that using a USB host shield will work for other type of Arduino, but I am no expert though!
I plugged the scanner in and ran the below code per the link below
https://www.arduino.cc/en/Tutorial/KeyboardControl...
And it worked :) The code most likely will need to be edited if you using a USB host shield for other Arduino's.
Note that the Barcode Scanner I bought has settings which can be adjusted by scanning barcodes provided by ZEBEX. Based on ZEBEX settings, ensure that "Enable IBM PC/AT/PS/2 Keyboard emulation" or something similiar is allowed on your device. When other scanning modes where used, it did not work.
The Barcode scanner I used, has a Y-adapter on the cable so it allowed me to add a PS/2 keyboard to it so with one input into the DUE, I was able to have a barcode scanner and a full keyboard!
Tip 5 years ago on Step 6
Can I have complete coding plzz
5 years ago
how to identify the data pin and clock pin
5 years ago
how to know my barcode scanner byte keymap [] ?
your code is for the all barcode scanner default? or what?
7 years ago
Hello
When barcode scanner using PS/2 we need to connect keyboard with barcode scanner. Do you know how to by pass this ?
7 years ago
Why does the serial monitor inly display one digit of the barcode?
7 years ago
Hello ,
My name is Barış , I am from Turkey
I m really impressed about your project and I want to do similar one but I m so rookie about pic programming.
I would like to do when the servo motor change the direction according to barcode scanner read.
Could you please show me the source about this project?
8 years ago on Introduction
Very cool project!