Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Question about short delays

    Scheduled Pinned Locked Moved Discussion
    sleeptime
    6 Posts 3 Posters 1.9k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • E Offline
      Eric24
      last edited by

      I assume the preferred (maybe the only practical) way to insert a short (i.e. less than 1 second) delay on PyCom's implementation of micropython, is to use time.sleep(), time.sleep_ms(), or time.sleep_us()? One example would be generating a 50ms pulse (i.e. toggle on then off) on a GPIO.

      I ask because there are other microphyton implementations that have sleep and delay functions on 'machine' or other implementation-specific libraries.

      What is the effective resolution of the sleep functions (i.e. the minimum accurate time that can be requested) and how accurate is it?

      Also, during the sleep period, are other threads allowed to run? If so, is there some "magic threshold" of sleep duration, below which they are not and above which they are?

      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @Eric24
        last edited by

        @Eric24 For most of the questions you must try yourself. Sleep affects the thread which calls it. For a regular square wave of 100 ms period time (50ms on/off) the best choice is to use PWM, because that is purely done by hardware. Second best choice is a timer interrupt, but that will have a substantial jitter of about +/- 300 µs for each transition. For all other software based activities you have no timing guarantee. Always assume that a few ms of jitter may occur.

        E 1 Reply Last reply Reply Quote 0
        • E Offline
          Eric24 @robert-hh
          last edited by

          @robert-hh I think you may have interpreted my question as being "how to achieve a string of pulses", but I'm actually only interested in a single pulse (i.e. one high-to-low to low-to-high transition of a particular pulse duration). So I don't think PWM is a solution for that. I realize there will be some amount of latency and tolerance for any software-based delay, and that's really what my question is about--what is the preferred way to cause an "accurate as possible" delay and what is that level of accuracy?

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @Eric24
            last edited by

            @Eric24 I understood your intention. So the best way I would choose is to start a pulse by pulling GPIO low, and then set off an one shot timer, which at the end triggers an interrupt, pulling GPIO high again. that would have a jitter of about 300 us, 1 ms worst case. You can also use sleep_ms(), but that may be affected ba other threads, unless you have only one.

            E 1 Reply Last reply Reply Quote 0
            • E Offline
              Eric24 @robert-hh
              last edited by

              @robert-hh Interesting idea; I'll give it a try, but I think I'll also compare it with a sleep_ms (if it's accurate enough, that would sure be simpler :)

              1 Reply Last reply Reply Quote 0
              • L Offline
                larabrian
                last edited by

                Sleep() function actually suspends the processing of the thread in which it is called by the operating system, allowing other threads and processes to execute while it sleeps. With multiple threads and processes, sleep() suspends your thread - it uses next to zero processing power.

                1 Reply Last reply Reply Quote 0

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • 1 / 1
                • First post
                  Last post

                Pycom on Twitter

                Pycom