Help with a pc fan project.

Hi.

I have a Intel 4 pin fan ( FHP 6074; Black - GND; Yellow - 12V; Green - Tach; Blue - PWM) and I want to make the following project:

In Serial Monitor I want to give the fan the command to spin with X RPM (Ex: 1500 RPM). Next, on an LCD
I want to read the fan speed to see if it's the same with the one desired.

I tried a lot of codes but none worked as I wanted.

Someone can help me please?

post your most promising code..

How do you measure the spin speed?

I tried to combine these codes, but no succes. And also i tried to convert PWM to RPM .

speed.txt (1.35 KB)

control.txt (993 Bytes)

can't read that on my smartphone.. see how to post code (using code tags) in the "how to use the forum" section

This in the control file

word VentPin = 3;

void setup() {
  pinMode(VentPin, OUTPUT);
  pwm25kHzBegin();
}

void loop() {
  pwmDuty(19); // 25% (range = 0-79 = 1.25-100%)
  delay(5000);
  pwmDuty(39); // 50% (range = 0-79 = 1.25-100%)
  delay (5000);
  pwmDuty(59); // 75% (range = 0-79 = 1.25-100%)
  delay (5000);
}

void pwm25kHzBegin() {
  TCCR2A = 0;                               // TC2 Control Register A
  TCCR2B = 0;                               // TC2 Control Register B
  TIMSK2 = 0;                               // TC2 Interrupt Mask Register
  TIFR2 = 0;                                // TC2 Interrupt Flag Register
  TCCR2A |= (1 << COM2B1) | (1 << WGM21) | (1 << WGM20);  // OC2B cleared/set on match when up/down counting, fast PWM
  TCCR2B |= (1 << WGM22) | (1 << CS21);     // prescaler 8
  OCR2A = 79;                               // TOP overflow value (Hz)
  OCR2B = 0;
}

void pwmDuty(byte ocrb) {
  OCR2B = ocrb;                             // PWM Width (duty)
}

This is the speed file

//project done by www.theorycircuit.com
//code by Crenn from http://thebestcasescenario.com  thank you!
//Varibles used for calculations
int NbTopsFan; int Calc;
 
//The pin location of the sensor
int hallsensor = 2; typedef struct{
 
//Defines the structure for multiple fans and 
//their dividers 
char fantype;
unsigned int fandiv; }fanspec;
 
//Definitions of the fans
//This is the varible used to select the fan and it's divider,
//set 1 for unipole hall effect sensor
//and 2 for bipole hall effect sensor
fanspec fanspace[3]={{0,1},{1,2},{2,8}}; char fan = 1;
 
void rpm ()
//This is the function that the interupt calls
{ NbTopsFan++; }
 
//This is the setup function where the serial port is initialised,
//and the interrupt is attached
void setup()
{ pinMode(hallsensor, INPUT);
Serial.begin(9600);
attachInterrupt(0, rpm, RISING); }
 
void loop ()
//Set NbTops to 0 ready for calculations
{ NbTopsFan = 0;
 
//Enables interrupts
sei();
 
//Wait 1 second
delay (1000);
 
//Disable interrupts
cli();
 
//Times NbTopsFan (which is apprioxiamately the fequency the fan 
//is spinning at) by 60 seconds before dividing by the fan's divider
Calc = ((NbTopsFan * 60)/fanspace[fan].fandiv);
 
//Prints the number calculated above
Serial.print (Calc, DEC);
 
//Prints " rpm" and a new line
Serial.print (" rpm\r\n");
}

do you have a Hall sensor on your fan?

do you get what this line does fanspec fanspace[3]={{0,1},{1,2},{2,8}};

I think a better approach would be to power up your motor and get variable speed working , write your own code to do that . Then get the lcd working , then try and measure the rpm using that third wire .
Finding a couple of bits of code , then sticking them together is not the way in my opinion .