Drive A DC Fan with Arduino and MATLAB Simulink

Drive A DC Fan with Arduino PWM Output From Simulink

In this article, I will try to explain how to program an Arduino with Simulink and perform an example application to demonstrate this process. You can directly skip 2nd part if you have an Arduino that’s ready to program with Simulink interface.

1. Required Equipment

-Arduino Uno

-A-Male to B-Male USB Cable

-L298 H Bridge

-12V DC Fan

-Male-Male and Female-Male Jumper Cables

-MATLAB Simulink (version R2017b or higher)

2. Connecting Arduino To Simulink

Connect Arduino Uno to your computer with a USB cable. Open MATLAB and click on Add-Ons from the MATLAB’s toolbar. From there download and install “Simulink Support Package for Arduino Hardware”. After the installation, download and install “MATLAB Support Package for Arduino Hardware”. Upon completion of the installation process, you will be asked if you want to configure your Arduino hardware. Click “Setup Now”. Now you will see a new pop-up window opened, named as “Hardware Setup”. Check whether if “Enable installation of Arduino USB Driver” is ticked, then proceed with clicking next. After that, you will be asked if you want to set up your Arduino board connection. Click yes, then proceed. Now you have to connect your Arduino Uno to your computer using a method shown in the setup. We will use a USB connection in this guide, so click the “USB” button and connect your Arduino to your computer, then proceed with the installation. In the next window, you should select your Arduino model, simply select “Uno” and select which port of your computer your Arduino is connected to (You can check this from device manager in windows 10). After that be sure to tick all the libraries (we won’t be needing all of them but if you want to do stuff which requires them in the future, you will want them to be on your computer) then press “Program”. Once the programming process is completed, proceed by clicking next.

Now simply press “Test Connection” to be sure there is no problem with your connection.
Once “To use your Arduino with the current settings, type a = Arduino” sentence appears on the screen you can proceed, and finish the setup.

3. Setup the Simulink Model

From MATLAB toolbar click “Simulink”, then click on the blank model. Now you will see a blank project window on the Simulink interface. From there click View->Library Browser from the toolbar of Simulink. Now you will see a library window, you can drag and drop the blocks on this window to your Simulink project. We will use the “Arduino Digital Output” block and “Pulse Generator” blocks, so simply drag and drop these blocks from the library window to your project and form a link between Pulse Generator and Arduino.

Should look like this once you are finished:

From there, click on the pulse generator to set the properties of your output. For example, I set these values like in the picture below, and with that settings, you obtain an output waveform like the image on the right of the settings.

4. Make the Physical Connections

Now to make our circuit work, we simply need a transistor. Since Arduino’s PWM signal generator can not give them enough power to our motor, we have to amplify the PWM signal that is generated by Arduino.

I used a pre-built L298 circuit for this job but you can simply do the same job with a single transistor. The main idea here is our PWM signal (graphed at page 3) is like a switch, when it’s 1, our transistor will start to work and our main power source (which is 5V power output on Arduino) will drive the motor until PWM gets to the 0 value, and making transistor off.
So, with this simple method we will obtain an output which looks like this:

As you see, the only difference is the amplitude.

Now since you know how things will work on our circuit you can make the connections like the picture below.

5. Run the Simulink Model on Hardware

Now we are in the final step, now we will build our Simulink model on our hardware. To do this we have to configure our Arduino hardware in Simulink. To do this, on Simulink toolbar press “Model Configuration Parameters” button (which looks like a gear). Then a new window will be opened. From there select “Hardware Implementation”, then select Arduino Uno as “Hardware board”. Then from the same window, click “Target hardware resources” below “Hardware board settings”. Then go into the “Host-board Connection” tab and change the “Set host COM port” option to “Manual”. Then type your Arduino’s COM number on the box below that setting. You can check which COM port is used by Arduino from the device manager as I mentioned earlier parts of this article. After you are done press apply, it should look like this in the end.

If everything is fine, then press ok. Now click the “Deploy to Hardware” button from the toolbar of the Simulink (blue box with electrical ports). Now you will observe the motor working.

Microsoft Student Partner