• EUR€
  • £GBP
  • $USD

Arduino Project 13: DIY Fan

DFRobot Jun 05 2017 3269

Related Product: Beginner Kit for Arduino

In this Arduino project, we will use a relay and a motor within this Arduino starter kit to make a small fan. A relay is an electrically operated switch that allows you to turn on or off a circuit using voltage and/or current much higher than the Arduino can handle.


  • DFRduino UNO R3 (similar as Arduino UNO R3)*1
  • Prototype Shield *1
  • Jumper Cables M/M *9
  • 5m RGB LED*1
  • Pushbutton*1
  • Resistor 220R*2
  • Relay *1
  • 130 Motor*1
  • Fan*1
  • Wiring

    Connect the button with a 220Ω pull-down resistor in order to hold the logic signal near zero volts when the button is disconnected. The relay has 6 pins. Pin 1 and 2 on the relay are input signals and are separately connected to digital pin 3 and GND on the Arduino. Pins 3, 4, 5, and 6 on the relay are the output signals but we will only use pin 4 and pin 6 at this time. A relay is similar to a push button, which has 2 connections as well.

    DIY Fan Circuit Diagram

    Fig. 13-1 DIY Fan Circuit Diagram


    Sample Code 13-1:

    //Project thirteen - the Arduino to control fan operation
    int buttonPin = 2;                  // int buttonPin = 2;
    int relayPin = 3;                   // int relayPin = 3;
    int relayState = HIGH;              // int relayState = HIGH;
    int buttonState;                    // record the current button state
    int lastButtonState = LOW;          // record the last button state
    long lastDebounceTime = 0;
    long debounceDelay = 50;            // eliminate debounce time
    void setup() {
    pinMode(buttonPin, INPUT);
    pinMode(relayPin, OUTPUT);
    digitalWrite(relayPin, relayState);  // configure the initial state of relay
    void loop() {
    int reading = digitalRead(buttonPin);             //read the value of button
    // once detects change of state, record time
    if (reading != lastButtonState) {
    lastDebounceTime = millis();
    // wait for 50ms to evaluate if it is the same state as last state
    // if different, change the button state
    // if the state of button is high(pressed), change the state of relay
    if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
    buttonState = reading;
    if (buttonState == HIGH) {
    relayState = !relayState;
    digitalWrite(relayPin, relayState);
    //change the last state of button
    lastButtonState = reading;

    After uploading the sketch, you can control the relay and LED with the button.


    The debounce of push button is the

    if (reading != lastButtonState) {
    lastDebounceTime = millis();
    if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {

    When signals are received by the Arduino, the program does not operate on them immediately - it tests if the signal is correct and waits for a certain time to confirm. If the signal is correct, the program will be operating accordingly.

    The reason the program tests for a correct signal is because there is a bouncing process for the button when pressed. It might generate the wrong signal, so we test it to solve the problem in hardware.



    A relay is an electrically operated switch that allows you to turn on or off a circuit using voltage and/or current much higher than the Arduino can normally handle. There is no connection between the low voltage circuit operated by Arduino and the high power circuit - the relay isolates the circuits from each other.

    Let's take a look at the inner structure of relay:

    inner structure of relay

    Relays have 6 pins. Pins 1 and 2 are connected to the digital pin and GND. We use these 2 pins to power the relay. There is a coil between Pin 1 and Pin 2. When the circuit is HIGH, current flows in the coil, generates a magnetic field, closes the switch contacts and connects the NO (Normally Open) to COM(common)pin. When the circuit is LOW, no current runs in the coil, therefore the NC (Normally Closed) connects to the common pin. We connect Pin 4 and Pin 6 to control the switching on and off of the relay and the the LED.

    The difference between the DC motor, Stepper Motors and Servos

    DC (Direct Current) motors are devices that change electrical energy to kinetic energy. When you supply power to a DC motor it will start spinning continuously until that power is removed.

    When you switch the polarities, it will spin in the opposite direction. A motor runs continuously at a high RPM (revolutions per minute). These revolutions of the motor shaft can not be controlled to a specific angle, but you can control the speed. Because the rotations are so fast, it is impractical to use it for vehicles.

    A stepper motor has a gearing set on the DC motor to step down the speed and increase torque. This makes it more practical to use for vehicle applications. Its speed can be controlled by PWM. A servo is also a motor. It controls the position of the motor by a feedback system, as we saw in the servo projects covered. Servos are practical to use for robotics arms.

    Related category: Arduino kits

    Last Arduino Tutorial 12: Interactive Adjustable RGB LED