Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 13 of 13

Thread: Best way to Send pulses to pin

  1. #1
    Member
    Join Date
    Mar 2014
    Location
    Valencia
    Posts
    58

    Best way to Send pulses to pin

    Hi everybody, i have 2 questions about sending pulses

    Question 1

    In my project, i want to send some pulses to one pin, and i'd like to know the best way to do it

    Example

    With standard frequency 490 hz ( Period of 2 mms )

    If i want to send 200 pulses ( half of period in high, half of period in low)

    Option 1


    Code:
    for (int i=0; i<200; i++) {
        digitalWrite(pulsePin, HIGH);
        delayMicroseconds(1000);  o delay(1)
        digitalWrite(pulsePin, LOW);
        delayMicroseconds(1000);   o delay(1)
    }
    Option 2



    Code:
        analogWrite(pulsePin, 127);
        delay(400);  (2 mms each period * 200)
        analogWrite(pulsePin, 0);

    2 question. If i use many times the command Delaymicroseconds to send pulses, maybe the board can have a problem in the future?

    Thanks in advance

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,259
    It depends...

  3. #3
    Member
    Join Date
    Mar 2014
    Location
    Valencia
    Posts
    58
    Quote Originally Posted by PaulStoffregen View Post
    It depends...
    More information? What kind of situations?

    Thanks in advance

  4. #4
    Senior Member
    Join Date
    Sep 2013
    Location
    Hamburg, Germany
    Posts
    894
    Quote Originally Posted by carcaes1 View Post
    More information? What kind of situations?

    Thanks in advance
    It's the other way around, we need more information from you. Let's get back to your question:

    Quote Originally Posted by carcaes1 View Post
    In my project, i want to send some pulses to one pin, and i'd like to know the best way to do it
    Are the pulses always 50% duty cycle and 490 Hz? What is your application doing, apart from generating those pulses? How exact do the pulses need to be?

  5. #5
    Member
    Join Date
    Mar 2014
    Location
    Valencia
    Posts
    58
    Quote Originally Posted by christoph View Post
    It's the other way around, we need more information from you. Let's get back to your question:



    Are the pulses always 50% duty cycle and 490 Hz? What is your application doing, apart from generating those pulses? How exact do the pulses need to be?
    Sorry, my english is not very good and i didn't know how explain it.

    Yes, duty cycle 50%. My application needs to send pulses to a driver motor in order to move industrial servos. I'd like to do it with more frequency (375000 hz for example), but in my last example i wrote 490 hz. And the application delay is about 15 or 20 mms. In Each running of teensy, we receive data from COM and convert them in number of pulses in order to send the pulses to a driver motor connected in a pwm pin. Our idea is control 3 servos with 3 driver motor (or 1, it depends). So we should send pulses to each driver motor.

    Yes, the pulses should be fairly accurate.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,259
    Quote Originally Posted by carcaes1 View Post
    My application needs to send pulses to a driver motor in order to move industrial servos.
    It still depends, but at least we know the pulses go to a motor driver device.

    But there are many different industrial motor drivers, and many different types of motors. Some types, like stepper motor drivers, require pulses for step and direction. Other types control speed, where you give it a PWM signal to indicate the speed, but not the position.

    These details matter. Some approaches are best for consistent pulse timing for speed. Other approaches are best for assuring the correct number of pulses are sent, for position of steppers.

    So, still with so little specific information about the purpose of the pulse and the specific motor controller that receives them, the best answer I can give it "it depends".

  7. #7
    Member
    Join Date
    Mar 2014
    Location
    Valencia
    Posts
    58
    Thanks again Paul

    I will give you the model of driver motor a.s.a.p.

    The speed of these servos is 10 cm in 0,25 seg aprox.

    We want to move a system of 2dof, with 2 servos and we send pulses to move it to a specific position. The range is 20.000 pulses, 20 cm of real travel. The neutral position is 10.000 pulses (10 cm) , so from 0 to 20.0000. And we send the variation en each moment, the number of pulses to move it to a specific position.
    If we are in position 10 cm and we want to move it to position 11 cm, we send 1000 pulses and direction UP.
    I we are in position 11 cm and we want to move it to position 9 cm, we send 2000 pulses and direction DOWN.
    Last edited by carcaes1; 09-15-2014 at 05:20 PM.

  8. #8
    Member
    Join Date
    Mar 2014
    Location
    Valencia
    Posts
    58
    Hi again , this is the manual of driver motor

    DCS810V1m

    https://dl.dropboxusercontent.com/u/.../DCS810V1m.pdf

  9. #9
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,259
    Oh, I see. It's a closed loop DC servo driver, but with a stepper pulse control input.

    You probably should use the looped code approach. That way is much easier to be certain you've sent the exact number of pulses. The downside, of course, is you can't (easily) do any other work like checking for new input or reading sensors while you're executing the loop code to send an exact number of pulses. But this approach is simpler to get an exact number of pulses.

  10. #10
    Member
    Join Date
    Mar 2014
    Location
    Valencia
    Posts
    58
    Thank you very much again Paul for your advices.

    I will check it as soon as possible (i haven't received yet the driver motor)

  11. #11
    Member
    Join Date
    Mar 2014
    Location
    Valencia
    Posts
    58
    And if i use thousand of commands DelayMicroseconds in loops during hours of running cycle, the teensy works well?

  12. #12
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,259
    Yes, it can run for long times.

    However, digitalWrite and other code adds some CPU overhead. The total elapsed time will be slightly longer than only the number of microseconds spent in delayMicroseconds, so do not assume you can simply add up the number of microseconds to obtain accurate time keeping over long periods.

  13. #13
    Member
    Join Date
    Mar 2014
    Location
    Valencia
    Posts
    58
    Thanks again Paul.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •