PWMTryWMaster1.ino (1.6 KB)
Good day. I'm an intern who has been assigned with a project to design a test kit for a PWM fan using Master and Slave Arduinos. Now, I've written the code for master Arduino with some help from the Internet. When I'm testing the output from the PWMPin 9 using a oscilloscope, it is outputting PWM signals with the desired intervals. But, when I'm displaying the value in Serial Monitor its lagging by one dutycycle. I.e: Oscilloscope shows 40% on time, but serial monitor shows 20%. Pardon my messy coding, I'm still new with Arduino. Any help would be appreciated. Thank you.
const int PWMPin=9;
int count=0;
unsigned long startTime;
int RPM;
void setup (){
pinMode(PWMPin, OUTPUT);
analogWrite (PWMPin, 0);
Serial.begin(115200);
attachInterrupt (digitalPinToInterrupt (2), counter, RISING);
}
void loop (){
for (int pwm=0; pwm<=255; pwm+=51){
analogWrite(PWMPin, pwm);
delay(2000);
startTime= millis();
count=0;
while ((millis()-startTime)<1000){
}
RPM=(count*60)/2;
Serial.print("DutyCycle=");
Serial.print(map(pwm,0,255,0,100));
Serial.print("%\t");
Serial.print("RPM= ");
Serial.println(RPM);
}}
void counter(){
count++;
}
Hi, the Pin 2 is supposed to be connected to the Tachometer/Sense Wire of 4-wire fan to read the RPM.
I don't have the fan with me right now. Once I have it, I'll check and let you know.
As for, the while and delay(100), I tried removing them both, but it is still same.
Should I try after removing the 2 seconds delay along with one second delay?
But, then the reading in both oscilloscope and serial monitor will be too fast to check.
i. The delay is for the fan to move to desired speed before outputting the RPM reading back to pin 2. I'm still new to Arduino, so I might be doing this the wrong way.
iii. The scope is PC Oscilloscope branded Hantek.
iv. When the scope displays 20% duty cycle , the serial monitor should do the same. Here its lagging, instead of 20% its displaying 0%, and when the reading is 40%, it displaying 20%
Yes that is correct datasheet. I drew a 10k pull-up resistor in schematic.
Just wanted to confirm, does that mean the sketch I uploaded is not causing the problem? If yes, then I will get back once the fan is handed out to me and after I retested. To see whether it happens then. Thanks again for replying