# Precise timing problem

**URL:** <https://discourse.nodered.org/t/precise-timing-problem/59696>\
**Category:** Hardware\
**Created:** [11 March 2022 10:07 UTC](https://discourse.nodered.org/t/precise-timing-problem/59696 "2022-03-11T10:07:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jay1234](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jay1234](https://discourse.nodered.org/u/Jay1234)\
**Post date:** [11 March 2022 10:07 UTC](https://discourse.nodered.org/t/precise-timing-problem/59696/1 "2022-03-11T10:07:58Z")

</div>

Hello!

I am using NR to help control my astrophotography [barn door star tracker](https://content.instructables.com/ORIG/FC4/PKNE/HOHYJG7D/FC4PKNEHOHYJG7D.jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds&md=91e06c0cf430f091f656bc6cf1e03cdb). The set up currently consist of a RPi 3b running astroberry that's controlling a 28byj48 stepper motor.

I'm having issues with timing accuracy/latency. For example, If I set the Motor to run at a certain speed for 10 mins, it will actually end up finishing about 1 min later than it should have. The end result is images full of star trails...

So my question is: what is the best way to achieve precise timing that is relatively cheap and doesn't require anything more than basic skills?

So far I've considered

1. Using an analogue motor and PWM
2. Adding a micro controller to the set up specifically to drive the stepper motor

Are there any other options I should consider? If not, which of the above would result in better accuracy?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [11 March 2022 12:08 UTC](https://discourse.nodered.org/t/precise-timing-problem/59696/2 "2022-03-11T12:08:33Z")

</div>

10% timing inaccuracy seems a bit suspect

Can you post a flow what shows the problem

Or just run it for 9 mins ....... 🙂

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 March 2022 12:36 UTC](https://discourse.nodered.org/t/precise-timing-problem/59696/3 "2022-03-11T12:36:26Z")

</div>

As neither Linux nor Node.js (and hence Node-RED) are real-time services, some level of error is to be expected and is hard to quantify since it depends on so many other things.

My recommendation would be to use a dedicated microcontroller which IS typically a real-time system. Then you can drive that from a Pi with Node-RED.

---

<div class="post-metadata">

**Author:** ![Jay1234](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jay1234](https://discourse.nodered.org/u/Jay1234)\
**Post date:** [16 April 2022 04:39 UTC](https://discourse.nodered.org/t/precise-timing-problem/59696/4 "2022-04-16T04:39:53Z")

</div>

> [@TotallyInformation](#):
>
> As neither Linux nor Node.js (and hence Node-RED) are real-time services, some level of error is to be expected and is hard to quantify since it depends on so many other things.

@TotallyInformation Yes you're completely correct and this is something I wish I knew beforehand - but thats the fun of learning as you go. But this error is too significant for this project.

> [@cymplecy](#):
>
> 10% timing inaccuracy seems a bit suspect

@cymplecy Agreed. It seems to be pretty consistent at 10% too, but not consistently inconsistent enough to be reliable.

> [@cymplecy](#):
>
> Or just run it for 9 mins .......

Not really a viable solution. The flow basically turns human friendly input parameters (RPM, duration of run and direction) into arguments that are appended to the execution of a python script. As a result of running for slightly too long, the stepper motor also runs too slowly - and this is the biggest issue. If I can't get accurate speed out of the stepper motor then I'll end up with unwanted star trails.

Here is the flow. This is only responsible for starting the stepper motor - there is no ongoing activity while the motor is running. When the motor IS running, the CPU usage on the Pi remains low (less than 25%). I wasnt able to upload the code to this post as it exceeded the character count.

 ![2022-04-16 14_29_15-Node-RED _ 192.168.20.68](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fdc4a994dfbef63cb00dd9204e536b54ef5db79a.png)

> [@TotallyInformation](#):
>
> My recommendation would be to use a dedicated microcontroller which IS typically a real-time system. Then you can drive that from a Pi with Node-RED.

If I were to go down this route, which micro controller would you recommend?

---

<div class="post-metadata">

**Author:** ![davidz](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@davidz](https://discourse.nodered.org/u/davidz)\
**Post date:** [16 April 2022 05:31 UTC](https://discourse.nodered.org/t/precise-timing-problem/59696/5 "2022-04-16T05:31:32Z")

</div>

What is the level of timing accuracy you are looking for?

If you only pass parameters to the codes to run the motor, then a C program can provide micro seconds level accuracy for RPI 3b. An easy to use C library is [pigpio](https://abyz.me.uk/rpi/pigpio/cif.html#gpioPWM), which has hardware timed PWM functions for Pi GPIOs.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [16 April 2022 06:34 UTC](https://discourse.nodered.org/t/precise-timing-problem/59696/6 "2022-04-16T06:34:51Z")

</div>

To add to what @davidz says - the pigpio library is accessible from Python so no need to go down into engine room and use C 🙂

But I've not used Pipgpio to drive 28BY steppers so wouldn't know what facilities the library has to do this myself

But as davidz says, it uses hardware timing so it should be absolutely accurate on timing so worth investigating before switching over to a microcontroller

[edit] If you can't find some easy to use code from googling - I'd go and ask about this on the Pi forum

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [16 April 2022 10:00 UTC](https://discourse.nodered.org/t/precise-timing-problem/59696/7 "2022-04-16T10:00:49Z")

</div>

> [@Jay1234](#):
>
> If I were to go down this route, which micro controller would you recommend?

Well, the ESP devices have the advantage of WiFi built in and they are still very cheap. The ESP32 in particular has RTOS on board and has dual processor cores so is excellent for real-time processing. You can still connect them over a wire should you prefer that.

In addition, they have excellent firmware variations so you may not even need to go too far down the C++ rabbit hole. The ESPHome firmware for example supports many sensors out of the box but still lets you add custom code as you need to.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [16 April 2022 19:59 UTC](https://discourse.nodered.org/t/precise-timing-problem/59696/8 "2022-04-16T19:59:16Z")

</div>

I put a call out on twitter and got this link tweeted at me

> **[GitHub - stripcode/pigpio-stepper-motor: Control stepper motor over pigpio...](https://github.com/stripcode/pigpio-stepper-motor)**
>
> Control stepper motor over pigpio using python3 on Raspberry Pi - GitHub - stripcode/pigpio-stepper-motor: Control stepper motor over pigpio using python3 on Raspberry Pi

Try it out and let us know if it does the job 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [15 June 2022 19:59 UTC](https://discourse.nodered.org/t/precise-timing-problem/59696/9 "2022-06-15T19:59:35Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
