How to control DC motor using Arduino

with video illustration and program code

In this Arduino tutorial you will learn how to control DC motor using Arduino UNO.

Learning how to control DC motor using PWM from Arduino is useful for many projects. It is also important because it shows how pulse width modulation works.

Pulse Width Modulation(PWM) is a technique of encoding data into pulses width. That is the length of the pulses is itself information. While this can be used for communication purpose, it can also be used for emulating analog signal. That is although PWM pulses are really digital we can increase/decrease the pulse width and send out number of pulses so that it acts like analog signal.

Leaving what is PWM behind we now go to how we can control DC motor using PWM from Arduino.

Arduino has six PWM pins or ports which are pin 3, 5, 6, 9, 10, and 11. This special digital pins are indicated by tilde sign(~). You can use any of them for PWM purpose. Here we will use the pin 5.

Schematic for DC motor control using Arduino

The circuit setup for controlling a DC motor is shown below.

How to control DC motor using Arduino
Fig: Circuit Schematic for controlling DC motor using Arduino

The PWM pin 5 is connected to the base of the 2N3904 NPN transistor via a 1kOhm resistor. This transistor amplifies the PWM current amplitude from arduino because the arduino current output is not high enough to drive the DC motor. The motor is connected to the collector of the transistor. The emitter is grounded. The DC Motor is bypassed by 1uF electrolytic capacitor. This capacitor filters the spikes produced by the DC motors. Also a 1N4001 diode is connected across the DC motor as you can see. This diode is there to prevent back flow of voltage and currents spikes from DC motor to the voltage battery when the motor is suddenly or abruptly disconnected. As you can see a separate voltage supply has been used for the DC motor which is a 9V battery. This separate voltage supply is needed because the DC motor requires high current to drive its internal motor. It also saves the arduino from voltage spikes as aforementioned.

Program/Sketch for driving the DC motor from Arduino

Below is the program code for driving the DC motor from Arduino UNO.

In the code above, we first label the pin 5 as M(standing for Motor). In the setup function, we declare that this pin 5 or M is an output pin using the pinMode function. In the program loop, we send PWM pulses of increasing width value or duty cycle using the for loop. This increases the speed of the motor. The second for loop is used to decrease the speed of the motor.

Note that since Arduino microcontroller is 8 bit microcontroller, we can write 8 bit value to the digital PWM pin(s). This means we can write values from 0 to 255 in decimal and 00000000 to 11111111 in binary. This is what we have done in the for loop in the above program code.

Notice that analogWrite function is used to send out PWM signals in arduino. For more about analogWrite function and PWM see How to use arduino analogWrite function for PWM.

Video showing how to control DC motor using Arduino

The following video shows how to control DC motor using Arduino.

If you have any comments or question leave comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *