Source: https://mokxi.com/encoder-simulator

Simulator

# 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.

Start building free or read a rotary encoder knob

- 455 parts on the bench
- 43 boards running now
- 1.00x real time, on every board

Hold the button

## 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”.

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.

A rotary encoder knob on an Arduino Interrupts against polling The DC motor simulator The motor driver simulator

## 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.

Online circuit simulator Breadboard simulator ESP32 simulator Op-amp simulator Logic gate simulator LED circuit simulator Servo simulator LCD simulator OLED simulator NeoPixel simulator 7 segment display simulator LED matrix simulator Stepper motor simulator Motor driver simulator Ultrasonic sensor simulator Rectifier simulator Digital circuit simulator Potentiometer simulator PWM simulator Relay simulator Shift register simulator DHT11 simulator RFID simulator Accelerometer simulator RGB LED simulator Buzzer simulator MOSFET simulator Circuit simulator for kids Flip-flop simulator Multiplexer simulator Full adder simulator I2C simulator UART simulator Thermistor simulator PIR sensor simulator Oscilloscope simulator Multimeter simulator DC motor simulator Traffic light simulator Diode simulator Capacitor simulator Series and parallel circuit simulator Wheatstone bridge simulator Voltage regulator simulator Soil moisture sensor simulator Zener diode simulator Solar panel simulator AC circuit simulator Thevenin simulator Decoder simulator PID simulator Robot arm simulator Load cell simulator IR sensor simulator LM35 simulator HC-05 Bluetooth simulator nRF24L01 simulator

## 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.

Start building free Open the editor
