Hey guys this is my first arduino project ever (basically idk anything and am learning from scratch). My laptop overheats a lot so I am making my own cooling pad. I made this circuit to control an Arctic P12 max fan and display the rpm. I made this circuit by referring to [Hardware example (Arduino)]. (Hardware example (Arduino))
However my rpm readings are off the charts (100000) and the pulses I get from the tach are in the range of (10000-100000) when it should be around 130 at max. Idk where I am going wrong. Please advice.
This is the code I am using rn just for diagnostics.
#include <U8g2lib.h>
#define TACH_PIN 2
#define PWM_PIN 9 // Controls fan speed
#define POT_PIN A0 // Potentiometer input
volatile unsigned long pulseCount = 0;
// Create the U8g2 object for SH1106 OLED (assuming you're using I2C and the U8G2_SH1106_128X64_NONAME_F_SW_I2C constructor)
U8G2_SH1106_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* SCL=*/ A5, /* SDA=*/ A4, /* RST=*/ U8X8_PIN_NONE);
void countTachPulse() {
pulseCount++;
}
void setup() {
Serial.begin(9600);
pinMode(TACH_PIN, INPUT_PULLUP);
pinMode(PWM_PIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(TACH_PIN), countTachPulse, FALLING);
u8g2.begin(); // Initialize OLED
}
void loop() {
// 1. Set fan speed from potentiometer
int potVal = analogRead(POT_PIN);
int pwmVal = map(potVal, 0, 1023, 0, 255);
analogWrite(PWM_PIN, pwmVal);
// 2. Read tach pulses
pulseCount = 0;
delay(1000);
unsigned long rpm = (pulseCount * 60UL) / 2; // 2 pulses/rev for most fans
// 3. Print results to Serial Monitor
Serial.print("Potentiometer: "); Serial.print(potVal);
Serial.print(" | PWM: "); Serial.print(pwmVal);
Serial.print(" | Pulses: "); Serial.print(pulseCount);
Serial.print(" | RPM: "); Serial.println(rpm);
// 4. Display results on OLED
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_6x10_tr); // Compact font for better fitting
u8g2.setCursor(0, 12);
u8g2.print("Pot: "); u8g2.print(potVal);
u8g2.setCursor(0, 24);
u8g2.print("PWM: "); u8g2.print(pwmVal);
u8g2.setCursor(0, 36);
u8g2.print("Pulses: "); u8g2.print(pulseCount);
u8g2.setCursor(0, 48);
u8g2.print("RPM: "); u8g2.print(rpm);
u8g2.sendBuffer();
// Send the buffer to the OLED for display
delay(100); // Short delay before the next loop
}
some sample outputs are:
Potentiometer: 181 | PWM: 45 | Pulses: 9478 | RPM: 284340
Potentiometer: 179 | PWM: 44 | Pulses: 9693 | RPM: 290790
Potentiometer: 178 | PWM: 44 | Pulses: 10057 | RPM: 301710
and when Pot is 0 and pwm is 0 the pulses are around 3000 and rpm around 80k
Please Advice!!


