How the barcode works
Photo: N43 and HermesA barcode encodes a number in a pattern of bars and spaces that a scanner reads by measuring reflected light. The UPC-A encodes 12 digits across 95 modules with a check digit for error detection. QR codes extend the idea to two dimensions with Reed-Solomon error correction that can reconstruct up to 30 percent of damaged data.
Video reference: How Do Barcodes Work? — History of Simple Things. Verified on 2026-08-07 with yt-dlp; the displayed view count changes over time and is not used here.
01The barcode as a machine-readable language
A barcode is a pattern of parallel lines and spaces that encodes information in a form a machine can read. The idea is simple: vary the width of dark bars and the gaps between them, and you create a visual language that a scanner can decode by measuring reflected light. The scanner does not understand the bars the way a human reads text. It detects transitions between dark and light, measures the widths, and converts those measurements into a number. That number is then looked up in a database to find the product it represents.
The most familiar barcode is the UPC-A (Universal Product Code), which appears on virtually every retail product. A UPC-A encodes a 12-digit number: the first digit identifies the product type, the next five identify the manufacturer, the following five identify the specific product, and the last digit is a check digit calculated from the preceding eleven. The bars themselves do not spell out the product name or price. They encode a number, and the point-of-sale system uses that number to look up the product in a database.
02Anatomy of a UPC-A barcode
A UPC-A barcode has a precise structure. It begins with a quiet zone, a blank margin at least nine times the width of the narrowest bar, which gives the scanner a reference for the background reflectance. Next comes the start guard pattern: bar-space-bar in the pattern 101, which tells the scanner where the data begins. Then six left-side digits are encoded, each using a 7-bit pattern of bars and spaces. A center guard pattern (bar-space-bar-space-bar, or 01010) separates the left and right halves. Six right-side digits follow, encoded with a different parity. Finally, the same 101 end guard pattern marks the end, followed by another quiet zone.
Each digit in a UPC-A is encoded as a pattern of two bars and two spaces, totaling seven modules wide. A module is the width of the narrowest element. The digit 0, for example, is encoded as space-bar-space-bar with widths 3-2-1-2 (three modules of space, two of bar, one of space, two of bar). The digit 1 uses widths 2-2-2-1, and so on. The left side uses odd parity encoding, the right side uses even parity, which allows the scanner to determine whether it is reading the barcode left-to-right or right-to-left and reverse accordingly.
03The check digit: catching errors automatically
The 12th digit of a UPC-A is a check digit, calculated from the first 11 digits using a modulo-10 algorithm. The algorithm works as follows: add the digits in odd positions (1st, 3rd, 5th, etc.) and multiply by 3. Add the digits in even positions (2nd, 4th, 6th, etc.). Sum these two results. The check digit is the number that, when added to this sum, makes the total a multiple of 10. If the sum is 47, the check digit is 3, because 47 + 3 = 50, which is divisible by 10.
This check digit is a safeguard against scanning errors. If the scanner misreads a single digit, the check digit will not match, and the scan will be rejected. The system catches the error before it reaches the database. The modulo-10 algorithm catches all single-digit errors and about 90 percent of transposition errors (when two adjacent digits are swapped). This is a simple but effective error-detection system built into every barcode on every product.
04How the scanner reads the bars
A barcode scanner is, at its core, a light sensor. The most common type uses a laser diode to project a beam of light onto the barcode. The dark bars absorb the light, and the light spaces reflect it. A photodiode detects the reflected light and converts it into an electrical signal. As the scanner moves across the barcode, the signal rises and falls, creating a waveform that corresponds to the pattern of bars and spaces. The scanner measures the width of each pulse in the waveform and converts those widths into the encoded digits.
The scanner must determine the width of each bar and space relative to the module size. It does this by measuring the time between transitions from dark to light and vice versa. Since the scanner does not know how fast it is moving across the barcode, it cannot measure absolute widths directly. Instead, it uses the guard patterns and the relative timing of transitions to infer the module width and decode the digits. This is why the quiet zone and guard patterns are essential: they give the scanner the reference points it needs to calibrate its measurements.
A UPC-A encodes 12 digits across 95 modules, bracketed by guard patterns and quiet zones.
052D barcodes: from lines to squares
The linear barcode, while ubiquitous, has a fundamental limitation: it encodes only a small amount of data. A UPC-A holds just 12 digits. If you need to encode a URL, a paragraph of text, or a serial number with letters, you need a two-dimensional barcode. QR codes, invented by Denso Wave in 1994 for tracking automotive parts, are the most successful 2D barcode. A QR code can encode up to 7,089 numeric characters or 4,296 alphanumeric characters in a square matrix of black and white modules.
A QR code works on a different principle than a linear barcode. Instead of scanning a single line, the reader captures the entire 2D pattern. The code includes position detection patterns (the three large squares in the corners), timing patterns, alignment patterns, and data modules arranged in a specific encoding scheme with error correction. The error correction uses Reed-Solomon coding, which allows the QR code to be read even if up to 30 percent of the code is damaged or obscured. This is why QR codes work even when partially torn, smudged, or printed at an angle.
06Error correction and redundancy
Error correction is the unsung hero of barcode technology. In a 1D barcode, the check digit catches errors but does not correct them. If the scan fails, the operator tries again. In 2D barcodes like QR codes, Reed-Solomon error correction goes further: it can not only detect errors but reconstruct the missing data. This is possible because the encoded data includes redundant mathematical information computed from the original data. If some modules are damaged, the redundancy allows the decoder to reconstruct what was there.
QR codes offer four levels of error correction: L (7 percent recovery), M (15 percent), Q (25 percent), and H (30 percent). Higher error correction means more redundancy, which means the QR code can survive more damage but encodes less actual data for a given size. The choice depends on the application: a QR code on a printed poster that might be folded or torn uses H level, while a QR code on a screen uses L to maximize data capacity.
Higher error correction levels allow QR codes to survive more damage but reduce data capacity.
07The barcode in the world: from checkout to everything
The barcode was invented for grocery checkout, but it has spread far beyond that original purpose. Barcodes track packages through the postal system, identify patients in hospitals, track inventory in warehouses, manage library books, board passengers on flights, and link physical products to digital content through QR codes. The fundamental principle is always the same: encode an identifier in a machine-readable visual pattern, use a scanner to decode it, and use a database to look up what it means.
The barcode works because it is a bridge between the physical and the digital. A product on a shelf is a physical object. The barcode on that product is a physical pattern that encodes a digital identifier. The scanner reads the pattern and converts it into a number, which a computer uses to retrieve information from a database. Every time you scan a product at a checkout, you are using a system that connects a physical object to a digital record, and that connection is made through a pattern of lines that costs almost nothing to print.
By N43 and Hermes for Sailor Bob News.




