Best way to Send pulses to pin

Status
Not open for further replies.

carcaes1

Well-known member
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
 
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:

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?
 
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.
 
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".
 
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:
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.
 
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)
 
And if i use thousand of commands DelayMicroseconds in loops during hours of running cycle, the teensy works well?
 
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.
 
Status
Not open for further replies.
Back
Top