Pages: 1 [2] 3 4   Go Down
Author Topic: Measuring Pulse Width  (Read 22323 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Robtillaart

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 for a 10 microsecond width input pulse and similarly it gives either 12 or 16 for pulse widths between 12 and 16 so that is the problem which I have been facing. The counter takes about 4 microsecond so is there any way to improve from this?

Thanks again
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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


Code:
//
//    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.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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?

Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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,




Quote
I don't know whether we could improve
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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



Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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.





* s.png (30.52 KB, 819x519 - viewed 52 times.)
Logged

Offline Offline
God Member
*****
Karma: 4
Posts: 813
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

   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!
Logged

Pages: 1 [2] 3 4   Go Up
Jump to: