Arduino MicroPython Control Fan

This tutorial guides you on how to control a 12V or 5V fan with an Arduino using MicroPython. We will cover:

Arduino MicroPython fan

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Relay
1×12V DC Cooling Fan
1×(Alternative) 5V DC Cooling Fan
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

Or you can buy the following sensor 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 comes with two pins.

  • Connect the black pin, labeled as negative (-), to the negative wire of the DC power supply.
  • Connect the red pinned labeled as positive (+), to the positive wire of the DC power supply.
Fan Pinout
image source: diyables.io

Make sure that the voltage of the DC power supply is the same as the voltage needed by the fan. In this guide, we use fans that require 12VDC and 5VDC.

How to Control a Fan

  • When a 12V/5V DC fan is powered by a 12V/5V supply, it runs at its maximum speed.
  • When a 12V/5V DC fan is controlled using a 12V/5V PWM signal, its speed can be adjusted.

In this guide, we will show you how to turn a fan on and off using an Arduino. The guide on controlling the fan speed will be covered separately. To control the fan, a relay is connected between the Arduino and the fan. The Arduino manages the relay, which in turn controls the fan indirectly. If you are new to using relays or programming with MicroPython for the Arduino, I suggest starting with the following tutorial:

Wiring Diagram

The wiring diagram between Arduino MicroPython Fan

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code

This code turns the fan ON for five seconds and then turns it OFF for another five seconds.

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-controls-fan """ from machine import Pin import time RELAY_PIN = 'D5' # The Arduino Giga WiFi pin D5 connected to the fan via relay pin # Initialize the relay pin as an output relay = Pin(RELAY_PIN, Pin.OUT) # Infinite loop while True: relay.value(1) # Turn on fan for 5 seconds time.sleep(5) relay.value(0) # Turn off fan for 5 seconds time.sleep(5)

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the Arduino board to the fan according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Check how the fan is working.

Code Explanation

The comments in the Arduino MicroPython code above explain the details.

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!