Displaying RPM in sync with Duty cycle in Serial Monitor

Hi, I'm an intern and have been asked to work on a project to test 4 Wire Fan using 2 Arduino Uno. 1st Arduino pin 7,8,9 go high and low in 1 0 0, then 0 1 0, and 0 0 1 form. The 2nd Arduino takes these input and then produces the PWM output based on the input, 0%, 50%, 100%. I'm close to the finishing stage. I am having problem with displaying RPM right next to the PWM related. The RPMs are correct but its being displayed in next stage. I.e: for 50% duty cycle, it displaying RPM=0, which is 0% dutycycle's reading, and the reading for 50% , is displayed in 100% dutycyle.I'm supposed to take average readings after 6 loops, and because of this, the Average values are going crazy a bit. I have attached the related documents for your reference. Thank you!

12V Fan Datasheet


I think that your flow is flawed.

You first calculate the RPM and next set the digital outputs to change the fan speed. Shouldn't you firsts et the fan speed and next measure the RPM?

Other comments:

Please learn to properly indent your code using tools -> autoformat.

This is exactly the same as delay(1000); why bother with millis() for that?

Are you sure that below is correct? You use attachInterrupt with digitalPinToInterrupt (2) but you don't use that here?

Variables that are used by both an ISR and the main code should be declared volatile. Because count is an integer, you should disable interrupts before reading it in loop() and enable interrupts after that. That way there is no chance that they change while using them in e.g. loop().

Hi again @sterretje .

When I add it at

it reads same but now 1 step in advance. 50% = RPM of 100%, 100%= RPM of 0%.

Then I tried adding it inside every loop, then there was no reading at all.

I removed that line and tried simulating again, the RPM was 0 , not read at all.

When I changed that to detachInterrupt (digitalPinToInterrupt (2)); the RPM value only reads for 50%, the rest show 0.

After resetting few times, I noticed that the Pin 8 goes HIGH after 9+ seconds everytime I reset the board (Because of the 9 sec delay in the loop, I guess). But at that 9 seconds, the RPM is still showing the value. So, in serial monitor. for Dutycycle 0%, RPM=0 gets printed, and again after 9 sec, Pin 8 goes HIGH, which correlates PWM=0, and RPM=0 gets displayed again, this time beside Dutcycle 50%. Could be that be the culprit, if it is, can you please direct me in right direction of tackling it, maybe by reading the RPM after the delay? Thank you

Read again what I said; it's exactly the same as delay(1000); removing it is indeed not the solution because in that case chances of the interrupt being triggered are about 0% and hence a count of 0.

I'm just pointing out what I see as incorrect. Run the following test sketch.

void setup()
 {
  Serial.begin(115200);
  Serial.println(digitalPinToInterrupt(2));
}

void loop()
{
}

What is the number that is printed? So detachInterrupt ((2)) has no effect on the interrupt that was set with attachInterrupt (digitalPinToInterrupt (2), counter, FALLING). Check the 328P datasheet to see which interrupt you actually detached.

Further, if you detach an interrupt, you will have to attach it again if you want to use it again.

What I would do (for a desired PWM)

  1. disable interrupts (not detach)
  2. set count to 0
  3. set desired PWM
  4. enable interrupts
  5. wait N seconds
  6. disable interrupts
  7. read counter
  8. enable interrupts

As you're an intern, I will not provide fixes to the code; I'll leave it up to you to understand and implement.

Hi,
Have you got the gnd of the fan supply connected to the Arduino supply?

Tom... :smiley: :+1: :coffee: :australia:

Hey @TomGeorge , Yes I've connected ground the grounds. My bad, I forgot to draw that in the circuit. Anyway, thanks though!

I'll try my best and let you know. Thanks for the guidance so far!

Hi, after few times of editing the code the way you suggested, I managed to sync both Dutycycle and RPM. I'm having trouble to display the average. I changed the code to display 0% Dutycycle's RPM reading for 25 times, then move to 50% Dutycyle's RPM 25 times, then finally the same for 100% Dutycycle and then finding the average. Having one more isssue with the Average values for every Dutycycle over 25 loops.

From the code, I only display RPM values after 5 loops in for every Duty cycle, because the 1st few readings are still varying (either increasing or decreasing) as it changes from one Duty cycle to another. When I calculate, the average values, includes all 25 RPM readings. Is it possible to make it to read from only 5th reading to 25th? Thank you.

Below image shows the serial monitor output.

Maybe; we might be able to help further if you provide your new codein a new reply.

Question
Why did you remove the code from your opening post?

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