Hi, I am new to programming. Can anyone tell me a simple code for finding out the number of pulses given out by an external PWM ?
PWM is fixed frequency, so the number of pulses in any given time period is always the same.
If you have an external device that is producing a PWM signal you can use an Arduino to measure the time between pulses.
You need an Interrupt Service Routine (ISR) (see attachInterrupt()) which is triggered by the RISING pulse. It can then record the value of micros() and by subtracting one reading from the next you can get the number of microseconds for a single PWM cycle. The code in the ISR can be as simple as
void pulseTimer() {
pulseMicros = micros();
newPulse = true;
}
and the code in loop() can be like this
void loop() {
if (newPulse == true) {
noInterrupts();
latestPulseMicros = pulseMicros;
newPulse = false;
interrupts();
pulseLength = latestPulseMicros - prevPulseMicros;
prevPulseMicros = latestPulseMicros;
}
}
define pulseMicros etc as unsigned long
...R
Thanks
Are you interested in the ratio of HIGH to LOW?

I just want a programming to find out the number of pulses given in one minute by DC motor speed control PWM rc controller
I just want a programming to find out the number of pulses given in one minute by DC motor speed control PWM rc controller
If you can arrange for a 0-5v input of your pwm pulses to pin 2 you can use this simple interrupt counting routine. Note that variables shared between the interrupt and the main program are declared as volatile, and interrupts are disabled/reenabled briefly to make an unchanging copy of the count. See Nick Gammons tutorial on interrupts Gammon Forum : Electronics : Microprocessors : Interrupts
volatile unsigned long count = 0;
unsigned long copyCount = 0;
unsigned long lastRead = 0;
unsigned long interval = 60000; //one minute
void setup()
{
Serial.begin(115200);
Serial.println("start...");
attachInterrupt(0, isrCount, RISING); //interrupt signal to pin 2
}
void loop()
{
if (millis() - lastRead >= interval) //read interrupt count periodically
{
lastRead += interval;
// disable interrupts,make copy of count,re-enable interrupts
noInterrupts();
copyCount = count;
count = 0;
interrupts();
Serial.println(copyCount);
}
}
void isrCount()
{
count++;
}
ok,i'll try it.Thanks