Controlling Fan Speed Using Arduino Through Microsoft Visual Studio

In my previous articles, I have explained about the concept of enabling the LED, creating home automation concepts, and so on. Now, in the following article, we are going to control the speed of the fan, using the PWM pins, using Arduino. Here, we are not going to use any external devices like relay or some other power reducing elements. We will just make use of the PWM pins of Arduino. These pins have the ability to take control over the power supply of the pins. Before we get into it, I need you to look at the following articles to get an idea about the IOT.


Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL Serial chip. External Interrupts: 2 and 3. These pins can be configured to trigger an interrupt on a low value, a rising or falling edge, or a change in the value. See the attached Interrupt() function for the details. PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.

  • SPI
    10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK). These pins support SPI communication, using SPI library.

  • LED
    13. There is a built-in LED driven by digital pin 13. When the pin is HIGH value, the LED is on, when the pin is LOW, it's off.

  • TWI
    A4 or SDA pin and A5 or SCL pin. Support TWI communication, using the wire library.

Items required

  • A DC motor
  • Arduino
  • Connecting wires

Programming Arduino

If you have come across the articles in the above links, you will be clear about the concept of uploading the program into Arduino. If not, please go through the articles first. If you know the concept of uploading the code, then upload the code given below.

  1. char incomingdata;  
  2. void setup() {  
  3.     pinMode(9, OUTPUT);  
  4.     Serial.begin(9600);  
  5. }  
  6. void loop() {  
  7.     incomingdata =; {  
  8.         if (incomingdata == 'a') {  
  9.             analogWrite(9, 255);  
  10.         } else if (incomingdata == 'b') {  
  11.             analogWrite(9, 200);  
  12.         } else if (incomingdata == 'c') {  
  13.             analogWrite(9, 155);  
  14.         } else if (incomingdata == 'd') {  
  15.             analogWrite(9, 100);  
  16.         } else if (incomingdata == 'e') {  
  17.             analogWrite(9, -255);  
  18.         }  
  19.     }  
  20. }  

Designing in Visual Studio

The next step is to design an Application in Visual Studio to send the commands to Arduino to execute the program. Open up your VS and create a new Windows Forms Application. Subsequently, add 4 buttons to the main page. Name them accordingly and then double click on them to add button click event handlers for them. Also, add a serial port tool from the tools box to communicate with Arduino. If you are very new to Arduino programming, using VS, you won’t understand all these. Thus, I suggest you read the articles from the links given above, if you are new. For each button, you will be using a separate acknowledgment, which will be sent to Arduino. Your design and code will be, as shown below.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. namespace Controlling_Fan_Speed {  
  11.     public partial class Form1: Form {  
  12.         public Form1() {  
  13.             InitializeComponent();  
  14.         }  
  15.         private void speed5_Click(object sender, EventArgs e) {  
  16.             serialPort1.Open();  
  17.             serialPort1.Write("a");  
  18.             serialPort1.Close();  
  19.         }  
  20.         private void speed4_Click(object sender, EventArgs e) {  
  21.             serialPort1.Open();  
  22.             serialPort1.Write("b");  
  23.             serialPort1.Close();  
  24.         }  
  25.         private void speed2_Click(object sender, EventArgs e) {  
  26.             serialPort1.Open();  
  27.             serialPort1.Write("c");  
  28.             serialPort1.Close();  
  29.         }  
  30.         private void speed1_Click(object sender, EventArgs e) {  
  31.             serialPort1.Open();  
  32.             serialPort1.Write("d");  
  33.             serialPort1.Close();  
  34.         }  
  35.         private void speed0_Click(object sender, EventArgs e) {  
  36.             serialPort1.Open();  
  37.             serialPort1.Write("e");  
  38.             serialPort1.Close();  
  39.         }  
  40.     }  
  41. }  
Pin Connection

Connect the +ve end of DC motor to the pin number 9 and the negative end to the ground. Have a look over the image to get an idea.


The code which we wrote will make use of the PWM pins of Arduino. These pins will control the power supply, which is based on the cycle speed. Once you create the Application in your VS and upload your program into Arduino, run your Application code. By clicking on each button, you can control the speed of your fan.

This is how you can control the speed of your DC motor without using any external device. Comment out, if you have any queries. Hope, this will help you to learn something new about Arduino. Thank you. In comig days, I will present more interesting articles.