Control Speed of DC Motor Through Arduino IDE Serial Monitor


In this project, we will be interfacing a DC motor to Arduino Nano so that its speed can be controlled by sending commands through a PC. The Arduino Nano is connected to the PC with the help of USB cable and commands are given through the serial monitor of the Arduino IDE.  We will be using the PWM (Pulse Width Modulation) feature to get variable voltage and to control motor speeds. The motor will be connected to a transistor whose base will be connected to PWM pin of Arduino.

Working of Arduino DC Motor Control:

We connect the PC to the Arduino with the help of USB cable and send commands using the serial monitor. The speed of the motor can be changed from 0-9 (0 being the minimum speed and 9 maximum speed). The Arduino will use the PWM feature to send pulsating waves in order to control the speed.

Pulse Width Modulation (PWM) Speed Control:

Our Arduino will give output in digital form i.e. ZERO and ONE. Where ZERO is LOW and ONE is HIGH. On the other hand, the PWM is useful for analog output. It will give signals between zero and high frequencies.

Output Signal from PWM:

We represent the ON time as ‘Ton’ and OFF time as ‘Toff’ whereas T (time period) is the sum of them both. In reality, time period T does not vary but ‘Ton’ and ‘Toff’ can increase or decrease proportionally. The fraction of one time period is the duty cycle. A period is usually the time taken for a complete cycle. The motors speed will vary according to the Duty Cycle. We can represent Duty Cycle as:

Duty Cycle = (Ton ÷ T) x100 %

Transistor (2N2222):

The transistor will be used for driving the motor. We will connect it in series with the motor and connect the base of the transistor with the PWM pin of Arduino through a resistance. The Arduino sends the PWM signal while the transistor acts as a switch. When the signal is HIGH it short-circuits the emitter and collector while it opens at the LOW state. This is a continuous process which helps you control the speed of your motor.


  • Arduino Nano-1
  • DC motor (low power)-1
  • Transistor (2N222)-1
  • Power Adapter (12 V)-1
  • Resistance (1K0
  • Diode (1N4004)-1
  • USB Cable -1

Arduino DC Motor Control – Circuit

While building the circuit board using the general purpose PCB or breadboard. We have used an Arduino Nano in our project. Connect the Base pin of the transistor to the D9 pin of Arduino through a resistance of 1K ohm. Connect the motor between the collector pin of transistor and Vcc. Connect a reverse biased diode in parallel to the motor in order to block the reverse current. The Emitter pin of the transistor is grounded. We will use a 12 V adapter to power this circuit.

DC Motor Speed Control – Source Code:

int out1 = 9;
int val;

void setup() {

void loop() {
  if (Serial.available()){
    val =;

  if (val == '0') { analogWrite(out1, 0)  ; Serial.println("Speed is = 0"); val = 10;}
  if (val == '1') {analogWrite(out1, 255); delay(10); analogWrite(out1, 175) ; Serial.println("Speed is = 1"); val = 10;}
  if (val == '2') {analogWrite(out1, 255); delay(10); analogWrite(out1, 185) ; Serial.println("Speed is = 2"); val = 10;}
  if (val == '3') {analogWrite(out1, 255); delay(10); analogWrite(out1, 195); Serial.println("Speed is = 3"); val = 10;}
  if (val == '4') {analogWrite(out1, 255); delay(10); analogWrite(out1, 205); Serial.println("Speed is = 4"); val = 10;}
  if (val == '5') {analogWrite(out1, 255); delay(10); analogWrite(out1, 215); Serial.println("Speed is = 5"); val = 10;}
  if (val == '6') {analogWrite(out1, 255); delay(10); analogWrite(out1, 225); Serial.println("Speed is = 6"); val = 10;}
  if (val == '7') {analogWrite(out1, 255); delay(10); analogWrite(out1, 235); Serial.println("Speed is = 7"); val = 10;}
  if (val == '8') {analogWrite(out1, 255); delay(10); analogWrite(out1, 245); Serial.println("Speed is = 8"); val = 10;}
  if (val == '9') {analogWrite(out1, 255); delay(10); analogWrite(out1, 255); Serial.println("Speed is = 9"); val = 10;}


The beginning of the code will show two integers “out1” and “val”. ‘Out1’ is equal to 9 and shows that the D9 pin of Arduino is output whereas ‘val’ shows the data coming from the serial monitor. The serial communication in the void setup() begins with the function “Serial.begin(9600)” where 9600 is the baud rate of the serial monitor and ‘out1’ is the output of the monitor. We use “serial.available” in the void loop with the ‘if’ condition (it becomes true when data is sent through the serial monitor). The data is saved using the “” function in “val” integer.

In the first “if” condition, ‘0’ is sent through the serial monitor and becomes true. The “analogWrite(out1 , 0)” function is used for running the motor at zero PWM value. The function “analogWrite (out1, 0)” uses “out1” which indicates the pin which we want to use while “0” is the PWM value at this pin. After this “Speed is = 0” is shown using the “Serial.println” function.  Then the integer “val” is increased to 10 which is the random value other than 0-9. In the next condition “val ==1” is used and the motor runs at 175 PWM value. Similarly if we use condition 9, motor runs at 255 PWM which is the maximum value.


Connect the Arduino Nano board with the help of the USB and upload the code. Next, open the serial monitor and set your baud rate at 9600. Now type any number ranging from 0 to 9. You will see that the speed varies according to the value entered.

*image and code Courtesy

Related posts

Arduino Opta PLC Pros & Cons

Breakthrough of Ardino-Pro Opta: Micro-PLC with industrial IoT proficiency

Top Arduino sensors – the ultimate list 2021