Controlling a DC motor means:
- Start and stop the motor
- Change the running direction of the motor as forward (CW) or reverse (CCW)
- Vary motor 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 push-buttons (or maybe a single push button) are used to start and stop the motor
- SPDT type switch is used to select motor direction as CW or CCW
- Potentiometer is used to vary the speed of motor
- In some controllers, two push-buttons are used to increase-decrease motor speed (instead of potentiometer)
So, all these DC motor controllers require push buttons, a potentiometer, an SPDT switch, and maybe other components to control the motor’s speed and direction.
But in this project, only one rotary encoder is used to control the DC motor fully.
Means we can
- Vary motor speed
- Alter motor direction and
- Start / stop motor
With only one rotary encoder. The rotary encoder replaces all the above components like buttons, pot, switch, etc.
The project uses a rotary encoder with an Arduino NANO development board to control the DC motor’s speed and direction. It also uses a motor driver module (L293). The complete project can be built with few components – modules. So let us see how it is done
Let us first collect the required things
List of required items
Arduino NANO development board
12 V DC Motor
Motor driver module
Rotary encoder module
Now, we shall build the circuit first, and then we shall see its working and operation. It is straightforward to build this circuit because we have to connect only 3 modules. One can easily build it using only jumper wires and a breadboard. Here is the snap of the circuit.
- Rotary encoder module has 5 interfacing pins. Out of these 5 pins – 2 pins are for Vcc and Gnd, and they are connected with 5 V output and Gnd of Arduino board.
- Its other 3 pins, SW, DI, and CLK, are connected with Arduino board digital pins D12, D2, and D3 respectively
- DC Motor driver module is used to drive motor and provide required voltage and current to it. It has 4 inputs to drive 2 DC motors, out of which 2 inputs IN1 and IN2, are used.
- The PWM outputs D5 and D6 of Arduino board are connected to these IN1 and IN2 that are used to drive motor
- A 12 V DC motor is connected to OUT1 and OUT2 of the motor driver module, as shown
- Arduino board and motor driver module both are given 12 V from the external power supply
Working of circuit
As I have said before, the most interesting thing about this circuit is, using this rotary encoder only, we can…
- Run or stop the motor
- Alter the direction of motor
- Vary the speed of motor
I will first explain how the circuit works, and then I will explain how it is done.
- Initially, when 12 V supply is applied to Arduino board and motor driver module both, the motor is stopped
- The motor starts running in either direction (clockwise or anticlockwise) when rotary encoder switch is pressed. When switch is pressed again – the motor stops. Thus by alternatively pressing switch – motor runs or stops
- While motor is running in clockwise direction, if we start rotating encoder in same direction (clockwise) – it will keep on increasing motor speed till it reaches to max.
- Else while motor is running clockwise, and if we start rotating encoder in reverse direction (anticlockwise) – it will start decreasing motor speed till it stops
- Now, as encoder is rotated anticlockwise further, the motor slowly start running in anticlockwise direction and keep on increasing speed to max in anticlockwise direction
- Thus, motor speed increases and decreases in either direction by rotating encoder clockwise and anticlockwise
- So one can easily set desire speed and direction of motor using only rotary encoder
To understand the working of the circuit in detail, first, we have to understand the workings of a rotary encoder.
Working of rotary encoder
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 in contact with the common ground pin C, in a particular order according to the direction in which you are turning the knob.
When they come in contact with the common ground, they produce signals. These signals are shifted 90° out of phase with each other as one pin comes in contact with the other pin. This is called quadrature encoding.
When you turn the knob clockwise, the A pin connects first, followed by the B pin. When you turn the knob counterclockwise, the B pin connects first, followed by the A pin.
By tracking when each pin connects to and disconnects from the ground, we can use these signal changes to determine how the knob is being rotated. You can do this by simply observing the state of B when A changes state.
When the A changes state:
- if B and A are not equal, then the knob was turned clockwise
- if B and A are equal, then the knob was turned counterclockwise
See the figure given below
So these A and B outputs are DI and CLK pins of the rotary encoder module. The Arduino checks pulse output from both and determines encoder is rotated clockwise or anti-clockwise.
If the motor is rotating clockwise at some speed and
- Case 1 – encoder is also rotated clockwise, then Arduino will increase PWM width on D5 – so the motor speed will increase. if encoder is rotated clockwise continuously – the motor reaches maximum speed
- Case 2 – if encoder is rotated anticlockwise, then Arduino will decrease PWM width on D5, so motor speed decreases. And rotating encoder anticlockwise continuously will make PWM width on D5 to 0 and stop the motor once. Continue to rotate encoder in the same direction, increasing PWM width on D6, and motor will start rotating anticlockwise. If anyone keeps on rotating encoder in anti-clockwise direction, the PWM width on D6 becomes max, and the motor reaches max speed in an anti-clockwise direction.
The circuit’s complete working and operation is based on the program downloaded into FLASH memory of Arduino microcontroller ATMega328. The program is written in C language using Arduino IDE. The program uses the DC_Motor library to vary DC Motor speed and direction. Please go through DC_Motor library documentation for more details. here is the program code