Barcode Scanner

By Robert Sprowson

Originally published in EUG #36

A while back, I managed to get hold of a second hand barcode scanner which had previously been used in a stationery store. It appears to be the equivalent to Maplin Electronics part number KV75S. It came with an unlabelled 5-pin DIN plug which would have connected to the host terminal.

Assuming you have one of these scanners in front of you (from Maplin, a closed down shop or second-hand from Micromart then take the lid off. Some of the screws may be concealed by rubber grommets or labels so don't force it!

Inside you'll see a massively integrated chip at the heart a handful of tiny electrolytic capacitors and what looks like a blank calculator display pointing up to a mirror.

Don't get grubby fingerprints on this part - that blank grey rectangle is the CCD (Charge-Coupled-Device) which converts light into charges.

Where the DIN plug wire terminates there should be just three wires used. They may be colour-coded in black/red/yellow (for example) for 0V/5V/data but don't rely on this!

Look around at all the chips and the PCB tracks. You'll find that one of the wires goes to nearly all the parts so this is probably 0V. Another dead give-away is that tracing it immediately it enters the board: it should go to the '-' terminal of a supply smoothing capacitor and one of the remaining two wires will go to the '+' side. So that's the supply sorted.

To make sure, attach it to a 5V supply quickly and the row of red or green scanning LEDs will light. You won't (generally) damage anything if you do get it wrong IF you do it quickly enough.

The last of the three leads is data out. If you have an oscilloscope that can be triggering in 'once only' mode then when you scan a valid barcode, a waveform like that in the diagram will occur. It consists of a wide sync pulse (data line drops from 5V to 0V) then a stream of data which (using your artistic judgement) exactly aligns with the black and white stripes on the barcode.


Page 435 of the User Guide shows the pinouts of the User Port. In addition you will need a CCD Barcode Scanner (eg. KV75S from MAPLIN) and a 20-pin female IDC plug. It's also necessary to have some strong glue.


  • Monitor stock levels in a store of some kind (eg. Scouts/Library)
  • Hardware recognises all known barcode formats automatically
  • Hardware automatically rights a code if it was scanned upside down
  • Hardware generates no clock pulse so data must be read in by polling the port and not by clocking in under interrupts

Building It

  1. Cut off the useless DIN plug as that won't fit in the User Port
  2. Attach the line discovered to be 5V to the 5V line on the User Port
  3. Likewise for 0V
  4. Ensure that the data is clocked in on PB0
  5. It may be necessary to solder these into place carefully as the cable is not true IDC cable and probably won't splice properly onto the connectors
  6. As the cable is round and not ribbon cable, apply some glue to the cable so that tugs on it won't cause stress to the joints made in 3,4,5 above.


The program CODE13 should be available to you, which demonstrates how to read a code 13 type barcode with the hardware. If there is a particular coding method you wish to be able to read then the end half of the program will need to be altered to suit.

To use the software, type CHAIN"U.CODE13" or call it from the Utils Menu. The message "Waiting for scan..." appears as a machine code loop polls the User Port until the sync pulse is found. It will then (as fast as it can) build a table of the number of reads to the User Port that occurred before the data changed, and hence these are directly proportional to the width of the pulse. The machine code returns to the caller when the same value is read from the port 255 times. It is then assumed that no more transmissions follow. A value is returned in X which is the length of the table created. Since data always starts at 1 so a typical table might consist of:

Logic value on PB0    1  0  1  0  1  0  1  0  1  0  1  0  1  0  1 etc...
Number of loops      20 4F 20 20 65 20 20 4F 4F 20 63 23 22 4D 20 etc...

As you can see, the timing coming from the scanner is not crisp and clean so some filtering is needed. For demonstration purposes, only code 13 barcodes are handled and rather crudely too! The following is applied:

- find average unit length of bar by averaging the end three bits and start three bits (start/stop bars)
- go through each table entry assigning a normalised length of between 1 and 4 long as the maximum code 13 repeat length is four consecutive digits

So, for the example above:

Logic value on PB0    1  0  1  0  1  0  1  0  1  0  1  0  1  0  1 etc...
Number of loops      20 4F 20 20 65 20 20 4F 4F 87 63 23 22 4D 20 etc...
Normalised value      1  2  1  1  3  1  1  2  2  4  3  1  1  2  1 etc...

Which means that the scanned code must have been


This is then checked for start/stop/guard bits and correct length and then decoded. You'll need to tell the computer what the first number of the barcode is (this is normally printed underneath!) which is the combination number. When - and only when! - this number is correct will all the digits decode properly. It is left to the reader to add an extra loop from 0 to 9 to try by guessing to find what that digit is, and hence get the whole barcode without needing end user input.

Note that scans won't always be accepted the first time, due to an incorrect length code 13 barcode. Keep trying, it's because the filtering is so crude. Try using a standard deviation instead of averaging the start/stop bit lengths. Or adjust the size of the window you look through.

Code 13 Barcodes

Using the EAN (European Article Number) encoding, the barcode consists of:

1. Combination number.
   This is the first digit. In our example, this is 5. It refers to how
   the next six digits are encrypted as follows:
                     COMB #       PATTERN TO FOLLOW
                        0         A  A  A  A  A  A
                        1         A  A  B  A  B  B
                        2         A  A  B  B  A  B
                        3         A  A  B  B  B  A
                        4         A  B  A  A  B  B
                        5         A  B  B  A  A  B
                        6         A  B  B  B  A  A
                        7         A  B  A  B  A  B
                        8         A  B  A  B  B  A
                        9         A  B  B  A  B  A

2. The start bit. Two parallel bars.
3. The following 6. Encrypted as above, then (taking "1" as black and 
   "0" as white) are converted onto 7 bit binary digits.

   0              0001101   0100111   1110010
   1              0011001   0110011   1100110
   2              0010011   0011011   1101100
   3              0111101   0100001   1000010
   4              0100011   0011101   1011100  Notice that not C = A and
   5              0110001   0111001   1001110  that reverse of C = B
   6              0101111   0000101   1010000
   7              0111011   0010001   1000100
   8              0110111   0001001   1001000
   9              0001011   0010111   1110100

4. The guard bit. Two parallel bars.
5. The following 6. Encrypted as above, then (taking "1" as black and
   "0" as white) are converted onto 7 bit binary digits.
6. The end bit. Two parallel bars.
7. The checksum. For the barcode 123456782093C where C is the check bit
   to be:
   1. Add up the alternate numbers starting at the 12th:
   2. Times (1) by 3 => 69
   3. Add up the alternate numbers starting at the 11th:
   4. Add parts (2) and (3) => 96
   5. Find the smallest number needed to make part (4) evenly divisible
      by ten [100-96=4 so C=4 as 100x10 is even and 4 < 14 which would
      be the next]

Happy soldering!

Robert Sprowson, EUG #36