AutomateDirectUSAMotion LibraryIndustrial monitors & PCs

How-to

How to Track Conveyor Parts With an Encoder and a FIFO Buffer

Sensors, encoders and machine vision on the machine Published 8 min read By AutomateDirectUSA engineering team

Short answer

Load one FIFO element per slot of encoder-measured belt travel and read any element by index arithmetic instead of shifting data down a table. Worst-case registration error equals one slot length, and update cost is constant whether the block holds 100 or 100,000 elements. Size the slot from placement tolerance and the block from sensor-to-actuator distance plus 10 to 20 percent margin.

On this page
  1. Why shifting data fails at thousands of positions
  2. Move the pointer, not the data
  3. Size the slot, the block and the load rate
  4. Worked example: 12 m of belt at 1.5 m/s
  5. Load on encoder distance, not on a timer
  6. Read the slot under each actuator
  7. When tracking drifts: symptom, cause, check
  8. Questions asked

Track the belt, not the clock. Divide the conveyor into slots of equal length and, every time the encoder reports one slot of travel, load the current sensor readings into one element of a FIFO implemented as a circular buffer. The actuator never shifts data and never unloads its way down to a deep element: it converts its distance behind the sensor into an index and reads that element directly. Two numbers set the design. Slot length sets the worst-case registration error, which is exactly one slot. Block size is the sensor-to-actuator distance expressed in slots, plus 10 to 20 percent margin. Because loads and reads move pointers instead of copying data, a 100,000-element table updates as cheaply as a 100-element one.

Why shifting data fails at thousands of positions #

Four approaches each break on a long, variable-speed line:

  • A rung per position — move slot 9 to 10, 8 to 9, 7 to 8 — is maintainable at ten slots and hopeless at a thousand, and the whole chain executes on every slot event.
  • A single block-move instruction replaces the thousand rungs but still copies the entire table down one element on every event. Its cost grows with table length and belt speed, which are the two things you are trying to increase.
  • A bit-shift register is fast but carries bits only. Pass/fail flags or three bin codes in parallel registers work; a weight, a thickness, a lane ID or a timestamp does not fit.
  • Clocking any of these from a timer or from scan time ties the record to time instead of distance. The moment belt speed changes, the same number of shifts covers a different length of belt and the registration walks off the product.

Move the pointer, not the data #

A FIFO built as a circular buffer keeps two indices into ordinary memory: a head, where the next load writes, and a tail, where the next unload reads. Loading writes one element at the head and increments the head; unloading reads the tail and increments the tail. Nothing is copied, so the cost per load is constant.

The block is plain word memory, so any element can be read at any time with indirect addressing, without disturbing either pointer. The unload path exists to free space, never to retrieve data. Most current PLC FIFO instructions work exactly this way; confirm on yours by jogging the belt one slot at a time and watching the two pointers. If they wrap back to the start while the data stays put, you have a circular buffer. If the data physically moves, write the two-pointer logic yourself — it is a handful of rungs.

Size the slot, the block and the load rate #

  1. Set the slot length from placement tolerance. The record changes only at slot boundaries, so the worst-case registration error is one slot length. Rule of thumb: keep the slot no larger than half the actuator's placement window and no larger than half the minimum gap between parts.
  2. Convert to encoder counts: counts per slot = counts per millimetre × slot length in millimetres. Round down to a whole count and keep the fraction in the position clock; the load loop depends on it.
  3. Size the block: elements = sensor-to-actuator distance ÷ slot length, plus 10 to 20 percent so a record in transit is never recycled before it is used.
  4. Allocate one word block per data field. A weight field, a thickness field and a lane-ID field are three blocks, all written at the same index on the same slot event.
  5. Check the load rate against the scan: slot events per second at top speed = top speed ÷ slot length. If events exceed scans, one pass of the load loop must handle several slots.
  6. Check the total against controller memory: word tables of a few thousand to a few tens of thousands of elements are routine planning territory, and the exact element limit comes from the controller's memory documentation.

Worked example: 12 m of belt at 1.5 m/s #

Inputs for this calculated example — substitute your own:

  • Measuring wheel of 200 mm circumference with a 2,048-count encoder: 2,048 ÷ 200 = 10.24 counts per mm.
  • Placement tolerance ±10 mm, so a 20 mm window and a slot of at most 10 mm; minimum part gap 60 mm allows up to 30 mm. Chosen slot: 5 mm.
  • Sensor-to-actuator distance 12 m = 12,000 mm; top belt speed 1.5 m/s = 1,500 mm/s.

The arithmetic:

  • Counts per slot: 10.24 counts/mm × 5 mm = 51.2, so 51 whole counts with 0.2 count carried.
  • Slots in transit: 12,000 mm ÷ 5 mm = 2,400.
  • Block size: 2,400 × 1.2 = 2,880 elements per field.
  • Load rate: 1,500 mm/s ÷ 5 mm = 300 loads per second.
  • The block-move alternative would copy 2,400 words × 300 per second = 720,000 words per second, and more the day the table or the belt speed grows. The circular buffer performs 300 pointer updates per second and does not care whether the block is 2,880 or 28,800 elements.
  • Discarding the 0.2-count remainder instead of carrying it costs 0.2 × 2,400 = 480 counts by the time a record reaches the actuator: 480 ÷ 10.24 = 46.9 mm of drift.

Decision for this line: 5 mm slots put the worst-case registration error at 5 mm, half of the one-sided ±10 mm allowance, leaving the rest for sensor timing and belt slip. If the tolerance were ±2 mm, halve the slot to 2 mm: 12,000 ÷ 2 = 6,000 slots, × 1.2 = 7,200 elements, and 1,500 ÷ 2 = 750 loads per second. A 10 ms scan then absorbs 7 to 8 slots per pass, which the loop handles.

Load on encoder distance, not on a timer #

Read the raw counter once per scan and let a loop decide how many slots to load:

// Pseudocode; names are illustrative, not instruction names.
EncoderCount  : DINT := 0    // raw encoder counter, read once per scan
LastLoad      : DINT := 0    // counter value after the most recent load
CountsPerSlot : DINT := 51   // from the sizing steps
SensorWord    : WORD := 0    // field value captured at the sensor
Delta         : DINT := 0

Delta := EncoderCount - LastLoad;
WHILE Delta >= CountsPerSlot DO
    LoadOneElement(SensorWord);            // writes at head, bumps head
    LastLoad := LastLoad + CountsPerSlot;  // advance by whole slots
    Delta    := Delta - CountsPerSlot;
END_WHILE

Three details in that loop earn their keep. Signed subtraction of raw counters survives counter rollover without special cases, the same discipline behind counting encoder pulses on a PLC without losing position. Advancing LastLoad by whole slots instead of snapping it to the live counter preserves the fractional remainder — snapping it is what produces the 46.9 mm drift above. And clamp Delta to a sane ceiling, ten slots for example, so one burst of encoder noise cannot load thousands of elements in a scan and wipe the table. If you want an even sampling interval, read the counter in a timed interrupt, but keep the load gated on distance, never on the interrupt tick.

Bench test that proves the loop: jog the belt exactly 12 mm, about 123 counts at 10.24 counts per mm. The loop loads twice — LastLoad advances 0 to 51 to 102 — and stops with Delta = 21, about 2 mm correctly carried toward the third slot. Jog another 5 mm, roughly 51 counts, and Delta reaches 72: one load fires and the carried remainder returns to 21. Two jogs, three loads, no drift.

Read the slot under each actuator #

The newest element sits one index behind the head, so the element k slots back is at (head − 1 − k) modulo the block size. The actuator 2,400 slots downstream of the sensor reads k = 2,400 every scan, directly, with no unloading. Do not assume a base of zero or one from a help file: jog the belt one slot at a time, watch the pointers wrap to the first element, and confirm your modulo arithmetic against what you see.

Several sensors fall out of the same structure. Drive one position clock, write every field into its own block on the same event, and the readings for one physical piece of belt sit at index offsets equal to physical separation ÷ slot length: a gauge 6 m downstream of another sits 6,000 ÷ 5 = 1,200 index positions away, so comparing them is a subtraction of indices, not a timing exercise. Several actuators each compute their own k into the same block on the same scan. When the downstream device is a robot picking parts off the moving belt, the encoder takes on a second job and robot conveyor tracking encoder wiring becomes its own task.

