In automated production lines, accurate counting is a significant challenge. Whether it’s tracking product quantities, monitoring raw material consumption, or counting equipment cycles, the function of “counting” is crucial yet often underestimated.Today, I want to share a technique that many experienced electricians overlook—how to utilize PLC data registers to achieve advanced counting functions, elevating your production line management to the next level.
What is a Data Register?
Don’t be intimidated by the term “register.” Think of it as a special “digital storage cabinet.” A regular counter is like a simple mechanical counter that can only increment or decrement by one; whereas a data register is an “intelligent storage cabinet” that can hold values and perform various operations.
In Mitsubishi PLCs, data registers typically start with a D (e.g., D0, D1), while Siemens S7 series uses variables in DB blocks. Each data register can store a 16-bit or 32-bit integer value, depending on your PLC model.
The most powerful aspect of data registers is that they can not only store numbers but also perform addition, subtraction, multiplication, division, comparisons, bit operations, and more!
Basic Counting Circuit Design
Let’s look at a simple production line counting circuit (using Mitsubishi FX series as an example):
LD X0 // Read sensor input signal X0
AND M0 // AND operation with M0 (rising edge detection auxiliary relay)
OUT M1 // Output result to M1 (trigger counting flag)
LD X0 // Read X0 again
OUT M0 // Update M0 state for the next scan cycle to detect rising edge
LD M1 // Read trigger counting flag
INC D0 // Increment D0 register by 1 (count increases)
RST M1 // Reset M1, prepare for the next count
To explain briefly: when the sensor detects a product passing (X0 changes from OFF to ON), we detect this rising edge signal and increment D0 to achieve counting.
Note: The logic using M0 and M1 is to prevent a product from being counted multiple times, which is a detail that many beginners easily overlook!
Advanced Counting Technique: Batch Management
Is a regular counter not enough? Try this batch management implementation method:
LD X0 // Sensor input
AND M0 // Rising edge detection
OUT M1 // Trigger counting flag
LD X0
OUT M0 // Update rising edge detection state
LD M1 // When a new product is detected
INC D0 // Increment single product counter by 1
LD= D0 K12 // Check if it reaches 12 (one box)
RST D0 // If so, reset single product count
INC D1 // Increment box count counter by 1
RST M1 // Reset trigger flag

