Controlling a DC motor means:
- Starting and stopping the engine
- Change the motor running direction to forward (CW) or reverse (CCW)
- Vary engine speed (RPM)
All different types of DC motor controllers control the speed and direction of the motor. They use different types of input devices like
- Two buttons (or perhaps a single button) are used to start and stop the engine
- SPDT type switch is used to select motor direction as CW or CCW
- The potentiometer is used to vary the speed of the motor
- On some controllers, two buttons are used to increase and decrease the motor speed (instead of the potentiometer)
Therefore, all of these DC motor controllers require buttons, a potentiometer, an SPDT switch, and perhaps other components to control the speed and direction of the motor.
But in this project, only one rotary encoder is used to fully control the DC motor.
It means we can
- Vary engine speed
- Change the direction of the motor and
- Start/stop engine
With just one rotary encoder. Rotary encoder replaces all the above components such as buttons, potentiometer, switch, etc.
The project uses a rotary encoder with an Arduino NANO development board to control the speed and direction of the DC motor. It also uses a motor driver module (L293). The complete project can be built with few components – modules. So let's see how it's done
Let's first collect the necessary things
List of mandatory items
Arduino NANO development board

12V DC motor

Motor driver module

Rotary Encoder Module

Now, we will first build the circuit and then see its working and operation. It is simple to build this circuit because we only need to connect 3 modules. You can easily build it using just jumper wires and a breadboard. Here is the circuit snap.
Circuit Diagram
As shown in the figure, the circuit is built using three modules – rotary encoder, Arduino NANO and L298 DC motor driver.
- The rotary encoder module has 5 interface pins. Out of these 5 pins – 2 pins are for Vcc and Gnd, and they are connected with 5V and Gnd output of Arduino board.
- Its other 3 pins, SW, DI and CLK, are connected to the Arduino board's digital pins D12, D2 and D3 respectively.
- The DC motor driver module is used to drive the motor and provide the required voltage and current. It has 4 inputs to drive 2 DC motors, of which 2 inputs IN1 and IN2 are used.
- The PWM outputs D5 and D6 of the Arduino board are connected to these IN1 and IN2 which are used to drive the motor
- A 12V DC motor is connected to OUT1 and OUT2 of the motor driver module as shown
- The Arduino board and motor driver module receive 12V from the external power supply
Circuit operation
As I said before, the most interesting thing about this circuit is that, using just this rotary encoder, we can…
- Start or stop the engine
- Change the direction of the motor
- Vary engine speed
I will first explain how the circuit works and then I will explain how it is made.
- Initially, when 12V power is applied to the Arduino board and motor driver module, the motor is stopped
- The motor starts running in either direction (clockwise or counterclockwise) when the rotary encoder switch is pressed. When the switch is pressed again – the engine stops. Thus, alternately pressing the switch – the engine starts or stops
- While the motor is running clockwise, if we start rotating the encoder in the same direction (clockwise) – it will continue to increase the motor speed until it reaches maximum.
- Otherwise, while the motor is running clockwise, and if we start rotating the encoder in the reverse direction (counterclockwise) – it will start to slow down the motor until it stops
- Now as the encoder is turned further counterclockwise, the motor starts running slowly in the counterclockwise direction and continues increasing the speed to the maximum in the counterclockwise direction
- Thus, the motor speed increases and decreases in either direction by rotating the encoder clockwise and counterclockwise
- Thus, one can easily set the desired speed and direction of the motor using just the rotary encoder
To understand the working of the circuit in detail, we first need to understand the working of a rotary encoder.
Rotary Encoder Working

The rotary encoder consists of a slotted disk connected to common ground pin C and two contact pins A and B as shown in the figure.
When you turn the knob, A and B come into contact with common ground pin C, in a specific order according to the direction you are turning the knob.
When they come into contact with common ground, they produce signals. These signals are shifted 90° out of phase with each other when one pin comes into contact with the other pin. This is called quadrature encoding .
When you turn the knob clockwise, pin A connects first, followed by pin B. When you turn the knob counterclockwise, pin B connects first, followed by pin A.
By tracking when each pin connects and disconnects from ground, we can use these signal changes to determine how the knob is being turned. You can do this by simply observing the state of B when A changes state.
When A changes state:
- if B and A are not equal then the knob has been turned clockwise
- if B and A are equal, then the knob has been turned counterclockwise
See the figure below

Therefore, these outputs A and B are DI and CLK pins of the rotary encoder module. The Arduino checks the pulse output of both and determines whether the encoder is rotated clockwise or counterclockwise.
If the motor is rotating clockwise at some speed and
- Case 1 – the encoder is also rotated clockwise, so the Arduino will increase the PWM width at D5 – then the motor speed will increase. if the encoder is continuously turned clockwise – the motor reaches maximum speed
- Case 2 – if the encoder is turned counterclockwise, the Arduino will decrease the PWM width at D5, decreasing the motor speed. And rotating the encoder counterclockwise continuously will make the PWM width at D5 0 and stop the motor once. Continue to rotate the encoder in the same direction, increasing the PWM width at D6, and the motor will begin to rotate counterclockwise. If one keeps rotating the encoder counterclockwise, the PWM width at D6 becomes maximum and the motor reaches maximum speed counterclockwise.
The complete functioning and operation of the circuit is based on the program downloaded into the FLASH memory of the Arduino ATMega328 microcontroller. The program is written in C language using Arduino IDE. The program uses the DC_Motor library to vary the speed and direction of the DC motor. See the DC_Motor library documentation for more details. here is the program code