Measuring Pulse Width

Hi,
I keep meaning to look into it myself and never get the time, but isn't the input capture feature of hardware timer1 exactly what you need to measure these small intervals ?

Duane B

rcarduino.blogspot.com

yes I need to measure that and it will in turn give me pulse width

Hi,
Just to be clear, I am referring to the input capture functionality of the timer, this is a timer configuration which will copy the exact timer count into a dedicated register when the pin state changes, you can then read this captured hardware timer value from your code. As this is using the hardware timer directly, its not reliant on or effected by any code that may be running.

Not sure if thats what you understood.

Duane B

rcarduino.blogspot.com

Thank you so much for helping me out here but actually your code is giving me similar results as I was getting before the problem is that I have to measure the pulse widths between 10 to 15 microseconds accurately but your code as well as my code gives 8 or 12 microsecond

The micros() was just a reference timing, find below a stripped pulsewidth meter for periods smaller than 24576 micros based on a tight software loop. One iteration takes 6/16 microsecond so we can 'measure" the following steps between 10 and 15 usec. So approx 15 steps.

26	9,750
27	10,125
28	10,500
29	10,875
30	11,250
31	11,625
32	12,000
33	12,375
34	12,750
35	13,125
36	13,500
37	13,875
38	14,250
39	14,625
40	15,000
41	15,375
//
//    FILE: PulseWidthMeter.pde
//  AUTHOR: Rob Tillaart
//    DATE: 2012-mar-20
//
//    LINK: http://arduino.cc/forum/index.php?action=post;topic=96971.0
//

unsigned int count = 0;
void setup()
{
  Serial.begin(9600);
  Serial.println("pulse width meter 0.2");

  pinMode(3, INPUT);  
}

void loop()
{
  count = 0;
  while ((PIND & B00001000) == B00000000); // wait for HIGH
  while ((PIND & B00001000) == B00001000) count++; // start counting until LOW

  float usec = 1.0 * count * 6/16;
 
  Serial.print("CNT: ");
  Serial.println(count, DEC);
  
  Serial.print(" equals ");
  Serial.print(usec, 2);
  Serial.println(" microseconds.");

  delay(1000);
}

With the hardware timer more accuracy should be possible.

Thanks Robtillaart

I just want to ask that the values which you showed are the ones which you got after applying the pulse to arduino and the changing its width from 10 15 microseconds? is it so? actually I can't check this at this moment because I don't have arduino at this time but I am realy anxious to know this

Thanks again

I just want to ask that the values which you showed are the ones which you got after applying the pulse to arduino and the changing its width from 10 15 microseconds? is it so? actually I can't check this at this moment because I don't have arduino at this time but I am realy anxious to know this

The table was made by a spreadsheet the first column is the value of the counter and the second one is the number of microseconds the pulse was. As the units are in 6/16 of a micros this is an indication for the precision achievable this way (approx 1/3 micros)

So you said you need to measure pulse of 10-15 micros. How precise do you want to measure? Is a whole value OK, [10,11,12,13,14,15] or do you need a decimal digit?

ahaa that's very nice and yes the whole value is absolutely fine, means that if the input pulse width is 10.35 or so then even the value 10 would be perfect.

@Robtillaart

one more thing I want to ask is can i get serial print messages directly in an excel file simultaneously while the time of printing?

one more thing I want to ask is can i get serial print messages directly in an excel file simultaneously while the time of printing?

Yes, google for gobetwino

@Robtillaart

Thanks and today I have checked the code output by applying different pulses from 10 microseconds to 15 microseconds so for 10 microsecond input pulse the output comes to be 7.13 or 7.50 and for 12 microsecond input pulse the output comes to be either 9.00 or 8.13 and so on. I don't know whether we could improve it or not so what do you think?

Thanks again

what do you think?

The ratio seems to be correct!

7.5 : 9 == 10 : 12
7.13 : 8.13 ~~ 10 : 12

Which version of the code do you use? - the one with the unsigned int?
Can you post the actual output of the script?
How did you connect the signal? Have you connected the GND's?
Did you try a pull down/up resistor?
Do you apply one pulse or a constant stream?

There might be a problem with the synchronization at the start.

So there is a lot to think about,

I don't know whether we could improve

Hi,
Sorry for the persistence, but why doesn't anyone ever use timer1 input capture for this type of thing ?

I have not tried it yet myself and so if there is some fundamental reason why it wont work I would appreciate if someone could point me to it ?

Thanks

Duane B

rcarduino.blogspot.com

@Robtillaart yes I have used the code with unsigned int the output of the code is given below, I have applied constant pulse stream at 1Hz frequency and changed the input from 10 to 13 and then 15 microsecond. I have connected the ground as well but did not use pulldown/up resistor.

DuaneB:
Sorry for the persistence, but why doesn't anyone ever use timer1 input capture for this type of thing ?

My suggestion a few posts back was exactly that!

Sorry for the persistence, but why doesn't anyone ever use timer1 input capture for this type of thing ?

just to give some reasons:

  1. in software you can use (almost) any pin
  2. there are far more SW examples how to do the counting (good bad and ugly)
  3. not everyone understand in detail how to program the timers as counter ( few examples)
  4. not everyone oversees the consequences of using timers
  • timer0 - 8 bit, used for clock
  • timer1 - 16 bit, free (servo lib?)
  • timer2 - 8 bit, free?

maybe you can post a reusable example how to do use Timer1 as counter?

Best I found sofar is : - Arduino Playground - ReadReceiver -

Hi,

maybe you can post a reusable example how to do use Timer1 as counter?

Thats not out of the question, in the meantime I have been busy with having a look at this -

Duane B

rcarduino.blogspot.com

Well written DuaneB, bookmarked!

@Robtillaart and others

How can I improve the resolution? as I have to accurately measure the width and the counter should do more fast counting, can I use external clock of more than 16 MHz by doing prescaling? or if you can suggest some other method may be using a fast counter?

Thanks

switch to a faster processor like the MBED of NCP its around 100Mhz so it shoulc be able to do more counts per microsecond.

Or use Timers as proposed before in this thread, I can't help you on that as I have never really dived into that matter.

@Robtillaart

I have never used MBED of NCP so can it also provide the flexibility of using the serial monitor to see the results and also what about the syntax is it similar to arduino? since arduino is very user friendly

Thanks