This program implements the counting logic of “12 products packed into 1 box.” When D0 reaches 12, it increments the box count counter D1 and resets D0.
Practical Case: Shift Production Statistics
In actual production, we often need to count production by shifts. Assume there are three shifts in a day: morning shift (6:00-14:00), afternoon shift (14:00-22:00), and night shift (22:00-6:00).
First, set the shift judgment conditions (using R clock or external clock module):
// Read current hour into D10
// Determine shifts
LD>= D10 K6 // Greater than or equal to 6 o'clock
LD< D10 K14 // Less than 14 o'clock
AND // AND both conditions
OUT M10 // M10=ON indicates morning shift
LD>= D10 K14 // Greater than or equal to 14 o'clock
LD< D10 K22 // Less than 22 o'clock
AND // AND both conditions
OUT M11 // M11=ON indicates afternoon shift
LD>= D10 K22 // Greater than or equal to 22 o'clock
LD< D10 K24 // Less than 24 o'clock
OR // OR both conditions
LD>= D10 K0 // Greater than or equal to 0 o'clock
LD< D10 K6 // Less than 6 o'clock
AND // AND operation
OR // OR with previous condition
OUT M12 // M12=ON indicates night shift
Then use different registers to record the production output for each shift:
LD X0 // Sensor signal
AND M0 // Rising edge detection
OUT M1 // Trigger counting
LD X0
OUT M0
LD M1 // Product detected
AND M10 // Morning shift
INC D20 // Increment morning shift count by 1
LD M1
AND M11 // Afternoon shift
INC D21 // Increment afternoon shift count by 1
LD M1
AND M12 // Night shift
INC D22 // Increment night shift count by 1
RST M1 // Reset trigger flag
Data Backup and Recovery
Will the data in the registers be lost after a power outage? It depends on your PLC model and settings. Some PLCs have a power-off retention feature that can be configured to determine which registers need to be retained during power loss.
A safer approach is to periodically write important data to EEPROM or use special register areas that retain data during power loss:
// Periodic data backup (e.g., once every hour)
LD X1 // Backup trigger signal (can connect to timer or external trigger)
DMOV D0 D100 // Copy the value of D0 to D100 (assuming D100 is a power-off retention area)
DMOV D1 D101
DMOV D20 D120
DMOV D21 D121
DMOV D22 D122
Warning: Frequent writing to EEPROM will reduce its lifespan! Generally, the number of write cycles for PLC EEPROM is limited, so it is recommended to set a reasonable backup interval.
Interaction Display with HMI
Once the counting data is stored in registers, it usually needs to be displayed on a Human-Machine Interface (HMI). Simply add a numeric display control in the HMI software and point its address to the corresponding register:
- Total Count: D0
- Box Count: D1
- Morning Shift Output: D20
- Afternoon Shift Output: D21
- Night Shift Output: D22
You can also add buttons on the HMI for resetting the counters:
// Reset button (HMI input point X2)
LD X2
MOV K0 D0 // Clear counter
MOV K0 D1
Common Issues and Solutions
1. Inaccurate Counting
This is often caused by sensor signal jitter or improper PLC scan cycle settings. Solutions include:
// Increase debounce delay
LD X0 // Sensor signal
AND T0 // Combine with delay timer
OUT M1 // Trigger counting
LD X0
OUT T0 K5 // Set 50ms delay (specific time depends on your PLC time base)
I encountered a case in a carton factory where the count was always 1-2 more than expected. After investigation, I found that the sensor was improperly installed, causing one product to be detected multiple times. After adjusting the sensor position and adding a 50ms debounce delay, the issue was resolved.
2. Data Loss on Power Outage
If the PLC does not support power-off retention, you can:
- Use a PLC with battery backup
- Add a UPS power supply
- Periodically save data to an SD card or database
3. Data Overflow
The maximum value for a 16-bit data register is 32767; if the count exceeds this value, it will overflow. The solution is to use a 32-bit double word (DWORD) register:
LD X0 // Sensor signal
AND M0 // Rising edge detection
OUT M1 // Trigger counting
LD M1
DINC D0 // Use DINC instruction to increase double word register value (D0 and D1 as a 32-bit register)
RST M1
Data Application Expansion
In addition to basic counting, data registers can also implement more advanced functions:
- Production Efficiency Calculation:
// Calculate output per hour
DIV D0 D10 D11 // D11 = D0 (total count) ÷ D10 (working hours)
- Defect Rate Statistics:
// D0 good product count, D5 defective product count
ADD D0 D5 D8 // D8 total = D0 + D5
DIV D5 D8 D9 // D9 defect rate = D5 ÷ D8 (result multiplied by 100 for percentage)
MUL D9 K100 D9
- Predictive Maintenance:
// Record equipment operating cycle count, trigger maintenance warning
LD>= D0 K10000 // Running over 10000 times
OUT Y0 // Trigger maintenance indicator light
Practical Tip: To facilitate program management, it is recommended to use symbolic names instead of register addresses, such as naming D0 as “ProductCount.” This is especially important in large projects.
Practical Summary
Data registers are one of the most flexible resources in PLCs, mastering them makes your automation programs more powerful. In practical applications, I have found that many experienced electricians tend to use only basic instructions and relay logic, overlooking the powerful capabilities of data processing.
The combination of data registers and counters can meet almost all industrial counting needs and can even extend to simple data analysis applications.
Practical Exercise Suggestions
Try building a simple simulated production line, using buttons to simulate sensor signals, and LED indicators to display counting status.
Start with basic counting, gradually adding batch management, shift statistics, and other functions. Focus on training the calculation and conditional judgment of register data, which is key to mastering advanced PLC applications.
Remember to draw a logic flowchart before programming; this will make your program more organized and easier to troubleshoot. Finally, don’t be afraid to experiment! Try different combinations of instructions, and you will find that PLCs are more powerful than you think.