Timer-Based Width Measurement: The Art of Measuring Barcode Elements |
Executive Summary |
This article provides a comprehensive, accessible exploration of timer-based width measurement in barcode readers. We examine how microcontrollers use their timer and capture peripherals to measure the widths of bars and spaces in the digitized signal, enabling accurate decoding of barcode data. Rather than focusing on abstract theory, we ground every concept in concrete design examples and real patents from industry leaders including Microchip Technology, Symbol Technologies, and others. We explore the fundamental principle of measuring time intervals between signal transitions, the use of timers and capture units, the challenges of speed variations and their compensation, and the practical implementation of width measurement algorithms. The article covers both simple approaches and sophisticated techniques for handling the realities of hand-scanned barcodes. The closing summary synthesizes the key lessons and offers practical guidance for anyone implementing timer-based width measurement in barcode readers. |

|
Chapter 1: The Fundamental Measurement |
At its core, reading a barcode is about measuring widths. A barcode encodes data in the widths of its bars and spaces. A narrow bar might represent a binary zero, while a wide bar represents a binary one. Or, as in many symbologies, the ratios between adjacent elements encode the information. But in all cases, the decoder's first task is to measure these widths accurately. |
The digitized signal from the comparator is a stream of pulses. Each pulse corresponds to a bar or a space. The duration of each pulse---the time between a rising edge and a falling edge---is the width measurement. The microcontroller's timer/capture unit is the tool that makes this measurement possible. |
The concept is simple: when a transition occurs on the digitized signal, the timer's current count is captured. By subtracting successive captured values, the microcontroller determines the time elapsed between transitions. This time is proportional to the width of the bar or space. |
The timer operates at a known frequency, typically derived from the microcontroller's clock. This clock provides a consistent timebase, enabling absolute width measurements in units such as microseconds or timer ticks. The accuracy of the measurement depends on the timer's resolution and the stability of the clock source. |

|
Chapter 2: The Timer Capture Unit |
The timer capture unit is the key peripheral in a microcontroller for width measurement. It automates the process of capturing the timer value at a specific event, such as a rising or falling edge on an input pin. |
The operation of a capture unit is straightforward. The microcontroller's timer runs continuously, counting clock cycles. The capture unit monitors the input pin. When a configured event occurs---for example, a rising edge---the current timer value is latched into a capture register. An interrupt may be generated to alert the microcontroller that a new value is available. |
This approach has several advantages. The capture occurs in hardware, with no software overhead. The timer continues running, so no time is lost. The captured value is accurate and precise. |
In the context of a barcode reader, the digitized signal is connected to the capture input pin. Each transition captures the timer value. The microcontroller's interrupt service routine reads the capture value, calculates the elapsed time from the previous transition, and stores the width measurement in a buffer. This is the raw data that the decoder will use to reconstruct the barcode. |

|
Chapter 3: Measuring Pulse Widths in Practice |
A patent from Symbol Technologies describes a practical implementation of timer-based width measurement in a barcode scanner . The patent discusses the use of a microcontroller's timer circuit to measure pulse widths in a signal train generated by a BLDC motor's Hall Effect sensor. While the specific application is motor control, the principle is directly applicable to barcode reading: measuring time intervals between signal transitions. |
The patent explains that 'the time intervals t0 and t1 can be measured by a timer circuit that is already provided in the barcode scanner's microcontroller' . The term 'microcontroller' is used to refer to a microprocessor or other computing device that controls the scanner's operation. 'The timer circuit would measure the intervals between the same-edge transition points in the signal pulse train' . |
The measurement accuracy depends on the timer's clock speed. The patent notes that 'the microcontroller 60 also generally includes an oscillator 67 to control the operating frequency of the microcontroller and the clock speed of the timer circuit 65' . A higher clock speed provides finer resolution, enabling more accurate width measurements. |

