How to use Timers on the RPi PICO

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 พ.ค. 2024
  • Timers are very simple devices on the Raspberry Pi PICO but they can help you do so much more with your programs and they do it VERY efficiently. Covered in this video are: What they are, uses, types and more. Program examples for both types timers are provided and explained in great detail.
    VISIT OUR COMPANION WEBSITE -- makingstuffwithchrisdehut.com
    DOWNLOAD FILES HERE -- makingstuffwithchrisdehut.com...
    SOURCED ITEMS USED IN SOME OF THE VIDEOS
    10 pcs Seven Segment LED display amzn.to/37lv21R
    2 pcs Bar Graph LEDs amzn.to/3LIevno
    3 pack of breadboards amzn.to/3oAkK3H
    200 pc assortment of LEDs amzn.to/3JfLQVc
    450 pc assortment of LEDs amzn.to/3sr1vKL
    38 value / 1200 pcs resistor assortment amzn.to/3Llxq8k
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 56

  • @abdontroche
    @abdontroche 7 หลายเดือนก่อน +1

    Thank you for the explanation! It helps me a lot!

  • @norbertbans
    @norbertbans ปีที่แล้ว +2

    Well explained and for the like and subscription well deserved!!! Thanks for this simple but very good demo!!!

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  ปีที่แล้ว

      Thank you very much Norbertbans! I really appreciate the feedback and kind words - happy programming!

  • @skf957
    @skf957 ปีที่แล้ว +1

    Very nicely presented, thank you. Just checked out your channel after watching a couple of your Pico vids. I'm a big Pico fan from when it was launched here in the UK in early '21. I was going to subscribe on the basis of the Pico content alone, but was then amazed to see that you share another passion of mine - woodworking! I'd got to the stage of combining the electronics stuff with some of my woodworking projects when a change in my circumstances meant that all of my machines are now in storage. Hopefully not for too much longer. Meantime I'll binge on your vids. Good luck with the channel.

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  ปีที่แล้ว

      Thanks for the feedback! I certainly hope things improve for you so you can bring your shop back to life!!!!

  • @peterwest1158
    @peterwest1158 9 หลายเดือนก่อน

    Thank you for the info on timers and IRQ handling. Looking forward to your eventual updates.

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  9 หลายเดือนก่อน

      Glad you liked the videos. I have plenty more videos coming up.
      Cheers!
      Chris

  • @aligurbuzoglu5181
    @aligurbuzoglu5181 ปีที่แล้ว

    Thank you very much for your workshoop so it was very helpful

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  ปีที่แล้ว

      Thank you for watching and for the feedback! Be sure to let others know about this channel.

  • @andreasgerth3654
    @andreasgerth3654 11 หลายเดือนก่อน +1

    I think this basic explanation for beginners is very well done. Only now from the point of view of those who want to write more complex routines or are approaching the RAM limit, you shouldn't be so wasteful with it.
    You don't have to first import the "machine" library as a whole, and then import individual components like the "timer" again separately. This can be greatly reduced by summarizing all of them with
    "from machine import Pin, Timer",
    the same applies to "utime".
    I would also like to note that the Python directive states that no subfunction accesses global objects of the MAIN.
    So it looks like this, and would also simplify subsequent improvements / extensions.
    def blink(pin):
    [tab]pin.toggle()
    the timer itself comes with:
    Blue_Timer = Timer(period = 2_000, mode = Timer.PERIODIC, callback = lambda t: blink(blue))
    Instead of then, as shown later, because the function content is always the same, you can save yourself these 3 additional subprograms / functions by only assigning a different PIN for Timer-Init.
    A blink function is then sufficient for this to toggle several different LEDs on different GPIOs.

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  11 หลายเดือนก่อน +1

      Hi andreasgerth,
      Thanks for sharing that great information. I admit, my code if often not "pythonic" or even efficient as it is often developed with the sole purpose of explaining a specific topic.
      In an upcoming project series, it is looking like I will be pushing the limits on memory capacity for the PICO and will need to be VERY aware of memory waste on that project!
      If you don't mind, when I get into that project, can I reach out to you for advice and suggestions?
      Cheers!
      Chris

  • @jimcraig5727
    @jimcraig5727 ปีที่แล้ว +1

    Another well done video/tutorial Chris. The word "Source" in the functions parameter brackets baffled me for a bit until I figured out its just a word, holding a place for the period value in the Timer function. Correct me if I'm wrong. I need to get a better understanding of functions probably.

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  ปีที่แล้ว

      Hi Jim, That is the Source of where the interrupt came from, in this case a timer. I will need to do a video on explaining that because it is a bit tricky to understand depending on the type of interrupt.

  • @ramsesramirez1910
    @ramsesramirez1910 8 หลายเดือนก่อน

    Very good explanation!
    Thinking about corner cases where two or more timers are triggered at the same time. Is that even possible? Could there be a chance of a race condition or maybe one of the interrupts don't get called?
    Thanks!

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  8 หลายเดือนก่อน

      Hi @ramsesramirez1910
      Having been around microcontrollers for about 2 decades, the one thing I am certain of, strange things are possible. As for a race condition, I am not sure on that. I recall reading in the docs that this is somehow prevented but can't recall the details at this time. Unfortunately I don't have the time right now to dig into it deeper.
      Chris

  • @mikeg3660
    @mikeg3660 ปีที่แล้ว

    Good stuff … subscribed

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  ปีที่แล้ว

      Hi Mike,
      Welcome, I am glad you like what you see and have subscribed! Just a heads up, I was going over my "to do" list of videos I will be shooting and I have about 50 more planned around the PICO. I am confident that in a year from now, I will think of 50 more things to film about it too.
      Cheers!
      Chris

  • @robjr657
    @robjr657 ปีที่แล้ว

    Really enjoyed this video... I hope you continue doing more videos about the pico. I do have 1 question for you... If you import the machine library, why then do you have to import the timer from the machine library again? I quite new to this and don't understand why this is required... Thanks!

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  ปีที่แล้ว +1

      Hi OpenEye, I must have had a bad day when I did that example! from machine import Timer is appropriate and the other import is not needed - probably just habit.
      I would certainly love to keep doing more videos but I must admit, I am unable to gather a large enough audience. While I certainly don't do this for a living, it would be great if it would cover the costs of the materials used and right now these videos can't even pay for a Pico - LOL.

  • @Steven-jf4cs
    @Steven-jf4cs 5 หลายเดือนก่อน

    Great tutorial! Similar to the 'pumper timer' example, could I also call the Timer mode ONE_SHOT from inside a function to then execute a general cleanup/shutdown process, e.g., xyz.deinit(), pin.value(0), etc. I'm positive it could but wanted your input if this is an acceptable practice. Thanks again!

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  5 หลายเดือนก่อน

      Hi Steven-jf4cs,
      I am glad you enjoyed the video!
      Regarding your question, I don't have an opinion if it is acceptable or not. To start with, give it a go and if it works, that is pretty convincing to me that you can use it.
      Unfortunately, what is and is not acceptable can be debated by many from different points of view. The Python purists really get demanding that everyone must do things one very specific way even though dozens of other ways will yield the same result.
      For professional programmers, it is very important to follow consistent guidelines regarding practices and syntax etc so that all the team members can work on the same code.
      In the "wild west" of DIY, I follow much less strict rules, if it works great, if not, keep experimenting until it does.
      Cheers!
      Chris

  • @rodrigodemarchi1184
    @rodrigodemarchi1184 ปีที่แล้ว

    Nice video, thanks for it.

  • @helmutzollner5496
    @helmutzollner5496 ปีที่แล้ว

    Interesting!
    Would you implement a PWM with a timer? I guess you could always use a recurring to switch on the signal and a one-shot to switch it off.
    But would a millisecond timer be fine enough for that for let's say audio?

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  ปีที่แล้ว +2

      I don't think you would get the results you desire using a timer to generate the PWM signal. I would stick with the traditional means for generating PWM signals.
      Chris

  • @nomosapplication
    @nomosapplication 5 หลายเดือนก่อน

    thx for a great tutorial, now functioning right here on my r.pi. i am trying to import a timer that opens a number of pins, each for their own (x) sec, closes for each their (y) sec, for the entire given period - instead of blinking in even intervals - and then onwards to the next of in all 12 periods, before it loops to the first period - but apparently micro python does not yet incorporate this function. do i have to transfer to c+, or what is your take on this one?

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  5 หลายเดือนก่อน

      GLad you liked the video. I am sure that could be coded in python, but I am not aware of any such existing function to do that.
      Chris

  • @VoeViking
    @VoeViking ปีที่แล้ว

    Coming from Arduino ide to micro python, thanks for your help. Will check your other videos.

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  ปีที่แล้ว

      Hi VoeViking! Thanks for stopping by and glad you found some value in the form of help.

  • @garystout4300
    @garystout4300 ปีที่แล้ว

    Great video Chris! I have a question and maybe you can point me in the right direction. I have setup a pico with a LCD and RTC, which basically runs as a clock and date display. I would like to have an event or relay trigger at a preset start time and continue until a preset stop time. My gola is to have 2 relays turn on/off at preset times. The clock is running great but I am not sure how to proceed with allowing it to control some events. So to summarize, say I want relay #1 to turn on at 8am and then shutoff at 5pm.....any thoughts or libraries available to use to do something like this?
    Thanks,
    Gary

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  ปีที่แล้ว +1

      Hi Gary,
      I suspect what you want to do is pretty straight forward. Currently I am swamped with real-life work but hope to get back to video production later this fall and I think I should do a video on that as it would tie together a few things. Sorry I can't code it up quick for you, I just don't have much "fun time" these days with being so busy at my regular job. Cheers!

    • @garystout4300
      @garystout4300 ปีที่แล้ว

      @@MakingStuffwithChrisDeHut thanks Chris! I think I figured it out. I found some code for an alarm clock that would sound a buzzer. Instead of the buzzer, I was able to create 2 separate timers to fire off at set times and 2 more timers to turn off at set times. The on/off times are set in the code but for an in-house project, it will do the trick. It really wasn't as hard as I thought it might be, but I am still learning micro python as well. Thanks again, Gary

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  ปีที่แล้ว +1

      @@garystout4300 AWESOME! Glad to hear you got it working - that is the fun of playing with Microcontrollers.

  • @TheUnofficialMaker
    @TheUnofficialMaker 24 วันที่ผ่านมา

    good stuff

  • @rafaelrobles2335
    @rafaelrobles2335 ปีที่แล้ว

    nice video!

  • @loadlinetubes
    @loadlinetubes 9 หลายเดือนก่อน

    Hi, Is there a way to improve timers accuracy, e.g. 0.1 ms?...
    Thank you.

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  9 หลายเดือนก่อน +1

      Hi loadlinetubes ,
      I was recently told that MicroPython can now use the hardware timers on the PICO instead of software timers. However, I just reviewed the MicroPython documentation that still states the hardware timers are not implemented. I suspect that the accuracy issues are related to a software implementation and until the hardware timers are implemented accuracy will suffer.
      Chris

    • @loadlinetubes
      @loadlinetubes 9 หลายเดือนก่อน

      @@MakingStuffwithChrisDeHut ho Chris, thank you very much for your kind and quick reply.
      Daniele Colombi

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  9 หลายเดือนก่อน

      Out of curiosity, what are you trying to do with the timers?

    • @loadlinetubes
      @loadlinetubes 9 หลายเดือนก่อน

      @@MakingStuffwithChrisDeHut this will be the very 1st time I'll use RB Pi Pico. I woukd like to read, calculate and show on 4 digits display the BPM (Beats per minute) from a 555 based metronome, this for my son Tommaso, 11 Years old, studying acoustic Guitar 😊
      I've already planner everything and I need to know some details to measure the time between two "ticks", used to calculate the BPM.

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  9 หลายเดือนก่อน +1

      @@loadlinetubes
      On the PICO, I use this bit of code to measure time of an event (or between events).
      CT_St = time.ticks_us() #TIME MEASURE
      the code you are trying to measure is here
      CT_Et = time.ticks_us()
      print(time.ticks_diff(CT_Et, CT_St))
      This is microsecond accurate and may help.
      Cheers!

  • @henkoegema6390
    @henkoegema6390 3 หลายเดือนก่อน

    👍

  • @gedtoon6451
    @gedtoon6451 9 หลายเดือนก่อน

    Good video but now a little out of date. Micropython has supported the Pi Pico hardware timers since version 1.8.5 and the current version is 1.20.0 You can still use the software timer you refer to in this video, but you define it as Timer(-1).

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  9 หลายเดือนก่อน

      Hi gedtoon6451,
      That is GREAT NEWS! I would certainly like to get a re-visit video done regarding this. By chance do you have a link to the docs on this?
      Thanks for the update and sharing it.
      Cheers!
      Chris

    • @gedtoon6451
      @gedtoon6451 8 หลายเดือนก่อน

      My bad! I must have been looking at documentation for a different microcontroller. When I checked the latest docs for the pi pico, it still says only software timers are available. Sorry for that.

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  8 หลายเดือนก่อน

      No Worries my friend. Thanks for sharing and updating the information.
      Cheers!
      Chris@@gedtoon6451

  • @apanoiu
    @apanoiu 2 หลายเดือนก่อน

    I suggest using micropython and asyncio. Rendering most timers useless

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  2 หลายเดือนก่อน

      Thanks for that information. Could you share a bit more detail about it, perhaps and idea of what asyncio does or how it works?
      Cheers!
      Chris

  • @gwynsea8162
    @gwynsea8162 ปีที่แล้ว +1

    Be interesting to know what's going on under the hood with timers. And what happens if you have multiple timers that go at different times - what if one is in progress when another is ready? The documentation is... err... incomplete (polite) crap (factual)

    • @MakingStuffwithChrisDeHut
      @MakingStuffwithChrisDeHut  ปีที่แล้ว

      Hmm, that is interesting and would be helpful for all to know. I believe we can somewhat answer that question with some sample programs and observing the behavior. Hopefully I can fit this into the next filming schedule. Thanks for the feedback!