Part

Gear motor with encoder

A gearbox motor with a two-channel Hall encoder on its back: count the edges on A and B for how far and how fast, and which way.

See every part
  • 6 pins
  • 7 settings
  • In 1 project
Drawn live by the editor's own code, at the size you see it.
At a glance

The Gear motor with encoder in brief

What it is, and every setting the properties panel gives it.

An encoder gear motor, the JGA25-370 or N20 kind from robot kits, is a small brushed motor and gearbox with a magnet disc and two Hall sensors on the back of the motor shaft. It has six wires: M1 and M2 go to a motor driver, never to a board pin, while VCC and GND power the encoder from 3.3 or 5 volts and A and B are its two outputs. Mokxi runs the motor on the same model as the plain DC motor, 200 rpm at the wheel on 6 volts with a 120 millisecond lag, and times each encoder edge to the modeled shaft. With 11 pulses per motor turn and a 30 to 1 gearbox, the wheel gives 330 pulses a turn on each channel, and A leads B going forward. On an Uno, put A on pin 2 or 3 and count it in an interrupt, since a loop() with a delay() misses most of the 1100 pulses a second. Forgetting the encoder’s supply is the other classic: the motor turns and the count stays at zero. Load on the shaft and stiction are not modeled.

Family
Outputs
Pins
6
voltage
6
rpm
200
resistance
6
inertia
120
back_emf
0.85
ppr
11
ratio
30
Pins

6 pins

Named as the editor names them, so a wire lands where you expect it.

Pin
Role
What it does
M1
Input or output
The motor. To a driver: a board pin cannot run it.
M2
Input or output
The motor. To a driver: a board pin cannot run it.
VCC
Input or output
The encoder's supply, 3.3 V to 5 V.
GND
Input or output
The encoder's ground.
A
Output
The two Hall outputs (C1 and C2 on some boards), driven from VCC.
B
Output
The two Hall outputs (C1 and C2 on some boards), driven from VCC.
How to use it

How to use an encoder gear motor with an Arduino

The dosing pump example drives the motor from one channel of an L9110S on pins 5 and 6, powered from four AA cells, and gives the encoder its own 5 volts from the Uno. A goes to pin 2 and B to pin 3. attachInterrupt(digitalPinToInterrupt(2), onA, RISING) catches every rising edge of A, and reading B inside the handler gives the direction: low is forward, high is backward.

Keep the count in a volatile long and copy it between noInterrupts() and interrupts(), since a four-byte value can change halfway through a read on an Uno. A motor does not stop the instant it is told, so to land on a count, slow down for the last turn and brake a few pulses early, as the pump sketch does at 8 pulses to go.

Part pin
Board pin
A
Pin 2
B
Pin 3
VCC
5 V
GND
GND
M1
L9110S OA1
M2
L9110S OA2

On an ESP32 any GPIO takes an interrupt; power the encoder from 3.3 volts, since A and B swing to whatever its VCC is.

Arduino Uno: dosing pumplive0.000 s 0.00x
Hold the button
This is the simulator itself, running here. Click anything to open it in the editor.
Counting the encoder with direction
volatile long pulses = 0;
void onA() {
  if (digitalRead(3) == LOW) pulses++;
  else pulses--;
}
void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  attachInterrupt(digitalPinToInterrupt(2), onA, RISING);
}
void loop() {}
How it is modeled

What is true about the Gear motor with encoder, here

In its help article’s own words: what the simulation gets right, and where it stops.

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 lag. So a sketch sees the motor run up, coast after a brake, and slow when a battery sags, and can close a speed or position loop on it. At the default 200 rpm that is 1100 pulses a second on each channel.

Quadrature. The channels are exactly a quarter of a pulse apart, and the order flips with the direction.

Not modeled

What the plain motor leaves out: the load on the shaft and the flyback kick. The Hall sensors' own timing and uneven magnet poles are not modeled, so the duty is exactly half and the phase exactly 90 degrees. The encoder is on the motor side of the gearbox, as on the real motors, so gearbox backlash never reaches it.

From Gear motor with encoder, in full.

Projects

See the Gear motor with encoder in a project

One built-in project uses it. It opens in the editor, already wired.

Boards

The boards it is shown on

This board has a built-in project with the Gear motor with encoder in it.

Wire up the Gear motor with encoder

Open the editor and push it into the breadboard. It is free, and it runs on your own machine.