|
Chapter 4: The Voting Algorithm for Robustness |
A key innovation in the Symbol Technologies patent is the use of a 'voting algorithm' to handle variations in the measured pulse widths . The algorithm addresses a common problem in barcode reading: the measured widths are not perfectly constant, due to speed variations, noise, and other factors. |
The voting algorithm works as follows: 'The voting algorithm utilizes a single counter, hysteresis counter that is incremented or decremented depending on whether the currently measured temporal pulse width is consistent with the currently presumed order of the two pulses' . If the measured width is consistent with the presumption, the counter is incremented. If it is inconsistent, the counter is decremented. |
The hysteresis counter prevents errors due to random variations. 'The hysteresis counter prevents errors in the presumed ordering of the pulse widths due to random errors in the measurements and minor speed variations' . If the counter reaches zero, the presumed ordering is reversed. |
This approach is robust. It allows the system to tolerate measurement errors and speed variations without losing the correct interpretation of the signal. The patent notes that 'after several iterations, it quickly becomes apparent which of the two pulse widths is actually shorter. At the same time, the algorithm effectively handles any anomalies in the measured data' . |

|
Chapter 5: Handling Speed Variations |
One of the biggest challenges in barcode reading is speed variation. When a user scans a barcode by hand, the speed is not constant. It may accelerate, decelerate, or jerk. This causes the measured widths to vary, even though the physical widths are constant. |
A patent from a bar code reader manufacturer addresses this challenge . The patent describes a method for determining a reference timer count from the signal width of a bar immediately before the present bar. The reference value is updated continuously, tracking the speed variations. |
The patent explains that 'the rotation speed of the bar code disc changes with the characteristics of each motor, the remaining capacity of a battery, a film roll diameter, a load change, a voltage change, or the like. As a result, time required for the unit width to pass the photo-sensor, i.e., a signal width of the unit width, cannot be fixed to a predetermined value' . |
The solution is to use a reference value derived from the preceding bar. 'A timer count T of a presently read bar is compared with the reference value (2*Tref) obtained from the signal width of the preceding bar to determine the logical value of the present bar' . This approach assumes that the speed changes gradually, so the preceding bar's width is a good predictor of the current bar's width. |

|
Chapter 6: Handling Edge Distortion |
Another challenge in width measurement is edge distortion. When the amplitude center of the photodetector signal shifts relative to the threshold, the measured widths can be distorted. The patent from the bar code reader manufacturer addresses this problem as well . |
The patent explains that 'the amplitude center of the photoelectrical signal may become lower or higher than the threshold value Th... This results from that an output level of the bar code sensor increases or decreases gradually and a transition time from the highest value to the threshold value Th differs from a transition time from the lowest value to the threshold value Th' . |
The solution is to use a reference that is less sensitive to this distortion. The patent describes an approach where 'a ratio of 1:1 or 3:1 is satisfied between the signal widths of only L levels or only H levels' . By comparing bars of the same type (black-black or white-white), the effect of threshold shift is canceled. |
The technique involves measuring the widths of consecutive bars and using the previous bar's width as a reference. The patent explains that 'it becomes possible to reliably judge a signal width by determining the reference timer count from the signal width of a bar two bars before the present bar having the same type as the present bar, even if the amplitude center is not coincident with the threshold value Th' . |

|
Chapter 7: Width Measurement in Scanning Systems |
The practical implementation of width measurement often involves storing a sequence of width data for later processing. A patent from Symbol Technologies (EP0840249B1) describes a system for extracting fragmentary pulse width data from a scanning beam and synthesizing a complete data train . |
The patent describes a 'fragmentary pulse width data train extractor' that scans a bar code label on a moving article and extracts pulse width data trains corresponding to the black and white bars . A 'synthesizing unit' then synthesizes a complete bar code pulse width data train from the extracted fragments based on position data . |
This is particularly relevant for moving article applications, where a single scan may not capture the entire barcode. The system collects fragments from multiple scans and assembles them into a complete data set. A decoding unit then converts the complete data train into numerals, characters, etc. . |
Chapter 8: Microchip's Implementation Reference Design |
Microchip Technology provides a comprehensive reference design for a barcode scanner implementation using an Arm Cortex-M4 MCU . This design illustrates modern, full-featured timer-based width measurement. |
The reference design 'shows how to create a barcode scanner application with an external camera sensor' . While camera-based, the principles of width measurement remain the same: the captured image is processed to extract bar and space widths, which are then fed to the decoder. |
The design uses Microchip's MPLAB Harmony software development platform and runs on either the PIC32CX SG61 Curiosity Ultra Evaluation Board or SAM E51 Integrated Graphics and Touch Curiosity Evaluation Kit . It 'uses a small camera to capture an image of the barcode, which is then processed by software running on a microcontroller (MCU) to decode the barcode' . |
This reference design is a good example of modern barcode scanner implementation, where the microcontroller handles both the image capture and the decoding, supported by a rich ecosystem of software libraries and drivers . |

