I have a server of two Raspberry Pis and they needed to be cooled. I grabbed a fan. But it was way to loud! So I got an Arduino, wrote some Code and now I can control my fan.
Step 1: Figure out the pins of the fan connectorYou need a fan with a Plus (red) pin, a Minus (black) pin and a Speed pin. Some fans with three pins have plus, minus but then a Speedometer pin. The Speedometer pin is an output of the fan saying how fast the fan is rotating. Fans with four pins have a Speedometer pin and a Speed pin.
Step 2: Connect the Arduino to the fanConnect 12V to the Plus pin of the fan, GND to the Minus pin of the fan and a PWM pin, for example pin 3 of the Arduino to the Speed pin of the fan.
Step 3: Code#include <EEPROM.h>
#define FAN_PIN 3;
#define FS_ADDR 0x01
int fanSpeed;
void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT);
EEPROM.get(FS_ADDR, fanSpeed);
if(fanSpeed < 1) fanSpeed = 255;
analogWrite(FAN_PIN, fanSpeed);
Serial.begin(9600);
}
char rx_byte = 0;
String input = "";
void loop() {
if (Serial.available() > 0) { // is a character available?
rx_byte = Serial.read(); // get the character
// check if a number was received
if ((rx_byte >= '0') && (rx_byte <= '9')) {
input.concat(rx_byte);
}
else if (rx_byte == '\n') {
Serial.print("Received: ");
Serial.println(input);
if(input.toInt() < 256) {
fanSpeed = input.toInt();
EEPROM.put(FS_ADDR, fanSpeed);
} else {
Serial.println("Invalid Number");
}
input = "";
}
else {
Serial.println("Not a number.");
}
} // end: if (Serial.available() > 0)
analogWrite(FAN_PIN, fanSpeed);
}
Step 4: Control it!You can change the fan speed with putting in a number over serial. It saves the number also in the EEPROM (at FS_ADDR) so when it resets, the speed will be the same than before.
Comments
Please log in or sign up to comment.