Hi there,
So I'm working in a project where I need to vary the speed of a PWM fan using an Arduino. Later on, I'll want to measure the wind speed using hot-wire wind sensor.
I'm currently using a slightly modified code from this video: DIY Arduino PWM PC Fan Controller (Part 2) [Serial Control for Tuning] #0001 - YouTube
Here's the original code used in the video: arduino-pwm-fan-serial-control--for-tuning-/fan_serial_commander.ino at master · catalystoftechnology/arduino-pwm-fan-serial-control--for-tuning- · GitHub
All I changed from the original, was set the code such that I'm only controlling one fan instead of 5 different ones. I expected it to be a simply task but unfortunately I'm having a lot of trouble with this and I'm out of ideas.
Basically, when I upload the code into the board and open the serial monitor, the page starts getting rapidly filled with the message "Front:0". I'm also unable to change the fan speed at all, I'm able to input values for fan speed but nothing changes.
Here is the code that I'm using:
// variable declarations
int fanSpeed = 0;
String command;
char werd;
int currentFan = 0;
int front_a = 5;
int frontSpeed = 0;
int commandCheck;
String frontMarker = "-> ";
void setup() {
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
pinMode(4, INPUT);
// fans
pinMode(5, OUTPUT);//front 140mm
analogWrite(5,0);
Serial.println();
}
void loop(){
// this is what reads your serial commands
while ( Serial.available() > 0 ) {
// small delay because it can only deliver the bytes so quickly
delay(5);
// you have to read one byte (one character) at a time
werd = Serial.read();
// append the character to our "command" string
command += werd;
}
// clear serial so it's ready for your next command
Serial.flush();
if ( command.length() > 0 ) {
commandCheck = 0;
if ( command == "front" ) {
currentFan = 0;
frontMarker = "-> ";
} else {
// flag so we know it wasn't a change fan command
commandCheck = 1;
}
if ( commandCheck == 1 ) {
// the ".toInt()" appended to the command variable is to convert
// the user entered "command" from a string into an integer so
// that we can use it to set the pin's PWM pulse width
// (it'll error if you send an integer in string format)
if ( currentFan == 0 ) {
//front
analogWrite(front_a, command.toInt());
frontSpeed = command.toInt();
} else if ( currentFan == 1 ) {
} else {
//unknown command
}
}
command = "";
Serial.print(frontMarker);
Serial.print("Front: ");
Serial.println(frontSpeed);
// this function (below) is just to print a ton of empty lines so that the UI "appears" to be refreshing like a screen.
// you'll have to set your serial monitor to a certain width to get that effect to work correctly, of course
clear();
}
}
void clear() {
Serial.println();Serial.println();Serial.println();Serial.println();Serial.println();Serial.println();Serial.println();Serial.println();Serial.println();Serial.println();Serial.println();
Serial.println();Serial.println();Serial.println();Serial.println();Serial.println();Serial.println();Serial.println();Serial.println();
Serial.println();Serial.println();Serial.println();Serial.println();Serial.println();
}
My current setup has the blue wire from the fan connected to the 5th PWM pin in the Arduino. The GND and 12V wires are not connected to the Arduino directly, instead, I have a PSU hooked directly to the fan to power it up. Lastly, the green fan wire is not connected to the board. I know it's supposed to work as a tachometer, but I'm not sure where to plug it in based on the provided code.
Lastly, I'm using a 12V 120mm Noctua PWM fan and an Arduino UNO as my board.
Any help and suggestions are greatly appreciated!