Introduction: Arduino Oscillating Fan
Learn how to use Simulink and Stateflow to build an Arduino based oscillating fan. The setup includes a potentiometer to control fan speed and pushbuttons to control whether the fan is stationary or oscillation mode. The control algorithms for the fan are mainly implemented in Stateflow.
Summary of Functionality:
If built correctly, this project should be able to:
- Turn on the fan when the appropriate pushbutton is pressed
- Oscillate only when the fan is turned on and the pushbutton for oscillation mode is pressed
- Alter the speed of the fan with the potentiometer
- Operate without being connected to a computer
Step 1: Hardware and Software Needed
In order to construct this project, you will need:
- Arduino Mega
- Pushbuttons (2)
- Servo Motor
- Coreless Motor
- 1kΩ Resistors (5)
- 10kΩ Potentiometer
- LEDs (2)
- IRF510 MOSFET
- USB A to B Cable
To run the code you will need:
- Simulink Support Package for Arduino
If you do not have MATLAB and Simulink you can get a free 30 day trial by clicking on the link below!
Step 2: Hardware
You can wire the hardware using the provided circuit schematic or you can follow the steps detailed below.
- Connect analog pin 8 to the output of the potentiometer. The two reference pins should be connected to 5V and the Arduino's ground.
- Connect digital pin 24 to the cathode of the red LED. A 1kΩ pull-down resistor should connect the anode to the ground.
- Connect digital pin 26 to the cathode of the green LED. A 1kΩ pull-down resistor should connect the anode to the ground.
- Connect digital pin 30 and a 1kΩ pull-down resistor to one pin of the first pushbutton and the other pin to 5V.
- Connect digital pin 32 and a 1kΩ pull-down resistor to one pin of the second pushbutton and the other pin to 5V.
- Connect PWM pin 9 to the signal pin of the servo motor, 5V to the power pin and ground to the ground pin.
- Connect PWM pin 8 and a 1kΩ pull-down resistor to the gate of the IRF510. The source of the IRF510 should be connected to ground.
- Connect the negative pin of the coreless motor to the drain of the IRF510 and connect the positive pin to 5V.
Note: Be sure to wire the hardware with the correct polarities otherwise the project may not work.
Step 3: Software
Here are some screenshots of the Simulink model for the fan setup.
The model can be broken down as follows:
- When you first open the file, you will see the Simulink model at the highest level. This contains the Stateflow chart as well as various Simulink blocks corresponding to the inputs and outputs of the system.
- By double clicking on the Stateflow chart, you can view the control algorithms within it. The layout of a Stateflow chart is very similar to the layout of a finite state machine diagram.
- There are two main modes within the chart: fan mode and oscillation mode
- In fan mode, the model checks to see if the user has pressed the button to turn on the fan or not. If they pressed the button, it will wait for it to debounce (the user lets go of the button) and then it will turn the fan on at the speed specified by the potentiometer. The fan will remain on until the button is pressed again.
- In oscillation mode, the model checks to see if the fan button has been pressed (or if the fan is currently on) and that the oscillation button has been pressed. Once the buttons debounce, the fan will begin to rotate in the counterclockwise direction if possible. Once it has reached its counterclockwise boundary value, it will begin to rotate in the clockwise direction until it reaches its clockwise boundary value. The fan oscillates between these two values until it is turned off or the oscillation button is pressed again.
** Note: The oscillation directions may be flipped depending on how you attach your coreless motor to the servo motor.
The Simulink files for this project can be downloaded from the following link:
Step 4: Testing
To test whether your system works, you should run the model in External mode. In external mode, Simulink will generate code that runs on your Arduino, but at at the same time, you will be able to monitor your system as it operates. Additionally, if you look inside the Stateflow chart, you can see the system transitioning throughout the states real-time. This mode makes debugging very easy because you can see everything that is going on and if something goes wrong, you will know exactly where it happens.
To run the model in external mode, follow these three easy steps:
- Select External mode from the drop down menu at the top of the model.
- Make sure the box next to it shows "inf". This stands for infinite and it ensures that the model will run indefinitely.
- Press the green play button next to the box.
Step 5: Completion
Once you know your model works in external mode, you can deploy it to your hardware! All you have to do is press the 'Deploy to Hardware' icon which is also located at the top of the model. When you deploy the model to your hardware, you will not need to be connected to a computer in order to run the fan.
In just five steps, you have created your very own miniature oscillating fan using Arduino and Simulink!