Sum eight raw scale readings, subtract one maximum and one minimum, and divide the rest by six. Capture all eight on an edge trigger, trim in raw counts, convert to weight once, and check the group with a worked example: 32054 counts trimmed to 24054, 4009 counts, 5011.25 g.
Drop one highest and one lowest reading from a group of eight scale samples, sum the remaining six, and divide by six. That trimmed mean is what a check scale wants when single raw readings spike. The logic has four parts: capture eight raw counts into fixed registers on an edge trigger, find one maximum and one minimum, subtract each from the eight-sample sum, and divide by six. Trim in raw counts and convert to weight once, at the end. Average all eight and a spike skews the result. Convert each sample first and an integer PLC adds eight roundings the filter never asked for. The steps below build the logic, the worked example carries real numbers through it, and the last section checks the result before it reaches the operator panel.
What you need
- Eight word registers for the raw samples, so sample 1 through sample 8 always sit in the same place.
- A 32-bit register for the sum. A signed 16-bit register tops out at 32767, and eight full-scale samples reach 8 x 32767 = 262136, which overflows it.
- A sample counter that runs 0 to 8, a result-valid bit, and registers for the minimum, maximum, trimmed sum and trimmed mean.
- Rising-edge detection on the capture trigger.
- The raw channel address from your analog input card's configuration; the card manual names the register that carries the current reading.
- Your counts-to-weight factor from the scale calibration. The worked example below uses 8000 counts over a 10.000 kg span, which is 1.25 g per count.
Capture eight raw readings into one group
- Start each cycle on the rising edge of the package or operator trigger. Clear the sample counter and the result-valid bit at that edge, once.
- If the scale has a stability or motion output, use it as the store enable, so the eight readings come from the settled window.
- While the store enable is on and the counter is below 8, copy the raw channel value into the next sample register and increment the counter. Small PLCs commonly scan in 1 to 10 ms, so one reading per scan puts all eight inside 8 to 80 ms, which fits the settled window most check scales give a package.
- Stop storing when the counter reaches 8. The eight stored values stay untouched until the trim finishes.
- When the counter equals 8, run the trim and set the result-valid bit.
Gate the cycle start on the edge, never on the level. A trigger that stays on for several scans fills slot after slot with the same reading if you wire the level straight to the store enable. It is the same timing trap that makes an HMI trend chart record fewer samples than its trigger.
Find one maximum and one minimum, then trim
- Copy sample 1 into both the minimum and the maximum register.
- Compare samples 2 through 8 against both registers: a sample below the minimum replaces it, a sample above the maximum replaces it.
- Sum all eight samples into the 32-bit register.
- Subtract the maximum once, then subtract the minimum once.
- Divide the trimmed sum by 6 and store it as the trimmed mean.
- Set the result-valid bit.
Six compare-and-replace pairs do the whole search, so there is no sort and no second sample table. Many PLCs also carry minimum and maximum instructions that replace step 2. If two samples tie at the maximum, subtracting the maximum value once still leaves the twin in the sum: the trim removes one occurrence of each extreme, and that is the design.
The divisor is 6 because the trim always leaves 6. If you later move to a variable group size, divide by the count you actually captured; a knowledge-base note on dividing an array sum by the actual sample count shows what goes wrong when the divisor and the count drift apart.
Worked example: eight raw counts through the trim
Example inputs: the analog card reports 0 to 8000 counts across the load cell's 0 to 10.000 kg span, so 1 count = 1.25 g. A package passes, the motion output clears, and the capture stores these eight raw counts:
| Sample | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Raw counts | 4012 | 4008 | 4015 | 4002 | 4210 | 4006 | 4011 | 3790 |
The arithmetic, one line per rung:
- Maximum 4210 counts (one spike), minimum 3790 counts (one dropout).
- Sum of all eight: 4012 + 4008 + 4015 + 4002 + 4210 + 4006 + 4011 + 3790 = 32054 counts.
- Trimmed sum: 32054 - 4210 - 3790 = 24054 counts.
- Trimmed mean: 24054 / 6 = 4009 counts.
- Plain average of all eight: 32054 / 8 = 4006.75 counts, and an integer-only divide returns 4006. The two bad samples pull the plain mean 2.25 counts low, and 2.25 counts x 1.25 g = 2.8 g. On a check scale with a ±5 g band, two bad readings eat more than half the band.
- Convert once: 4009 counts x 1.25 g = 5011.25 g, shown to the operator as 5.011 kg.
Convert once, at the end
The factor is linear, so in exact arithmetic filter and conversion land on the same number. A PLC divides in integers, so every conversion rounds. Converting each of the eight samples to weight first adds eight roundings, and the trim then averages values that have already drifted. One conversion of the trimmed mean carries a single rounding, and it leaves the raw trimmed mean in its own register, so a wrong weight is easy to split into a sampling problem or a scaling problem.
When the conversion needs an offset as well as the factor, keep the offset out of the trim: removing an offset in HMI scaling using raw counts works through that raw-count math. The empty container gets the same treatment at the raw end: capture its readings the same way and subtract its trimmed mean, which is the job tare a scale signal from an analog input in PLC logic walks through. If your raw register carries an implied decimal point, trim the raw values untouched and let the display place the decimal; setting trend graph limits for implied decimal PLC values shows the same counts-to-value arithmetic.
How to check it worked
- Put the eight test counts from the worked example into the sample registers offline, or feed them in through a test pattern. The trimmed mean must come out at 4009 counts.
- Watch one cycle in the programming software: the counter stops at 8, the stored samples stay as entered, and the minimum, maximum, sum and trimmed sum match your hand calculation from the worked example.
- Hold the trigger on for ten scans. The edge must fire once, so each slot fills one time and the counter still stops at 8.
- Put two 4210 values in one test group. The trim must remove one of them, and the result must match a hand calculation of that group.
- Start a second cycle right after the first. The result-valid bit must clear before capture restarts, so no stale weight survives to the next package.
- Weigh a known test mass and compare the displayed number against the scale's own calibration procedure.
Keep the trim out of any automatic reject or divert decision until these checks pass with known test weights on the line. And if the six middle readings disagree by more than a few counts, fix the load cell wiring, shield ground or mounting first: a filter that tidies a noisy signal is hiding the fault, not curing it.
Recommendation: For a check scale that takes eight readings per package, trim in raw counts and convert once. Edge-start each cycle, store one reading per scan until the counter reaches 8, sum all eight, subtract one maximum and one minimum, divide by 6, then apply the counts-to-weight factor to that single number. Use the 32-bit sum register whatever the card's range, and keep the result out of automatic reject logic until the checks pass on known test weights.
Questions
How do I extend the trim to more than eight readings?
Keep the same pattern with a wider group: capture n readings, subtract one maximum and one minimum, and divide by n minus 2, so ten readings leave eight in the average. Move to the 32-bit sum register first if you have not: ten full-scale 16-bit samples reach 327670, well past a 16-bit sum.
What if two spikes land on the same side in one group?
The trim removes one high and one low per group, so two high spikes in the same eight leave the second one in the six-reading sum. Burst noise needs a wider group or a cleaner signal first: check the load cell cable routing, the shield ground and the mechanical mounting for vibration.
Should the reject station act on this average?
Use the trimmed mean for the log and the operator display. The reject or divert decision on a check scale follows the scale's own calibration and tolerance procedure, and it usually has to land inside one package pass, not after eight stored samples. Let nothing automatic act on the number until the checks pass with known test weights.
Why does my trimmed mean come out one count low?
An integer divide drops the remainder. A trimmed sum of 24055 divided by 6 gives 4009.17, and an integer-only PLC returns 4009. If the fraction matters on your check band, use the floating-point divide most PLCs carry, or keep the remainder register and round it into the log entry.
Planning information, not legal or engineering advice. Rules differ by place: check codes and permits with your local authority and your licensed trades.