Hi,
I'm working on a project using a Teensy LC, where I want to control and monitor 4x 4-wire PWM fans.
Currently I have two fans connected, using a separate 12v supply (with common ground to the teensy). I am sending PWM signals from the Teensy which seems to work fine for controlling fan RPM. The tach signals from the fans are open drain and are connected directly to Teensy pins with internal pull up resistors enabled.
I am having trouble timing the fan tach pulse width interrupts detecting the edge of the pulse, the first interrupt seems to work and gives an acceptable output, but the second one does not seem to work.
Part of the problem is that I can't find any documentation on what interrupts are available on what pins, or how many interrupts are available to use. Also tbh, I don't fully understand how attachinterrupt(digitalPinToInterrupt(fan1_tach), -, -) resolves.
Any help would be greatly appreciated.
I'm working on a project using a Teensy LC, where I want to control and monitor 4x 4-wire PWM fans.
Currently I have two fans connected, using a separate 12v supply (with common ground to the teensy). I am sending PWM signals from the Teensy which seems to work fine for controlling fan RPM. The tach signals from the fans are open drain and are connected directly to Teensy pins with internal pull up resistors enabled.
I am having trouble timing the fan tach pulse width interrupts detecting the edge of the pulse, the first interrupt seems to work and gives an acceptable output, but the second one does not seem to work.
Part of the problem is that I can't find any documentation on what interrupts are available on what pins, or how many interrupts are available to use. Also tbh, I don't fully understand how attachinterrupt(digitalPinToInterrupt(fan1_tach), -, -) resolves.
Any help would be greatly appreciated.
Code:
const int fan1_tach = 10;
const int fan2_tach = 6;
const int fan1_enabled = 1;
const int fan2_enabled = 3;
const int fan1_pwm = 4;
const int fan2_pwm = 9;
const int led = 13;
volatile unsigned long tach1_pulseWidth[2];
volatile unsigned long tach2_pulseWidth[2];
void setup() {
pinMode(fan1_tach, INPUT_PULLUP);
pinMode(fan2_tach, INPUT_PULLUP);
pinMode(fan1_enabled, OUTPUT);
pinMode(fan2_enabled, OUTPUT);
pinMode(fan1_pwm, OUTPUT);
pinMode(fan2_pwm, OUTPUT);
pinMode(led, OUTPUT);
analogWriteFrequency(fan1_pwm, 25000);
analogWriteFrequency(fan2_pwm, 25000);
digitalWrite(fan1_enabled, LOW);
digitalWrite(fan2_enabled, LOW);
digitalWrite(led, HIGH);
analogWrite(fan1_pwm, 128);
analogWrite(fan2_pwm, 128);
attachInterrupt(digitalPinToInterrupt(fan1_tach), fan1_ISR, FALLING);
attachInterrupt(digitalPinToInterrupt(fan2_tach), fan2_ISR, FALLING);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
Serial.print(tach1_pulseWidth[0] - tach1_pulseWidth[1]);
Serial.print(", ");
Serial.println(tach2_pulseWidth[0] - tach2_pulseWidth[1]);
}
void fan1_ISR() {
tach1_pulseWidth[1] = tach1_pulseWidth[0];
tach1_pulseWidth[0] = micros();
}
void fan2_ISR() {
tach2_pulseWidth[1] = tach2_pulseWidth[0];
tach2_pulseWidth[0] = micros();
}