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) is connected to the Arduino pin 2 which is external interrupt pin, so rotating the rotary encoder makes its output (pin A) generates some pulses. The generated pulses interrupts the Arduino microcontroller and hence the microcontroller detects the direction of rotation (of the rotary encoder) by reading the rotary encoder pin B (DT). If pin B is logic 1, the direction of rotation was CW, the speed will be increased by incrementing a variable named motor_speed. If pin B is logic 0, the speed will be decreased by decrementing the variable motor_speed.

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.