One boundary: the tracking table is control logic, not a safeguard. Anything that must stop the belt or keep people out runs through the machine's interlocks and its risk assessment, never through tracking data.

When tracking drifts: symptom, cause, check #

Symptom Likely cause Confirming check or fix
Actuator fires early when the belt slows, late when it speeds up Loads clocked by timer or scan, not by distance Log loads per second against belt speed; gate loads on the count delta
Placement error grows over a shift, resets on restart Fractional counts per slot discarded, LastLoad snapped to the live counter Watch the gap between LastLoad and the counter grow; advance by whole counts
Scan time climbs as tracking positions are added Whole-table copy on every event Replace the block move with head and tail pointers
Tracking freezes after the belt has run a while Buffer full, further loads rejected Unload one element for every load once full
Reject hits the neighbouring part when parts run tight Slot longer than the minimum part gap Shrink the slot and grow the block to keep the same coverage
Data is right at the sensor, wrong at the actuator Block shorter than the longest offset Resize to the longest distance plus margin

Questions asked

How large can the tracking table get before it becomes a problem?

Length is a memory decision, not a speed one: a circular-buffer load costs one pointer update whether the block holds 100 elements or 100,000. Word tables of a few thousand to a few tens of thousands of elements are routine planning territory on current mid-range controllers. Take the exact element limit from your controller's memory documentation before committing to a six-figure table.

What happens when the buffer fills up?

Nothing, provided you unload one element for every load once the full condition is detected. The tail element is always the oldest record, the piece of belt farthest downstream, so with the block sized to the longest offset plus margin the discarded record has already served its purpose. Some controllers clear the unloaded slot, so a zeroed element at the tail is a quick confirmation the unload path runs.

My scan time is 100 ms. Is that fast enough to track at 1.5 m/s?

Yes, because the loop catches up in whole slots, not one per scan. At 1.5 m/s with 5 mm slots, 100 ms of travel is 150 mm, or 30 slots, and one pass loads all 30. The hard limit is advancing a full block between reads: 2,880 elements cover 14.4 m of belt, nearly a hundred times that 150 mm. For even sampling, read the counter in a timed interrupt but keep the load gated on distance.

Do I need to handle encoder counter rollover?

Take the difference of raw counts as a signed 32-bit value and rollover needs no special case. In the worked example, a 32-bit counter wraps after about 210 km of belt: 2,147,483,648 counts at 10.24 counts per mm is 209,715,200 mm. Keep the subtraction signed and the noise clamp in place, and the wrap passes through the same arithmetic as any other delta.

Keep reading

  • How-to

    Robot Conveyor Tracking: Wire the Encoder to the Controller

    Yes, a PC application can feed part data to a tracked conveyor pick, but it must never own position. Terminate the quadrature encoder and the part-detect proximity switch on the robot controller's conveyor tracking input so the switch edge latches the count in hardware; the PC sends only class, offsets and grip choice. Live coordinates streamed through a PC arrive stale by the round trip, and at 300 mm/s every 10 ms costs 3 mm at the gripper.

    Sensors, encoders and machine vision on the machine

  • How-to

    Sizing a Spindle Encoder Input for Servo Feed Control

    A 2000 PPR quadrature encoder on a 250 RPM spindle produces 33,333 counts/s, which fits a 50 kHz counter but overruns a 10 kHz variant; the most resolution that counter family allows is 3000 PPR. Scale feed per revolution to motor RPM through screw lead and gear ratio, then keep command pulses per revolution low enough that rapid speed stays under the 100 kHz pulse-output ceiling.

    Sensors, encoders and machine vision on the machine

  • How-to

    Connecting a 5V Servo Encoder to 24V PLC Inputs

    No interface can raise the PLC input's own frequency limit, so a 20 kHz optocoupler board does not make a slower high-speed input faster. Compute f = RPM × pulses per revolution ÷ 60 first; if it exceeds the input's documented maximum, use a faster counter input. If it fits, translate the 0-5 V signal through an optocoupler whose LED current, output topology and switching times all check out.

    Sensors, encoders and machine vision on the machine