PrinzEisenherz1
Member
Hello everybody!
I am new here in the forum and my English is unfortunately not the best, sorry!
I bought the teensy 4 and wanted to port an old project from an Arduino Mega.
Unfortunately I can't get the rotation speed of a 4-pin fan.
Here are my attempts:
Wiring:
I am not a programming professional.
I hope someone from you can help me?
greeting
Johnny
I am new here in the forum and my English is unfortunately not the best, sorry!
I bought the teensy 4 and wanted to port an old project from an Arduino Mega.
Unfortunately I can't get the rotation speed of a 4-pin fan.
Here are my attempts:
Code:
/*
Lueftersteuerung RadiationShield
- PWM gesteuert bei uber- bzw unterschreiten eingestellter Schwellwerte
*/
#include <Thermistor.h>
#include <NTC_Thermistor.h>
#include <SmoothThermistor.h>
/********************************************/
/* PIN-Section */
/* Hier Sensor-Pins definieren */
/********************************************/
#define PWM_FAN_PIN_BLUE 4
#define RPM_FAN_PIN_GREEN 7
#define UPDATE_ZYKLUS 1000
#define MAX_SPEED 255
#define MIN_SPEED 50
#define FAN_FLANKEN 4
// Thermistor NTC10k
#define PIN_INPUT 14
#define REFERENCE_RESISTANCE 10000
#define NOMINAL_RESISTANCE 10000
#define NOMINAL_TEMPERATURE 30
#define B_VALUE 3950
/**
Smoothing factor of a temperature value.
*/
#define SMOOTHING_FACTOR 5
Thermistor* thermistor = NULL;
float celsius;
/********************************************/
/* Variablen-Section */
/********************************************/
int speed;
int FanSpeed = 0;
float TempNTC;
int RevTemp = 25;
unsigned long letzte_ausgabe = 0;
volatile int counter_rpm;
/********************************************/
/* Setup-Section */
/********************************************/
void setup(){
Serial.begin(115200);
Thermistor* originThermistor = new NTC_Thermistor(
PIN_INPUT,
REFERENCE_RESISTANCE,
NOMINAL_RESISTANCE,
NOMINAL_TEMPERATURE,
B_VALUE
);
thermistor = new SmoothThermistor(originThermistor, SMOOTHING_FACTOR);
celsius = thermistor->readCelsius();
pinMode(PWM_FAN_PIN_BLUE, OUTPUT);
speed=MIN_SPEED; //Langsam beginnen
pinMode(RPM_FAN_PIN_GREEN, INPUT);
digitalWrite(RPM_FAN_PIN_GREEN, HIGH);
attachInterrupt(RPM_FAN_PIN_GREEN, rpm_fan, FALLING);
analogWrite(PWM_FAN_PIN_BLUE,speed); // Lüfter langsam starten
letzte_ausgabe = millis();
}
void loop(){
TempNTC = thermistor->readCelsius();
if (TempNTC > RevTemp) {
FanSpeed = MAX_SPEED; // PWM 255
}
if (TempNTC < RevTemp) {
FanSpeed = MIN_SPEED; // PWM 50
}
if (FanSpeed > 255) {
FanSpeed = 255;
}
analogWrite(PWM_FAN_PIN_BLUE, FanSpeed);
if ((millis() - letzte_ausgabe) >= 1000){
// Interrupt deaktivieren um das rechnen nicht zu unterbrechen.
detachInterrupt(RPM_FAN_PIN_GREEN);
// RPM errechnen und ausgeben:
FanSpeed = (counter_rpm * 60UL) / FAN_FLANKEN;
Serial.print("FanSpeed : ");
Serial.print(FanSpeed);
Serial.println(" U pro Min");
// Counter zuruecksetzen
counter_rpm = 0;
// Zeitpunkt setzen
letzte_ausgabe = millis();
attachInterrupt(RPM_FAN_PIN_GREEN, rpm_fan, FALLING);
}
}
void rpm_fan(){
counter_rpm++;
}
Wiring:
I am not a programming professional.
I hope someone from you can help me?
greeting
Johnny