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.
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
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.
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.
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.
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!
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
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!
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
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?
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
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?
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
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!
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.
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
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!
@@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
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
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
@@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.
@@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!
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).
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
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.
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)
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!
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.
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
Well explained and for the like and subscription well deserved!!! Thanks for this simple but very good demo!!!
Thank you very much Norbertbans! I really appreciate the feedback and kind words - happy programming!
Thank you for the explanation! It helps me a lot!
Glad it helped!
Cheers!
Chris
Thank you for the info on timers and IRQ handling. Looking forward to your eventual updates.
Glad you liked the videos. I have plenty more videos coming up.
Cheers!
Chris
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.
Thanks for the feedback! I certainly hope things improve for you so you can bring your shop back to life!!!!
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.
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.
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!
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
Thank you very much for your workshoop so it was very helpful
Thank you for watching and for the feedback! Be sure to let others know about this channel.
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!
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
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?
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
Good stuff … subscribed
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
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?
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
Coming from Arduino ide to micro python, thanks for your help. Will check your other videos.
Hi VoeViking! Thanks for stopping by and glad you found some value in the form of help.
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!
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.
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
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!
@@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
@@garystout4300 AWESOME! Glad to hear you got it working - that is the fun of playing with Microcontrollers.
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
That is a very high frequency to get using MicroPython.
Cheers!
Chris
Nice video, thanks for it.
Thanks for watching! Much appreciated.
Hi, Is there a way to improve timers accuracy, e.g. 0.1 ms?...
Thank you.
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
@@MakingStuffwithChrisDeHut ho Chris, thank you very much for your kind and quick reply.
Daniele Colombi
Out of curiosity, what are you trying to do with the timers?
@@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.
@@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!
nice video!
Rafael, thank you very much for the feedback- much appreciated! Cheers!
good stuff
Thank you for the kind words!
Cheers!
Chris
👍
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).
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
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.
No Worries my friend. Thanks for sharing and updating the information.
Cheers!
Chris@@gedtoon6451
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)
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!
I suggest using micropython and asyncio. Rendering most timers useless
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