Hello,
I am using my Arduino board to make a PWM fan controller, my fan is a huge 140mm unit capable of pushing some serious air and luckily it has a PWM input.
I have tested a simple code to output a PWM signal with 50% duty cycle at 37khz (acceptable PWM frequency input is 25-50khz for the fan), this runs the fan at approx. 50% speed.
The next step is to write some simple code to allow me to run a Rotary 4 position switch OFF/1/2/3. My idea was to supply the input on the switch with 5v from the Arduino and simply look for a High/Low state on either of the input pins and then trigger some code depending on their state.
For some reason I cannot get this to work and the fan simply remains off, which it should do if no signal is detected on any input. I cannot measure 5v on the Arduino 5v output so I think this could be the error?
Please see my code below:
//Project -- 3 Speed PWM Fan Controller
int Spd1 = 8; // Switch Position 1
int Spd2 = 12; // Switch Position 2
int Spd3 = 13; // Switch Position 3
void setup()
{
DDRD |= 1 << PD3; // set pin 3 as an output (the same as pinMode(3, OUTPUT))
}
void loop()
{
{
if (Spd1 == 1)
{
// 37 kHz means 27 us per cycle, calculation below for 25% on time duty cycle.
PORTD |= 1 << PD3; // set pin 3 high (much faster than digitalWrite)
delayMicroseconds(7);
PORTD &= ~(1 << PD3); // set pin 3 low (much faster than digitalWrite)
delayMicroseconds(20);
}
}
{
if (Spd2 == 1)
{
// 37 kHz means 27 us per cycle, calculation below for 50% on time duty cycle.
PORTD |= 1 << PD3; // set pin 3 high (much faster than digitalWrite)
delayMicroseconds(13);
PORTD &= ~(1 << PD3); // set pin 3 low (much faster than digitalWrite)
delayMicroseconds(14);
}
}
{
if (Spd3 == 1)
{
// 37 kHz means 27 us per cycle, calculation below for 100% on time duty cycle.
PORTD |= 1 << PD3; // set pin 3 high (much faster than digitalWrite)
}
}
{
if (Spd1 == 0 && Spd2 == 0 && Spd3 == 0) {
PORTD &= ~(1 << PD3); // set pin 3 low (much faster than digitalWrite)
}
}
}