In this tutorial I have shown how to control CPU Fan using PWM using
Arduino Nano board. We will be using Arduino IDE for programming.
Part List
Connection
Circuit Diagram
Working
You can watch following video to see how project works. Here I have
shown Set speed and Actual speed of DC Fan.
Code
#define MAX 2145
#define MIN 1110
#define MID 1500
#define PWM_PIN 3
#define SEN_PIN 2
#define PLUS_PIN 7
#define MINUS_PIN 8
#define OFFSET 879.80769230769230769230769230769
#define CONSTANT 4.9615384615384615384615384615385
int PWM = 0;
float Speed_Req = 0,RPM = 0 ;
volatile int tick = 0, Speed = 0;
void SEN_ISR()
{
tick++;
}
void pciSetup(byte pin)
{
*digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
}
ISR (PCINT2_vect) // handle pin change interrupt for D0 to D7 here
{
if (digitalRead(PLUS_PIN) == LOW )
{
if (Speed <= 99)Speed += 1;
//Speed += 1;
while (digitalRead(PLUS_PIN) == LOW);
}
}
ISR (PCINT0_vect) // handle pin change interrupt for D8 to D13 here
{
if (digitalRead(MINUS_PIN) == LOW )
{
if (Speed > 0)Speed -= 1;
//Speed -= 1;
while (digitalRead(MINUS_PIN) == LOW);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(PWM_PIN, OUTPUT);
pinMode(SEN_PIN, INPUT_PULLUP);
pinMode(PLUS_PIN, INPUT_PULLUP);
pinMode(MINUS_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(SEN_PIN), SEN_ISR, FALLING);
pciSetup(PLUS_PIN);
pciSetup(MINUS_PIN);
TCCR2B = TCCR2B & B11111000 | B00000111; // for PWM frequency of 30.64 Hz
}
void loop() {
// put your main code here, to run repeatedly:
//Speed_Req = ((((MAX - MIN) * Speed) / 100) + MIN );// - OFFSET ) / CONSTANT);
Speed_Req = (MAX - MIN);
Speed_Req = ((Speed_Req * Speed) / 100);
Speed_Req = Speed_Req + MIN ; //(((MAX - MIN)));// * Speed));// / 100) + MIN;
PWM = (Speed_Req - OFFSET) / CONSTANT;
analogWrite(PWM_PIN, PWM);
delay(1000);
RPM = (tick * 30);
Serial.print("SP REQ = ");
Serial.print(Speed_Req);
Serial.print('\t');
/*
Serial.print("PWM = ");
Serial.print(PWM);
Serial.print('\t');
*/
Serial.print("Speed % = ");
Serial.print(Speed);
Serial.print('\t');
Serial.print("RPM=");
Serial.println(RPM);
tick = 0;
RPM = 0;
}
Wrapping Up
I hope you found this helpful, please leave comment for any question,
query or suggestion.
You can Download Arduino Code From Here.
You may also check other Posts
Inverter 12V DC to 230V AC using Arduino Nano
How to Install Mplab Harmony Integrated Software Framework
Scan Network Using Python On Raspberry Pi / How to Detect Wi-Fi
devices on Raspberry Pi
How To Load Micropython Firmware On ESP8266 using Windows
Overview of MicroPython
Arduino Fan Speed Control Using Pulse Width Modulation (PWM)
#tags
ArduinoArduino IDE
FAN
CPU FAN
PWM
Speed Control
DC Fan
Nano
Arduino Mini
Arduino Nano


0 Comments