Hello Everyone!
I have been facing a problem of measuring pulse width using arduino nano board. The pulses are very short 5 to 15 microseconds duration and they are coming at a frequency of 1 Hz. My code is listed below but all I am getting is that I can read the pulses after every 1 microsecond or more, means if I apply 2 microsecond width it can measure it and then if it is 2.2 or 2.3 it will still display 2 or even if it is 2.5 and so on until it reaches near to 3 microsecond then it would display either 2.5 or 3. So my results are not accurate and I need help with my code, I have just joined this forum after watching that someone previously answered this but the code was different so I hope that I would get some help from here. I would be grateful to all of you. here is my code
#include<stdlib.h>
unsigned int pulse_counts = 0;
unsigned int count = 0;
void setup()
{
Serial.begin(9600);
// Serial.println("pulse width measurment");
pinMode(3, INPUT);
}
void loop()
{
count = 0;
pulse_counts++;
while ((PIND & B00001000) == B00000000); // wait for HIGH
while ((PIND & B00001000) == B00001000) count++; // start counting until LOW
float usec = 1.0 * count * 6/12;
For serial output display - Uncomment this
Serial.print("Count = ");
Serial.print(pulse_counts);
Serial.print(" , Width = ");
Serial.print(usec, 2);
Serial.println (" microseconds");
// delay(1000);
}