|
Chapter 9: MCU-Based Imaging Code Readers |
An MCU-based imaging code reader is an increasingly popular architecture for barcode reading in consumer and cost-sensitive applications. The patent US20210027030A1 describes such a system, where a microcontroller with embedded memory performs the reading and decoding functions . |
The patent explains that 'the MCU-based code reader 114 is able to have a smaller footprint and is more cost-effective than a microprocessing unit (MPU)-based code reader' . The MCU includes an embedded memory that operates as an image buffer for image data . |
A key challenge with MCU-based systems is the limited memory. 'Conventional microcontrollers ... do not have as much storage capacity within the embedded memories as would be required for a full image frame' . The solution is to use subsampling: reading only a subset of the image data from the imager . |
Subsampling can be done by row skipping, column skipping, or reading a region of interest. The subsampled image data is stored in the embedded memory and processed to decode the barcode . This approach enables cost-effective barcode reading in a wide range of applications. |
Chapter 10: The Practicalities of Subsampling |
The subsampling technique described in the patent enables MCU-based barcode readers to operate with limited memory . The imager may have 640x480 pixels, but the embedded memory may only be 256 KB SRAM. Rather than storing a full image, the MCU reads a subset. |
As the patent explains, 'rather than reading the entire frame of the imager 300 into the embedded memory of the microcontroller, a subsample of the image captured by the imager 300 may be read' . This can be done by reading every 8th row, resulting in a 38.4 KB image rather than the full 307.2 KB . |
This is practical for 1D barcodes, which are essentially 1D patterns. The code reader can read multiple subsampled frames and, if necessary, stitch together data from separate frames to form a complete barcode . This is a clever way to achieve barcode reading with limited resources. |

|
Chapter 11: Width Measurement in Card Readers |
A patent on a card reader provides another detailed example of timer-based width measurement . The system uses a 1D CCD array to capture the barcode as the card is swiped. The width data is measured and stored in a FIFO memory for decoding. |
The patent describes that 'the data processing circuit 318 starts the pulse width count circuit 310' when the card is detected . The CCD array converts the optical signal to an electrical signal, and the binarization circuit converts it to a digital signal. 'The widths of bars and spaces are counted ... and the width data are sequentially stored in the FIFO memory 312' . |
A marker value, '00H,' is stored at the end of each scan to indicate the end of the width data string . This allows variable-length width data strings to be stored efficiently, without requiring a fixed memory allocation for each scan. This is a practical technique for handling the unknown length of barcode data. |
Chapter 12: Decoding from Width Data |
Once the width data is captured, the decoding process begins. A patent from MSI Data Corporation describes a method for decoding linear bar coded information using a logarithmic time base generator . |
The patent describes that 'the ratio of the widths of the coding segments are computed for decoding the bar coded data by utilizing a logarithmic time base generator for providing a logarithmic measure of the widths of preselected coding segments' . The logarithmic time base is used to compute the ratios, which are then compared to a translation table. |
This approach is efficient for decoding barcodes where the ratios between element widths are the key information. The logarithmic representation simplifies the comparison and reduces the computational requirements. |

|
Chapter 13: Practical Software Implementation |
In practice, timer-based width measurement is implemented in the microcontroller's firmware. A typical implementation involves an interrupt service routine for the capture event, a buffer to store the measured widths, and a decoding algorithm that processes the data. |
A forum discussion from AutoHotkey illustrates a simplified approach to barcode input capture using software timers . While not a dedicated barcode reader, the code demonstrates the principle of measuring the time between inputs to identify barcode data. |
The code captures keyboard input and measures the time between keystrokes. If the time between successive keystrokes is less than a threshold, the input is considered part of a barcode . This is a practical demonstration of the timer-based approach, even if not optimized for hardware performance. |
Chapter 14: The Importance of Timer Resolution |
The accuracy of width measurement depends on the timer's resolution. A higher resolution allows more precise measurement of narrow bars and better discrimination between different widths. |
The timer resolution is determined by the clock frequency of the timer. A 16-bit timer running at 1 MHz has a resolution of 1 microsecond. A 32-bit timer running at 100 MHz has a resolution of 10 nanoseconds. The required resolution depends on the barcode density and the scanning speed. |
The Symbol Technologies patent notes the importance of 'appropriate accuracy to measure the temporal pulse widths between signal transitions' . The timer must be able to distinguish between narrow and wide bars, even with speed variations and noise. |

