Is the new Raspberry Pi AI Kit better than Google Coral?
128827 Views
Build Your Own AI Assistant Part 1 - Creating the Assistant
120382 Views
Control Arduino with Python using Firmata / PyFirmata
88230 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
62460 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
58121 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
53235 Views
Pico Plotter
LEGO Gets Lights & Sound with Tiny FX
Thinkman
Podman vs Docker
MicroPython Robotics
Bottango and Isaaca
Getting Started with C on the Raspberry Pi Pico
0h 20m
Running K3s on Raspberry Pi
0h 36m
From Docker to Podman
0h 28m
MicroPython Robotics Projects with the Raspberry Pi Pico
0h 24m
Bottango Basics
0h 22m
Mini-Rack 3D Design Tutorial
Learn how to Program in Python, C, Rust, and more.
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
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 >
You can use the arrows ← → on your keyboard to navigate between lessons.
← →