An encoder simulator: count a quadrature motor on an Arduino
Mokxi is a free online encoder simulator that runs in your browser, with nothing to install and no account needed. A gear motor with a two-channel Hall encoder turns a peristaltic pump on an Arduino Uno. The sketch counts the encoder’s pulses on an interrupt, reads the direction from the second channel, and stops the motor on a measured dose. Press the button and watch it count.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
What you can do here
In the circuit above, or in the editor it opens.
- Count encoder pulses on an interrupt and turn them into turns
- Tell the direction from which channel leads
- Read the motor’s speed from the pulse rate
- Stop a motor on a count, with a slow approach and an early brake
- Change the pulses per turn, the gearbox or the dose
- Close a speed or position loop on the count
What the running circuit shows
Pulses are turns, and turns are milliliters.
The encoder gives 11 pulses a turn of the motor on each channel, through a 30:1 gearbox: 330 pulses on channel A for each turn of the pump. The pump head moves 0.5 ml a turn, so a 5 ml dose is 10 turns, 3300 pulses. The serial monitor opens on “press for a dose of 5.00 ml”.
Press the button and the motor runs up to about 199 rpm at the output, about 1090 rising edges a second on channel A: in the engine, 109 in a tenth of a second. Each rising edge of A fires an interrupt, and the interrupt reads channel B. Low means forward, and every one of those 109 edges came with B low, because a forward motor’s A leads its B by a quarter of a pulse. Run it backward and B is already high when A rises, so the count goes down.
A motor does not stop the instant it is told, so the sketch slows to a crawl for the last turn and brakes 8 pulses early for the coast: at a crawl, shorted, with its gearbox dragging, the motor stops within a few degrees of the pump head. In the engine it prints “dosed 5.00 ml (3300 pulses)” a little over four seconds after the press, on the mark. Move the brake point in the sketch and watch the dose move with it.
How the encoder is modeled
The pulses follow the shaft.
Each edge happens when the modeled shaft reaches it, and the shaft chases the applied voltage with the motor’s own lag, so the sketch sees the motor run up, coast after a brake and slow when the battery sags. The two channels are exactly a quarter of a pulse apart and swap order with the direction. The motor is the same model as the plain DC motor, with the encoder on the motor side of the gearbox, as on a real JGA25-370 or N20.
What it leaves out: the load on the shaft, the flyback kick and stiction, which the plain motor leaves out too, and the Hall sensors’ own timing and uneven magnet poles, so the duty is exactly half and the phase exactly 90 degrees, cleaner than a real encoder on a scope. The pump’s volume per turn is a number in the sketch, not something the engine measures: calibrate a real one by pumping 20 turns into a measuring cylinder.
Questions
Is it free?
Yes. The circuit on this page, every part in it and the editor it opens in are on the free plan, with no card.
Do I need an account?
No. The circuit runs the moment the page does, and the editor opens without one. A free account is only for saving a project to the cloud and sharing it by link.
How do I read a quadrature encoder on an Arduino?
Put channel A on an interrupt pin with attachInterrupt on RISING, and in the interrupt read channel B: low is one direction and high the other, so add or subtract one. That counts each pulse once; counting every edge on both channels gives four times the resolution.
How many pulses per revolution will I get?
The encoder’s pulses per motor turn times the gearbox ratio: 11 × 30 = 330 per output turn here, counting the rising edges of A. Both are properties of the part, so set them to match your motor.
Can I simulate a rotary encoder knob too?
Yes. The KY-040 knob is in the parts bin, and the rotary encoder tutorial runs one on an Uno with a state table that copes with contact bounce.
More simulators
Each one opens on a circuit that is already running.
Count a motor
Open the circuit, press the button and watch the pulses add up to a dose. Nothing to install, and no account needed.