DC Motor Speed Control Using Arduino & PWM
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.
Motor is connected to a transistor, and the base of transistor is connected to PWM pin of Arduino and motors speed is varied according to PWM signal coming from Arduino.
Arduino DC Motor Control – Working
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.
PWM Speed Control (Pulse Width Modulation)
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.
As shown in the above figure the ON time is â€œTonâ€ and the OFF time is â€œToffâ€. T is the sum of the â€œTonâ€ and â€œToffâ€ which is called the Time Period. In the concept of PWM â€œTâ€ is not varying and the â€œTonâ€ and the â€œToffâ€ can vary, in this way when â€œTonâ€ increase â€œToffâ€ will decrease and â€œToffâ€ increase when â€œTonâ€ decrease proportionally.
The duty cycle is the fraction of one Time period. Duty cycle is commonly expressed as a percentage or a ratio. A period is the time it takes for a signal to complete an on-and-off cycle. As a formula, a duty cycle may be expressed as:
DUTY CYCLE = (Ton Ã· T) x100 %
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.
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.
|DC Motor||Low Power||1|
|Power Adapter||12 Volts||1|
|USB Cable||For Arduino Nano||1|
Arduino DC Motor Control – Circuit
Circuit diagram is shown in the figure below. If you are making this circuit on the general purpose PCB (ZERO PCB) or breadboard, this figure is useful.
Moreover, if you are good in the PCB Etching, use the images provided below.
In the circuit an Arduino Nano is used, which is very small in size and Breadboard friendly.
A transistor’s (2n2222) BASE pin is connected to D9 pin of Arduino through a Resistance of 1k ohm, Resistance is used for current limitation. Motor is connected between collector pin of transistor and Vcc. A diode (1n4004) is connected parallel to the motor in reverse bias; it is used for blocking the reverse current. Emitter pin of the transistor is connected to the ground. This circuit is powered by a 12 volt adapter.
Arduino PWM Motor Control – Video
DC Motor Speed Control – Download Program
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.
- Connect the Arduino through USB and upload the code
- Open the serial monitor and set the baud rateÂ at 9600
- Now type any number from 0 to 9.
After typing any value from zero to 9, speed of the motor varies, but we cannot see varying speed in video properly, but you can see it live.