RPM measurements on multiple 4 wire 12V fans
Posted: Fri May 01, 2020 6:50 pm
Hello,
Kind of a noob but have been playing with ESP32s for a bit.
Doing a battery box project and will have 3 to 4 4 wire 12V PC fans for cooling.
I am trying to get an RPM reading on each individual fan.
I found a sketch that was meant for an Arduino UNO and it did exactly what I wanted it to do so I tried it on an ESP32s and no go.
I found out why and after getting the attachInterrupt right and it is working just fine on the ESP32 but for only 1 fan. I have tried to figure out how to get 2 or 3 more fans to give me a reading but am stuck. Basically at this point I don't know where to begin.
If anyone can point me in the right direction it would be greatly appreciated.
Here is the sketch.
Wolf
Kind of a noob but have been playing with ESP32s for a bit.
Doing a battery box project and will have 3 to 4 4 wire 12V PC fans for cooling.
I am trying to get an RPM reading on each individual fan.
I found a sketch that was meant for an Arduino UNO and it did exactly what I wanted it to do so I tried it on an ESP32s and no go.
I found out why and after getting the attachInterrupt right and it is working just fine on the ESP32 but for only 1 fan. I have tried to figure out how to get 2 or 3 more fans to give me a reading but am stuck. Basically at this point I don't know where to begin.
If anyone can point me in the right direction it would be greatly appreciated.
Here is the sketch.
Wolf
Code: Select all
//code by Crenn from http://thebestcasescenario.com
//project by Charles Gantt from http://themakersworkbench.com
//To disable interrupts: cli(); disable global interrupts and
//to enable them: sei(); enable interrupts
//Variables used for calculations
int NbTopsFan;
int Calc;
//The pin location of the sensor
int hallsensor = 33; typedef struct
{
//Defines the structure for multiple fans and
//their dividers
char fantype;
unsigned int fandiv;
} fanspec;
//Definitions of the fans
//This is the variable 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
//Variables used for calculations
void setup()
{
Serial.begin(115200);
pinMode(hallsensor, INPUT);
attachInterrupt(digitalPinToInterrupt(hallsensor), rpm, FALLING);
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 approximately the frequency 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");
}