DC Motor Speed Control Using Arduino
DC Motor Speed Control Using Arduino
Speed control of DC motor with PC Interface is an easy DIY project. In this project
DC motor’s speed is controlled by sending the command through PC. Arduino is
directly connected to PC through the USB cable and command is given to
Arduino on serial monitor of the Arduino IDE.
Arduino is connected to PC through the USB cable. We can send the command to
PC on the serial monitor. We can change the speed of motor from 0 to 9. When 0
is sent over the Serial Monitor, the motor runs at minimum speed (that is zero).
When the speed is varied from 1 to 9, the speed increases, with the value 9 set as
the maximum speed of the motor.
A PWM DC motor controller technology is used to control the speed. In PWM,
the Arduino sends a pulsating wave that is similar to astable mode of 555 timer
IC.
Microcontroller and Arduino are digital devices; they cannot give the analog
output. Microcontroller gives Zero and ONE as output, where ZERO is logical
LOW and ONE is logical HIGH. In our case, we are using 5 volt version of the
Arduino. So it’s logical ZERO is zero voltage, and logical HIGH is 5 voltage.
Digital output is good for digital devices but sometimes we need the analog
output. In such a case the PWM is very useful. In the PWM, output signal switches
between zero and one, on high and fixed frequency, as shown in the figure
below.
Now the motor’s speed varies according to duty cycle. Suppose the duty is zero,
motor does not run and when duty cycle is 100 % the motor moves on maximum
RPM. But this concept is not always right because motor starts running after
giving some fixed voltage that is called threshold voltage.
Transistor (2N2222)
Microcontroller and the Arduino can process signals and consumes almost 20 to
40mA current but motors need high current and voltage, so we are using the
transistor for driving the motor. Transistor is connected in series with motor and
transistor’s base is connected to Arduino’s PWM pin through a resistance. PWM
signal is coming from Arduino and the transistor works as a switch and it short
circuit the Emitter (E) and Collector (C) when PWM signal is in High state and
normally opens when PWM signal is in LOW state. This process works
continuously and the motors runs at desired speed.
Components
Component
Specification Quantity
s
Arduino Nano 1
Transistor 2N222 1
Power 12 Volts 1
Adapter
Resistance 1K 1
Diode 1N4004
In the circuit an Arduino Nano is used, which is very small in size and Breadboard
friendly.
In the beginning of the code two integers are declared by name “out1” and “val”,
where out1 is equal to 9 which shown that pin D9 of Arduino is used as output
pin (or PWM pin). Moreover, data coming from the serial monitor saved in the
second integer “val”.
In the void setup() serial communication is begin by using function
“Serial.begin(9600)” where 9600 is the baud rate of serial monitor. After it “out1”
is declared as output because the motor is an output device.
In the void loop “serial.available” is used inside the “if” condition, it become true
when any data is sent over the serial monitor. This data is saved in “val” integer
using “Serial.read” function.
After it many “if” conditions are used, in the first “if condition”, when ‘0’ is sent
through the serial monitor, it become true. In the bracket “analogWrite(out1 , 0)”
is used for running the motor at the zero PWM value. In the function
“analogWrite (out1, 0)”, “out1” is used to indicate the pin which we want to use
and “0” is the PWM value at this pin. After it “Speed is = 0” is shown on serial
monitor using “Serial.println” function. After it the integer “val” is updated to 10,
where 10 is the random value, which is other than 0 to 9.
In the next line if condition is used for “val ==1”, at this time motor runs at PWM
value of 175. Same conditions are used upto 9, at the 9 motor runs at 255 PWM
value, 255 is the maximum PWM value.
Process
In this tutorial we are going to interface a DC motor to Arduino UNO and control it's speed
using PWM (Pulse Width Modulation) concept. This feature is enabled in UNO to get
variable voltage over constant voltage. The method of PWM is explained here; consider a
simple circuit as shown in figure.
If the button is pressed if the figure, then the motor will start rotating and it will be in motion
until the button is pressed. This pressing is continuous and is represented in the first wave
of figure. If, for a case, consider button is pressed for 8ms and opened for 2ms over a cycle
of 10ms, during this case the motor will not experience the complete 9V battery voltage as
the button is pressed only for 8ms, so the RMS terminal voltage across the motor will be
around 7V. Due to this reduced RMS voltage the motor will rotate but at a reduced speed.
Now the average turn on over a period of 10ms = Turn ON time/ (Turn ON time + Turn OFF
time), this is called duty cycle and is of 80% (8/ (8+2)).
If the button is pressed if the figure, then the motor will start rotating and it will be in motion
until the button is pressed. This pressing is continuous and is represented in the first wave
of figure. If, for a case, consider button is pressed for 8ms and opened for 2ms over a cycle
of 10ms, during this case the motor will not experience the complete 9V battery voltage as
the button is pressed only for 8ms, so the RMS terminal voltage across the motor will be
around 7V. Due to this reduced RMS voltage the motor will rotate but at a reduced speed.
Now the average turn on over a period of 10ms = Turn ON time/ (Turn ON time + Turn OFF
time), this is called duty cycle and is of 80% (8/ (8+2)).