|
Chapter 15: Edge Detection and Pulse Width Measurement |
The edge detection and pulse width measurement are tightly coupled. The system must detect the transitions accurately to measure the widths correctly. |
The bar code reader patent describes the use of a waveform shaper to convert the analog signal to a digital pulse train . The waveform shaper uses a threshold to distinguish between bars and spaces. The timer measures the width of each pulse in the digital signal. |
The accuracy of the edge detection affects the accuracy of the width measurement. If the threshold is not set correctly, the edges will be shifted, and the measured widths will be distorted. The patent describes techniques for compensating for threshold shifts to maintain accurate width measurement . |
Chapter 16: Speed Compensation Techniques |
Speed compensation is a critical aspect of timer-based width measurement. The measured widths are directly proportional to the scanning speed. If the speed changes, the measured widths change, even though the physical widths are constant. |
The bar code reader patent describes a speed compensation technique that uses the width of the preceding bar as a reference . This assumes that the speed changes gradually, which is a reasonable assumption for hand-scanned barcodes. The patent also describes techniques for using the widths of multiple preceding bars to improve the estimate . |
The Symbol Technologies voting algorithm provides another form of speed compensation. By comparing the measured widths over multiple revolutions, the algorithm determines the correct order of the pulses, despite speed variations . |

|
Chapter 17: Dealing with Noise and Anomalies |
The measured width data is often contaminated by noise. The voting algorithm described in the Symbol Technologies patent handles anomalies by using a hysteresis counter . If a measurement is inconsistent with the presumed order, the counter is decremented but not reset. This allows the system to tolerate occasional errors. |
The patent explains that 'the algorithm effectively handles any anomalies in the measured data. For example, ... the temporal pulse width t1 measured during the first revolution is an anomaly... But the measurements made in the subsequent revolutions confirmed the initial presumption of the ordering' . The voting algorithm provides a robust method for extracting the correct data from a noisy signal. |
Chapter 18: Hardware Implementation of Width Measurement |
Some barcode readers use dedicated hardware for width measurement, rather than relying on the microcontroller's timer peripherals. The card reader patent describes a 'pulse width count circuit' that is a dedicated hardware block . The circuit counts the widths of bars and spaces in synchronization with the operation clocks of the CCD array. |
Hardware implementation can provide higher speed and lower latency than software-based measurement. The width data is stored directly in a FIFO memory, ready for processing by the data processing circuit. This is particularly important for high-speed scanning applications where the microcontroller may not be able to keep up. |

|
Chapter 19: Software Implementation of Width Measurement |
In many modern barcode readers, width measurement is performed in software, using the microcontroller's timer/capture peripherals. This is a flexible approach that can be easily updated and modified. |
A forum developer describes a software implementation using a timer and an interrupt: 'I use only one interrupt. Timer only used to calc bar width. (Toggle interrupt setting - raise/fall every time an interrupt is occurred)' . The interrupt service routine calculates the bar width from the timer value and stores it in a buffer. |
This approach is efficient and uses minimal hardware resources. The interrupt service routine is short and fast, allowing the microcontroller to handle other tasks while the barcode is being scanned. |
Chapter 20: The Decoder's Role |
The decoder is the final stage in the processing chain. It takes the width data and converts it into the data encoded in the barcode. The decoder algorithm depends on the symbology being used. |
The decoder may use the width data directly, or it may compute the ratios of adjacent elements. The MSI Data Corporation patent describes a method for computing the ratios of the widths using a logarithmic time base . The ratios are then compared to a translation table to determine the encoded data. |
The accuracy of the width measurement directly affects the decoder's ability to decode the barcode. If the widths are measured accurately, the decoder can reliably decode the data. |

