Barcodes

As I progress in my career as a computer scientist, I am slowly discovering the awesome power of data, big or small. Data runs this world, whether it’s in the form of tweets, weather forecasts, or news bites. However, there is a very primitive data form that is everywhere but is hardly given a second thought: barcodes.

Barcodes have been around since the early 1970s and have greatly contributed to the success of the commercial and retail industry. Every time I scan my groceries, I am in awe of how the barcode scanner can pick up which item I am placing in my cart. I know that the scanner can’t really distinguish a box of cereal from a jug of milk. It just reads the barcode and identifies the unique number associated with a particular item, which it then cross references with a database to determine the price, quantity, etc.

But how does the scanner actually read the barcode? Is there a tiny fairy sitting in the scanner writing down the number as fast as possible? No, in the fight between science and superstition, science wins out this time as the barcode scanner is just a laser that can figure out the number of black and white lines and their widths.

Barcode Pattern

There are many types of barcode encoding used, but the most common one is UPC (Universal Product Code). The UPC barcode format is shown below from wikipedia. All number digits are represented by 7 bars, with each digit having a unique encoding. For example, in the picture below, the left side 4 is represented by 1space-1bar-3space-2bar.

Barcode

A pattern for “start” and “end” exist on both sides of the code. There is also a pattern for “middle” in the middle of the code. In the picture above, the number representations on the right side of the “middle” pattern are simple inverses of the representations on the left side. This is to ensure that the code can be read whether it is the right way up or not. This is how the code is read:

  1. A laser travels from either left or right side of the code.
  2. As soon as it sees the “start” pattern, it starts reading the code.
  3. Once it hits the “middle” pattern, it starts reading the following code in inverse.
  4. As soon as the laser gets to the “end” pattern, it stops reading the code.
  5. The “start” and “end” pattern are the same, ensuring that the code can be read from both sides.

Barcode Scanner

The simplest way to obtain barcode data is to shine a light on the code and get feedback from the reflected light. This can be accomplished by shining a laser on the code and detecting the amount of reflected light by photocells.

In order for the laser to be a “line” that shines on the code, and oscillating mirror is used to spread a point of light across a larger area. Multiple photocells then look at the reflected light and convert it to 1s and 0s corresponding to white and black lines. This patterns of 1s and 0s is then analyzed by a computer which determines what product the UPC represents.

I think I will attempt to create my own barcode scanner now. Or at the very least an animation that explains how they work in a simpler manner than reading a long post.