Serial monitor output not in sync with Oscilloscope reading

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++;
}

Can you confirm the signal coming into pin 2 is a logic level signal and not open collector ?

Do not use delay( ) or while( ) in sketches unless you are absolutely sure the blocking code will not effect your sketch.

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.

Have not gone line by line thru your sketch.


This is a two second delay:

This is a one second delay:

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 have edited the code to much shorter version for your checking. The result is same.

So you are freezing all normal code execution for 3 seconds, for what reason ?


If you want to trigger the scope to a line of code, first toggle a free Arduino pin and use it/that as a trigger for the scope.

Is this a digital storage scope ?


What do you want to see on the scope and at the same exact time, what do you want to happen on the serial monitor .

Try:


void setup ()
{
  pinMode(PWMPin, OUTPUT);
  pinMode(2, INPUT_PULLUP);  //  <————<<<<
  analogWrite (PWMPin, 0);
  Serial.begin(115200);
  attachInterrupt (digitalPinToInterrupt (2), counter, RISING);
}

Edit

Is this a 5v or 12v fan ?

BTW
Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

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%


This a 24V fan. I have not connected the fan yet as my supervisor have not handed it to me. 24V Fan

If I have the correct data sheet your sensor output is open collector so it will need the pull-up to 5v.

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.