Arduino Mega - Control Fan

In this guide, we will show how to control a 12V or 5V fan with an Arduino Mega. We will explain it in more detail:

Arduino Mega fan

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Relay
1×12V DC Cooling Fan
1×Alternatively, 5V DC Cooling Fan
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of DC Fan

Pinout

A DC fan usually has two pins.

  • Negative (-) pin (black): connect to the black wire of the DC power supply.
  • Positive (+) pin (red): connect to the red wire of the DC power supply.
Fan Pinout
image source: diyables.io

Make sure the DC power supply has the same voltage the fan needs. In this guide, we use fans that need 12 V DC and 5 V DC.

How to Control Fan

  • A DC fan that gets power from a 12V or 5V power supply will run at full speed.
  • A DC fan that gets a 12V or 5V PWM control signal can have its speed adjusted.

This guide shows how to turn a fan on and off with an Arduino Mega. The guide for controlling fan speed is in another guide. To control the fan, we use a relay between the Arduino Mega and the fan. The Arduino Mega controls the fan through the relay. If you are not familiar with relays (what they are, how they work, and how to program them), you can learn about them in the Arduino Mega - Relay tutorial.

Wiring Diagram

The wiring diagram between Arduino Mega Fan

This image is created using Fritzing. Click to enlarge image

Arduino Mega Code

The following code keeps the fan on for five seconds and then off for five seconds.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-controls-fan */ #define RELAY_PIN 3 // The Arduino UNO R4 pin connected to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); // turn on fan 5 seconds delay(5000); digitalWrite(RELAY_PIN, LOW); // turn off fan 5 seconds delay(5000); }

Detailed Instructions

Do these steps one by one:

  • Connect the Arduino Mega to the fan as shown in the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the correct board (Arduino Mega) and the right COM port.
  • Copy and paste the given code into the Arduino IDE.
  • Click the Upload button to send the code to the Arduino Mega.
  • Watch the fan to see how it works.

Code Explanation

The explanation is in the comments of the Arduino code shown above.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!