How to use Timers on the RPi PICO

  Рет қаралды 16,365

Making Stuff with Chris DeHut

Making Stuff with Chris DeHut

Күн бұрын

Пікірлер: 58
@andreasgerth3654
@andreasgerth3654 Жыл бұрын
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 Жыл бұрын
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
@abdontroche
@abdontroche 11 ай бұрын
Thank you for the explanation! It helps me a lot!
@MakingStuffwithChrisDeHut
@MakingStuffwithChrisDeHut 11 ай бұрын
Glad it helped! Cheers! Chris
@skf957
@skf957 Жыл бұрын
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!!!!
@norbertbans
@norbertbans 2 жыл бұрын
Well explained and for the like and subscription well deserved!!! Thanks for this simple but very good demo!!!
@MakingStuffwithChrisDeHut
@MakingStuffwithChrisDeHut 2 жыл бұрын
Thank you very much Norbertbans! I really appreciate the feedback and kind words - happy programming!
@peterwest1158
@peterwest1158 Жыл бұрын
Thank you for the info on timers and IRQ handling. Looking forward to your eventual updates.
@MakingStuffwithChrisDeHut
@MakingStuffwithChrisDeHut Жыл бұрын
Glad you liked the videos. I have plenty more videos coming up. Cheers! Chris
@jimcraig5727
@jimcraig5727 2 жыл бұрын
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 2 жыл бұрын
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.
@aligurbuzoglu5181
@aligurbuzoglu5181 2 жыл бұрын
Thank you very much for your workshoop so it was very helpful
@MakingStuffwithChrisDeHut
@MakingStuffwithChrisDeHut 2 жыл бұрын
Thank you for watching and for the feedback! Be sure to let others know about this channel.
@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
@TheUnofficialMaker
@TheUnofficialMaker 4 ай бұрын
good stuff
@MakingStuffwithChrisDeHut
@MakingStuffwithChrisDeHut 4 ай бұрын
Thank you for the kind words! Cheers! Chris
@robjr657
@robjr657 2 жыл бұрын
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 2 жыл бұрын
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.
@VoeViking
@VoeViking 2 жыл бұрын
Coming from Arduino ide to micro python, thanks for your help. Will check your other videos.
@MakingStuffwithChrisDeHut
@MakingStuffwithChrisDeHut 2 жыл бұрын
Hi VoeViking! Thanks for stopping by and glad you found some value in the form of help.
@rodrigodemarchi1184
@rodrigodemarchi1184 2 жыл бұрын
Nice video, thanks for it.
@MakingStuffwithChrisDeHut
@MakingStuffwithChrisDeHut 2 жыл бұрын
Thanks for watching! Much appreciated.
@henkoegema6390
@henkoegema6390 7 ай бұрын
👍
@Steven-jf4cs
@Steven-jf4cs 9 ай бұрын
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 9 ай бұрын
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
@rafaelrobles2335
@rafaelrobles2335 Жыл бұрын
nice video!
@MakingStuffwithChrisDeHut
@MakingStuffwithChrisDeHut Жыл бұрын
Rafael, thank you very much for the feedback- much appreciated! Cheers!
@ramsesramirez1910
@ramsesramirez1910 Жыл бұрын
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 Жыл бұрын
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
@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 Жыл бұрын
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 9 ай бұрын
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 9 ай бұрын
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
@bradychen7980
@bradychen7980 3 ай бұрын
I was trying to set the timer frequency to 20MHz, however the code seemed to stop working at 125KHz, I was wondering if that’s the limit of the machine.Timer class
@MakingStuffwithChrisDeHut
@MakingStuffwithChrisDeHut 3 ай бұрын
That is a very high frequency to get using MicroPython. Cheers! Chris
@garystout4300
@garystout4300 2 жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@garystout4300 AWESOME! Glad to hear you got it working - that is the fun of playing with Microcontrollers.
@loadlinetubes
@loadlinetubes Жыл бұрын
Hi, Is there a way to improve timers accuracy, e.g. 0.1 ms?... Thank you.
@MakingStuffwithChrisDeHut
@MakingStuffwithChrisDeHut Жыл бұрын
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 Жыл бұрын
@@MakingStuffwithChrisDeHut ho Chris, thank you very much for your kind and quick reply. Daniele Colombi
@MakingStuffwithChrisDeHut
@MakingStuffwithChrisDeHut Жыл бұрын
Out of curiosity, what are you trying to do with the timers?
@loadlinetubes
@loadlinetubes Жыл бұрын
@@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 Жыл бұрын
@@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!
@gedtoon6451
@gedtoon6451 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
No Worries my friend. Thanks for sharing and updating the information. Cheers! Chris@@gedtoon6451
@apanoiu
@apanoiu 6 ай бұрын
I suggest using micropython and asyncio. Rendering most timers useless
@MakingStuffwithChrisDeHut
@MakingStuffwithChrisDeHut 6 ай бұрын
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 Жыл бұрын
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!
E003 Using Rotary Encoder Switches on the Raspberry Pi
15:58
Making Stuff with Chris DeHut
Рет қаралды 12 М.
Using PICO Interrupts
25:15
Making Stuff with Chris DeHut
Рет қаралды 18 М.
So Cute 🥰
00:17
dednahype
Рет қаралды 47 МЛН
SCHOOLBOY. Мама флексит 🫣👩🏻
00:41
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 6 МЛН
What is VGA and How to Use it With a Raspberry Pi Pico
18:28
Gary Explains
Рет қаралды 54 М.
Raspberry Pi Pico Lecture 3: Timers, timer interrupts, SPI
52:23
V. Hunter Adams
Рет қаралды 18 М.
How to use the PICO Watchdog Timer
11:07
Making Stuff with Chris DeHut
Рет қаралды 2,7 М.
Makerverse Supercapacitor Real-Time Clock | Raspberry Pi Pico Guide
9:04
Core Electronics
Рет қаралды 4,5 М.
Micropython Threads - Use Both Cores, on Raspberry Pi Pico and ESP32
44:21
RP2040 - Baremetal Assembly - Interrupts and Alarms
17:00
Will Thomas
Рет қаралды 2,1 М.
WAY faster than a Raspberry Pi-but is it enough?
17:26
Jeff Geerling
Рет қаралды 671 М.
Raspberry Pi Pico Stepper Motors via PIO
25:17
Tinker Tech Trove
Рет қаралды 60 М.
So Cute 🥰
00:17
dednahype
Рет қаралды 47 МЛН