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.
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.
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.
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.
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.
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.
You can use the arrows ← →
on your keyboard to navigate between lessons.