|
Chapter 21: Real-World Barcode Scanner Module |
The Waveshare Barcode Scanner Module provides a modern, practical example of a barcode scanner system . The module uses an image sensor to capture barcode images and advanced image recognition algorithms to decode them. |
While this module uses image processing rather than a simple digitized signal, the underlying width measurement principle is the same. The image processing algorithm extracts the bar and space widths from the captured image. The module supports a wide range of 1D and 2D formats and can be configured via software scripts . |
The module supports cross-platform development, with sample code for Arduino, Raspberry Pi, and Raspberry Pi Pico . This demonstrates the widespread availability of barcode scanning technology and the importance of software flexibility in modern systems. |
Chapter 22: The Evolution of Width Measurement Technology |
Barcode width measurement technology has evolved significantly. Early systems used analog techniques, such as the logarithmic time base generator described in the MSI Data Corporation patent . Later systems used digital counters and timers. |
Modern systems use microcontrollers with high-resolution timers and capture units. The trend is toward greater integration, with the entire barcode reading function, including width measurement and decoding, implemented on a single chip. |
The image-based barcode scanners, such as the Waveshare module, represent the latest evolution, using image processing to extract width information . This approach provides greater flexibility and can handle 2D codes, such as QR codes and Data Matrix codes. |

|
Chapter 23: Conclusion |
Timer-based width measurement is a fundamental technique in barcode reading. It enables the accurate measurement of bar and space widths, which is essential for decoding barcode data. |
We have examined the principles of timer-based width measurement, the use of timer capture units, and the practical implementation in real barcode readers. We have explored the challenges of speed variations and edge distortion, and the techniques for compensating for these challenges. |
The future of barcode width measurement is likely to be driven by continued improvements in microcontroller technology and image processing algorithms. The fundamental principle of measuring widths, however, will remain at the heart of barcode reading. |
Chapter 24: Practical Guidance for Implementation |
For designers implementing timer-based width measurement, the following guidance is recommended: |
First, select a microcontroller with a timer/capture unit that has sufficient resolution for the required barcode density and scanning speed. A higher clock frequency provides better resolution. |
Second, implement a robust edge detection algorithm that can handle noise and threshold shifts. The waveform shaper should be designed to produce clean, well-defined edges. |
Third, use a buffer to store the measured widths. A FIFO or ring buffer is a good choice, as it allows the microcontroller to process the data while continuing to capture new measurements. |
Fourth, implement a decoding algorithm that is appropriate for the barcode symbology. The algorithm should be robust enough to handle speed variations and noise. |
Fifth, consider using a voting algorithm or hysteresis counter to handle measurement anomalies. This can significantly improve the reliability of the decoder. |

|
Chapter 25: Common Pitfalls and Solutions |
There are several common pitfalls in timer-based width measurement: |
One pitfall is incorrect timer configuration. The timer must be configured with the correct clock source and prescaler to achieve the desired resolution. The capture unit must be configured for the correct edge. |
Another pitfall is inadequate buffering. If the buffer is too small, data may be lost before it can be processed. The buffer size should be sufficient to handle the maximum expected data rate. |
A third pitfall is insufficient compensation for speed variations. The measured widths will vary with speed. The decoder must account for this, either by using a dynamic reference or by normalizing the widths. |
A fourth pitfall is noise in the digitized signal. The waveform shaper must be designed to produce a clean signal. The comparator should include hysteresis to prevent chatter. |
By avoiding these pitfalls, designers can implement a robust and reliable timer-based width measurement system. |
Chapter 26: Summary --- Timer-Based Width Measurement in Perspective |
Timer-based width measurement is an essential technique for barcode reading. It provides a simple, reliable method for measuring bar and space widths, enabling accurate decoding of barcode data. |
We have examined how the technique is implemented in practice, using microcontrollers with timer/capture peripherals. We have explored the challenges of speed variations and edge distortion, and the techniques for overcoming these challenges. |

|
The key lessons from our exploration are: |
Timer-based measurement is simple and effective. The timer captures the value at each signal transition. Subtracting successive values gives the width. |
Speed variation is a key challenge. Hand-scanned barcodes vary in speed. A dynamic reference, such as the width of the preceding bar, can compensate. |
Edge distortion can distort the measurement. The amplitude center of the signal may shift relative to the threshold, causing the measured widths to be inaccurate. Comparing bars of the same type can cancel this effect. |
A voting algorithm can handle anomalies. By using a hysteresis counter, the system can tolerate occasional measurement errors without losing the correct interpretation. |
The decoder relies on accurate width data. The width measurement must be accurate for the decoder to correctly interpret the barcode. |
In the end, timer-based width measurement is a testament to the power of simple, robust engineering. It is a technique that has been used in barcode readers for decades, and it remains the foundation of reliable barcode decoding. The art of timer-based width measurement lies in the careful balance of resolution, speed compensation, and noise rejection, creating a system that can reliably measure widths in the face of real-world variations. |