Arduino Forum

Topics => Science and Measurement => Topic started by: rahulkp1986 on Mar 12, 2019, 06:03 pm

Title: How can i measure 0.5 micro second pulse width with pulse in function
Post by: rahulkp1986 on Mar 12, 2019, 06:03 pm
Hello all
I need to measure a pulse width up to .5 micro second resolution im using the below code

Code: [Select]
int pin = 7;
unsigned long duration;

void setup() {
  Serial.begin(9600);
  pinMode(pin, INPUT);
}

void loop() {
  duration = pulseIn(pin, HIGH);
  Serial.println(duration);
}
See also


I tried using arduino uno, STM32F103C8T6, and Lolin NodeMCU ESP8266 CP2102  all are giving resolution of 1 micro second, and there is an error of +- 2 micro second

i have heard STM32F103C8T6, and Lolin NodeMCU ESP8266 CP2102 are faster than uno.. so it should give much higher accuracy right? but in my case all are same

can anyone help?
Title: Re: How can i measure 0.5 micro second pulse width with pulse in function
Post by: AWOL on Mar 12, 2019, 10:07 pm
Use the capture-compare functionality of the AVR.
Title: Re: How can i measure 0.5 micro second pulse width with pulse in function
Post by: Idahowalker on Mar 20, 2019, 06:05 am
Look to the ESP.getCycleCount() macro:

 https://www.instructables.com/id/625-Nanosecond-Resolution-Timer-for-Any-Microcontr/ (https://www.instructables.com/id/625-Nanosecond-Resolution-Timer-for-Any-Microcontr/)