114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
84% Percent Complete
By Kevin McAleer, 3 Minutes
In MicroPython, a timer is a hardware component that can be used to measure time intervals, trigger events, and schedule tasks. Timers are essential for time-sensitive applications, such as controlling motors, reading sensors, and managing communication protocols.
timer
A timer is a hardware component that keeps track of time intervals. It can be configured to trigger events at specific intervals, making it useful for a variety of applications that require precise timing.
In MicroPython, the machine module provides functionality to configure and use timers. Here’s how you can set up and use timers in your projects.
machine
Here’s an example of setting up a periodic timer that triggers an event every second:
import machine # Define the timer callback function def tick(timer): print("Tick!") # Create a timer object timer = machine.Timer(-1) # Initialize the timer to trigger the callback every second (1000 ms) timer.init(period=1000, mode=machine.Timer.PERIODIC, callback=tick)
In this example, the tick function is called every second by the timer.
tick
Here’s an example of setting up a one-shot timer that triggers an event after 5 seconds:
import machine # Define the timer callback function def timeout(timer): print("Timeout!") # Create a timer object timer = machine.Timer(-1) # Initialize the timer to trigger the callback once after 5 seconds (5000 ms) timer.init(period=5000, mode=machine.Timer.ONE_SHOT, callback=timeout)
In this example, the timeout function is called once after 5 seconds.
timeout
When using timers in MicroPython, consider the following best practices:
For more advanced applications, you can use multiple timers, configure different modes, and combine timers with interrupts for precise control over time-sensitive tasks.
Here’s an example of using two timers with different intervals:
import machine # Define the first timer callback function def timer1_callback(timer): print("Timer 1 Tick!") # Define the second timer callback function def timer2_callback(timer): print("Timer 2 Tick!") # Create two timer objects timer1 = machine.Timer(0) timer2 = machine.Timer(1) # Initialize the first timer to trigger the callback every second (1000 ms) timer1.init(period=1000, mode=machine.Timer.PERIODIC, callback=timer1_callback) # Initialize the second timer to trigger the callback every half second (500 ms) timer2.init(period=500, mode=machine.Timer.PERIODIC, callback=timer2_callback)
In this example, timer1 triggers every second, and timer2 triggers every half second.
timer1
timer2
Timers in MicroPython are powerful tools for managing time-sensitive tasks. By configuring and using one-shot and periodic timers, you can create precise and efficient programs. Following best practices ensures that your timer callbacks are efficient and do not interfere with the overall operation of your program.
< Previous Next >