Arduino Project
Mini Fan 2.0
Course Introduction
This Arduino project controls a DC motor using a TA6586 motor driver and four push buttons. The system uses PWM control to adjust speed and responds immediately to button input.
Note
If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.
1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE
Wiring

Common Connections:
TA6586 - Motor Driver Chip
BI: Connect to 6 on the Arduino.
FI: Connect to 5 on the Arduino.
GND: Connect to breadboard’s negative power bus.
VCC: Connect to breadboard’s red power bus.
DC Motor
GND: Connect to TA6586 B0.
VCC: Connect to TA6586 F0.
Button1
Connect to breadboard’s negative power bus.
Connect to 9 on the Arduino.
Button2
Connect to breadboard’s negative power bus.
Connect to 10 on the Arduino.
Button3
Connect to breadboard’s negative power bus.
Connect to 11 on the Arduino.
Button4
Connect to breadboard’s negative power bus.
Connect to 12 on the Arduino.
Writing the Code
Note
You can copy this code into Arduino IDE.
Don’t forget to select the board(Arduino UNO R4 WIFI) and the correct port before clicking the Upload button.
#include
// ===== Motor Control =====
const int fiPin = 5;
const int biPin = 6;
const int blueBtn = 9;
const int yellowBtn = 10;
const int redBtn = 11;
// ===== Servo Control =====
Servo servo;
const int servoPin = 3;
const int greenBtn = 12;
// ===== Variables =====
int fanSpeed = 0; // 0=off, 128=half, 255=full
int angle = 90; // Start centered
int direction = 1; // 1=right, -1=left
bool servoActive = false; // Whether oscillation is active
bool lastBtnState = HIGH; // For toggle detection
// ===== Servo Limits =====
const int leftLimit = 45;
const int rightLimit = 135;
void setup() {
// Motor setup
pinMode(fiPin, OUTPUT);
pinMode(biPin, OUTPUT);
pinMode(blueBtn, INPUT_PULLUP);
pinMode(yellowBtn, INPUT_PULLUP);
pinMode(redBtn, INPUT_PULLUP);
// Servo setup
servo.attach(servoPin);
pinMode(greenBtn, INPUT_PULLUP);
servo.write(angle); // Start at center
}
void loop() {
// ===== Fan Speed Control =====
if (digitalRead(blueBtn) == LOW) {
fanSpeed = 255; // Full speed
} else if (digitalRead(yellowBtn) == LOW) {
fanSpeed = 128; // Half speed
} else if (digitalRead(redBtn) == LOW) {
fanSpeed = 0; // Stop
}
// Apply fan speed
if (fanSpeed > 0) {
analogWrite(biPin, fanSpeed);
analogWrite(fiPin, 0);
} else {
analogWrite(biPin, 0);
analogWrite(fiPin, 0);
}
// ===== Green Button Toggle =====
bool currentBtnState = digitalRead(greenBtn);
if (lastBtnState == HIGH && currentBtnState == LOW) {
servoActive = !servoActive; // Toggle oscillation
}
lastBtnState = currentBtnState;
// ===== Servo Logic =====
if (fanSpeed == 0) {
// Fan off → stop servo immediately (keep current angle)
servoActive = false;
delay(10);
return;
}
if (servoActive) {
// Oscillation active: swing between 45° and 135°
angle += direction;
if (angle >= rightLimit) direction = -1;
if (angle <= leftLimit) direction = 1;
servo.write(angle);
delay(30); // Movement speed (slower = smoother)
}
// When servoActive is false and fan running, servo holds its last angle
}
