1. Introduction

In this tutorial, we’re going to see how barcodes work, and some specific examples of how to read them.

2. How Do Barcodes Work?

A barcode is a machine-readable code comprising a set of dark and light bars of varying widths that we can use to represent a short string of letters and numbers.

We can use them to tag anything that we want to be able to quickly and reliably read. The most recognized example is stock in shops so that the tills can scan them, but this can extend to anything – IT asset tags, passports, store loyalty cards, and a vast range more.

But how do they work exactly? There are dozens of different barcode standards, but most work similarly. They’ll generally use the patterns of bars to represent the characters in our codes and some sequences used for error correction and other control mechanisms.

Most barcodes are considered to be either discrete – which means that every character comprises several bars and spaces, with an additional space between each character – or continuous – which means that there’s no space between different characters.

Different barcode systems will then use several different sizes of bars. Some will only have two sizes – wide and narrow – in which the exact dimensions are less important as long as it’s obvious which is which. Others will have many different sizes that all work as multiples of the base size. These can allow for more data in the same space, but they’ll require more accuracy in both printing and reading.

3. Worked Example – UPC Barcodes

Let’s look at an example of how a real barcode works with UPC barcodes. UPC barcodes are amongst the most common, used worldwide for products in retail stores. These are the barcodes that all of our products have on them, which get scanned at the tills when we finish shopping.

A UPC Barcode uses 4 different widths of bars in a continuous scheme. They can typically represent 12 digits, with each digit represented by 4 bars. They then have some special constructs to indicate the ends of the barcode and to separate the barcode into two halves:

Rendered by QuickLaTeX.com

This sounds complicated, so let’s see it for real.


Here we can see a fairly standard set of bars. Reading this from left to right, what we get is:

  • 1-1-1 – This indicates that it’s the start of the barcode
  • 3-2-1-1 – This translates to the digit “0”
  • 1-1-1-4 – This translates to the digit “6”
  • 1-4-1-1 – This translates to the digit “3”
  • 1-1-3-2 – This translates to the digit “4”
  • 1-2-3-1 – This translates to the digit “5”
  • 1-2-3-1 – This translates to the digit “5”
  • 1-1-1-1-1 – This indicates the middle of the barcode
  • And so on

These barcodes have a couple of unique features to help with reading them. Every single sequence of bars adds up to a total of 7 wide, which means that if we’re reading a sequence that doesn’t fit this, then we can assume that the barcode is invalid in some way.

There are also no sequences that, when reversed, read as a different sequence. Additionally, all of the sequences on the right-hand side are inverted compared to the left. That is – dark bars on the left side are light on the right side, and vice versa. These allow us to read the barcode from either end and still correctly understand it.

For example, if we started reading from the right-hand side, then we’d see:

  • 1-1-1 – This indicates that it’s the start of the barcode
  • 2-2-1-2 – This isn’t a valid code. However, 2-1-2-2 is valid, so we can treat this as the last digit
  • 4-1-1-1 – This isn’t a valid code. However, 1-1-1-4 is valid, so we can treat this as the second-last digit
  • And so on

4. Worked Example – Code39 Barcodes

Another common type of barcode is Code39, also called Code 3 of 9. These allow the barcode to represent letters, numbers, and a few other characters.

Every character in this barcode comprises 9 bars, of which 3 bars – 2 dark and 1 light – will be wider than the others. This gives rise to the name – 3 of 9 bars are wide. There’s then an additional space between each character: a narrow light bar. Because of how the barcode is laid out, this means that there will always be 5 dark bars, of which 2 are wide, and there will always be 4 light bars, of which 1 is wide.

Decoding these barcodes depends on which of the bars are wide. The first thing is to determine the wide light bar, which allows us to select a group of characters:

  • If the first light bar is wide, our characters are U, V, W, X, Y, Z, or the symbols hyphen, period, space or asterisk
  • If the second light bar is wide, then our characters are 1, 2, 3, 4, 5, 6, 7, 8, 9, or 0
  • If the third light bar is wide, our characters are A, B, C, D, E, F, G, H, I or J
  • If the fourth light bar is wide, our characters are K, L, M, N, O, P, Q, R, S or T

Of these, the Asterisk character is reserved for use as the start and end of the sequence, but we can use all of the other characters for the barcode itself.

Next, we determine which of the dark bars are the wide ones. The positions of these are then added together using a 2-of-5 code. This is similar in concept to binary but with different values for the positions.

  • The first dark bar is valued at 1
  • The second dark bar is valued at 2
  • The third dark bar is valued at 4
  • The fourth dark bar is valued at 7
  • The fifth dark bar is valued at 0

So, if the second and third dark bars are the wide ones, then we get a value of 2+4=6. There’s a special case of 4+7, which we treat as 10 instead of 11. This is then used to determine which character in our group to use.

This all sounds very complicated, but in reality, we can simplify it to the same as for UPC. We just need to know the correct pattern for each character, and we can directly translate them. We can even include the additional gap in our patterns, becoming a continuous pattern. For example:

  • N-W-N-N-W-N-W-N-N-N = Asterisk
  • W-N-N-W-W-N-N-N-N-N = “5”
  • N-N-W-N-W-N-N-W-N-N = “P”

So let’s see this in action:


Reading this from left to right, we get:

  • N-W-N-N-W-N-W-N-N-N – An asterisk, which indicates the start of the barcode
  • N-N-W-N-N-W-N-N-W-N – A “B”
  • W-N-N-N-N-W-N-N-W-N – An “A”
  • And so on.

The fact that we know the barcode must have an asterisk character at each end, and the pattern for this character isn’t symmetrical, means that we can determine if we’re reading left-to-right or right-to-left by this. If we see the normal pattern for the asterisk then we’re reading left to right. If we see the reverse pattern – N-N-N-W-N-W-N-N-W-N – then we’re instead reading right-to-left.

5. Conclusion

Here we’ve discussed what barcodes are and how they work and seen exact details of how UPC and Code39 barcodes work. There are still lots of other barcodes that work in similar ways but not the same way. Next time you need to represent some information, why not investigate if using barcodes can help?

Comments are closed on this article!