70Technology

How a barcode is read

The stripes on a pack are a number with a built in check. See digits become bars, work out the check digit, make a typo and smudge the stripes to see what a scanner does.

Checkout

Twelve digits in a row of stripes

The stripes on a pack of tea are a number. In the common UPC-A code the number has 12 digits, and each digit is drawn as a pattern of 7 narrow slots, each one dark or light. A scanner sweeps a beam across the stripes, measures how long it sees dark and light, and turns the lengths back into digits. The last digit is not part of the product number. It is a check on the other eleven.

Four scanner windows to try. See how digits become bars. Work out the check digit. Make a typo and see if the check catches it. Then smudge the stripes and see what the scanner does. The code in the windows is an example. You can type your own 11 digits. Without a script each window shows one picture or table.

Window one

From digits to bars

A UPC-A code is 95 slots wide. It starts with a guard of three slots, dark, light, dark. Then come six digits of 7 slots each, a middle guard of five slots, six more digits, and an end guard like the start. Each digit has its own pattern. On the right half the patterns are the left ones turned inside out, dark where the left had light. That way a scanner can tell which half it is looking at, and which way up the code is.

Type 11 digits. The page works out the twelfth and draws the whole code. The guards are drawn a little longer than the digits.

036000291452
The pattern of each digit on the left and on the right, dark as 1 and light as 0
DigitLeft halfRight half
000011011110010
100110011100110
200100111101100
301111011000010
401000111011100
501100011001110
601011111010000
701110111000100
801101111001000
900010111110100

The code 036000291452 is 95 slots wide. The twelfth digit, 2, is the check digit.

Window two

The check digit

The twelfth digit is worked out from the first eleven. Add the digits in places 1, 3, 5, 7, 9 and 11, counting from the left, and multiply by 3. Add the digits in places 2, 4, 6, 8 and 10. Add the two results. Take the last digit of the total. If it is 0, the check digit is 0. Otherwise it is 10 minus that last digit.

The page shows each step for the code in the window. Change a digit and watch the sums move. The scanner does the same sum on what it reads, and compares the answer with the twelfth digit.

The steps of the check digit sum
StepValue
Digits in places 1, 3, 5, 7, 9, 11 added14
That times 342
Digits in places 2, 4, 6, 8, 10 added16
Total58
Last digit of the total8
Check digit2

For 03600029145 the total is 58, its last digit is 8, so the check digit is 2.

Window three

Catch the typo

A check digit is for typing mistakes, and for reading mistakes. Change one digit of a code and the check no longer matches, every time. The weights 3 and 1 never cancel out when only one digit changes. Swapping two neighbouring digits is caught most of the time. It slips through only when the two digits differ by exactly 5, because the sum changes by twice the difference, which is a multiple of 10.

Pick a place and a new digit, or swap two neighbours. The page says whether the changed code passes. Below that it tries every one digit mistake and every swap for the code in the window.

How many mistakes the check digit catches in the example code
MistakeTriedCaught
One digit changed108108
Two neighbours swapped99

Change a digit or swap two neighbours to see if the check digit still matches.

Window four

A smudge on the stripes

A scanner measures the widths of the bars and the gaps, not their size. So the same code can be big or small, as long as the narrowest bar is wide enough for the scanner to see. The code also needs a blank margin, called the quiet zone, around it so the scanner knows where the stripes begin.

Flip one or two slots, as a smudge or a gap would. The page reads the stripes back the way a scanner does. Every digit pattern here has an odd number of dark slots on the left and an even number on the right, so a single smudge makes a pattern that is not a digit. Two smudges in one digit can turn it into another digit, and then the check digit catches it.

036000291452
What happens to the example code when slots are flipped
SmudgeWaysCannot be readRead, but fails the check
One slot95950
Two slots4465440164

Of the 4,465 ways to flip two slots, 4,401 cannot be read, 64 read as digits but fail the check, and none pass as a wrong code.

Key takeaways

  1. A UPC-A code is 95 slots wide: three of guard, six digits of seven, five in the middle, six more digits and three of guard.
  2. The right half of the digit patterns is the left half turned inside out, so a scanner knows which half it sees.
  3. The twelfth digit comes from the other eleven: three times the odd places plus the even places, then ten minus the last digit.
  4. The check catches every wrong single digit and every swap of neighbours, except those that differ by exactly 5.
  5. A scanner uses the widths of the stripes, so the size does not matter. A smudge gives a pattern that is not a digit, or a code that fails the check.

Sources

Some of these sites could not be opened while this page was written, so some facts were checked through search results that quote them. The 7 slots of each digit, the guards of 3, 5 and 3 slots, the 95 slots in all, the first six digits on the left and the last six on the right, the right patterns being the left ones inverted, the check rule (3 times the odd places plus the even places, then ten minus the last digit modulo 10), the widths and not the size being what a scanner measures, the quiet zone, the first scan on 26 June 1974 in Troy, Ohio, Woodland and Silver in 1949 and Laurer’s rectangular design each appeared in at least two places. The ten patterns were checked against a table in one source for digits 0 to 5 and the rest follow the standard table. Sources differ on the right guard, on the kind of gum and its price, and on the exact day in 1973 when the code was chosen, so the page does not give that day. The claim that one changed digit is always caught and a swap of digits that differ by 5 is not comes from the sum, and is computed on the page. The counts of smudges are computed on the page for the code in the window.