DC Motor control with rotary encoder and Arduino

In this tutorial I’m going to control a DC motor speed and direction of rotation using Arduino uno board, rotary encoder and L293D motor driver chip.

I used the rotary encoder shown below:

rotary encoder

The rotary encoder has 5 pins: GND, + (+5V or 3.3V), SW (push button), DT (pin B) and CLK (pin A).
As an addition to the rotary encoder there is a push button and three pull up resistors for pins SW, DT and CLK of 10K ohm. With the three pull-up resistors, the normal state of each terminal is logic high.
The rotary encoder generates (when rotating) two square waves on pins A (CLK) and B (DT) with 90° out of phase as shown in the figure below:

rotary encoder output waveform

Since the normal state of pin A (CLK) and pin B (DT) are logic high we’ve to detect falling (transition from high to low) of one of them, here pin A is used to detect the movement of the rotary encoder in both directions (falling of pin A signal). Direction of rotation can be detected by knowing the status of pin B, if pin B is logic high this means the direction of rotation is clockwise (CW), and if pin B is logic low this means the direction of rotation is counter clockwise (CCW).

Related projects:
Arduino DC motor speed and direction control with L293D
Arduino based remote controlled DC motor
Arduino DC motor control with joystick and L293D

Hardware Required:

  • Arduino UNO board
  • Rotary encoder
  • DC motor
  • L293D motor driver
  • Breadboard
  • 12V source
  • Jumper wires

DC Motor control with rotary encoder and Arduino circuit:
Circuit schematic diagram is shown below.

Arduino rotary encoder motor control

(All grounded terminals are connected, don’t forget the 12V source negative terminal)

The L293D quadruple half-H drivers chip allows us to drive 2 motors in both directions. With two PWM outputs from the Arduino we can easily control the speed as well as the direction of rotation of one DC motor. (PWM: Pulse Width Modulation).

The L293D is supplied with 2 different sources, the first one (VCC1) is +5V which comes from the Arduino boards, and the 2nd one (VCC2) is +12V which is the same as motor nominal voltage. Pin IN1 and pin IN2 are the control pins where:

IN1 IN2 Function
L H Direction 1
H L Direction 2
L L Fast motor stop
H H Fast motor stop

Arduino pin 5 and pin 6 are PWM signal outputs, at any time there is only 1 active PWM, this allows us to control the direction as well as the speed by varying the duty cycle of the active PWM signal. The active PWM pin decides the motor direction of rotation (one at a time, the other output is logic 0).
The rotary encoder push button terminal is connected to the Arduino pin 4, with this button we can change the direction of rotation of the motor.

DC Motor control with rotary encoder and Arduino code:
The rotary encoder pin A (CLK) and pin B (DT) are connected to Arduino UNO pins 2 and 3 respectively. Both pins can be used to interrupt the Arduino microcontroller (ATmega328P) whenever there is a change in the state of at least one pin. The two interrupts are initialized with the following lines:

Interrupt 0 is for pin 2 and interrupt 1 is for pin 3.
Both interrupts call the function void encoder_read() which is used to read the state of the two rotary encoder pins pin 1 (CLK) and pin B (DT).

Full Arduino code:

Here is a simple video of the example:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.