Counting production pulses with hardware interrupts
A production pulse counter is the single most valuable signal you can take from an industrial machine: every pulse is one cycle, one revolution, one unit produced. This example wires a proximity sensor to input I0_0 of an ESP32 PLC and counts pulses in a hardware interrupt, so nothing is ever missed while the loop handles MQTT or SD logging. It comes from a real textile machine monitoring deployment on Terrot circular knitting machines.
Interrupts, not polling
Polling an input inside loop() misses pulses the moment your firmware gets busy writing to SD or reconnecting WiFi. A hardware interrupt on the falling edge fires regardless of what the loop is doing, incrementing a volatile counter in a few microseconds. The ISR does the absolute minimum — no Serial, no String allocation — which keeps it safe to call thousands of times per second.
Debouncing and atomic reads
Industrial sensors mounted near motors and variable-frequency drives pick up electrical noise, and mechanical contacts bounce. A minimum-time-between-pulses filter inside the ISR (30 ms in this example) rejects bounce and spikes without any external hardware. When the 20-second reporting cycle reads the counter, it wraps the read-and-reset operation in a noInterrupts()/interrupts() pair, so a pulse arriving in the middle of the read can never corrupt or lose a count.
Cycle count, shift total and RPM
Two counters run in parallel from the same interrupt: one resets on every MQTT snapshot to give per-interval production, the other accumulates the whole shift and only resets when a remote command says so. Extrapolating the cycle count to one minute gives a live RPM estimate — on a circular knitting machine that maps directly to fabric output, and a sudden drop to zero is your machine-stop alarm for free.
A snippet from the implementation
Straight from the example as deployed on the ESP32 PLC — copy it freely:
void setup() {
Serial.begin(115200);
pinMode(I0_0, INPUT);
attachInterrupt(I0_0, onPulse, FALLING); // falling edge of the sensor
checkWiFi();
mqtt.setServer(MQTT_HOST, MQTT_PORT);
}
The full example is a complete program — wiring header, setup and main loop — ready to adapt to your application.
Frequently asked questions
How fast can the ESP32 count pulses?
With a minimal ISR the ESP32 comfortably handles thousands of pulses per second. The 30 ms debounce in this example caps it around 33 pulses per second, which is plenty for machine-cycle counting; lower it for encoders.
Why use volatile variables for the counters?
The counters are modified inside the interrupt and read in the main loop. The volatile keyword stops the compiler from caching the value in a register, so the loop always sees the latest count.
What sensor should I connect to the input?
Any 24 V industrial proximity sensor (inductive or photoelectric) pointed at a moving part of the machine works. The PLC inputs accept industrial voltage levels directly, no signal conditioning needed.