LED blink using timer
In this tutorial, you’ll try a new way to blink the LED - blink using a timer.
Learning goals
- Learn how to use a timer to trigger the interrupt.
- Blink the LED using timer.
🔸Background
What is a timer?
You've known the interrupt caused by the pin state change in the previous tutorial. An interrupt will occur once a rising edge or falling edge is detected. Besides, there is also timer interrupt. It allows the interrupt to happen at every specified time interval.
In your everyday life, you must have used timers. For example, you are cooking meals, and the dish still needs another 30min. You would set a timer for 30min and do many other works since the timer will remind you after 30min. It can improve your efficiency.
The timer you are going to use is quite similar. It is hardware built on the microcontroller. You can set the expected time for interrupt and an ISR. When the set time has elapsed, the interrupt occurs, the microcontroller will go to execute the ISR. After it, the microcontroller goes back to continue its previous work.
🔸Circuit - LED module


The circuits above are simplified for your reference.
🔸Preparation
Class
Timer
- this class is used to set interrupt at a specific time interval.
Method | Explanation |
---|---|
init(mode:period) | Initialize a timer. - mode : decide how many times the interrupt will happen: .oneShot or .period , .period by default to make the interrupt happen continuously. - period : the time interval, measured in ms. |
setInterrupt( start:_:) | Set the condition to trigger the interrupt. - start : decide whether it will start as you invoke this method, or you will start it manually later by using start() . - callback : set the task for interrupt. It needs functions with no parameter and return value. |
🔸Projects
1. LED blink using timer
You will blink the LED module and onboard blue LED with different speed.

Example code
// Import the SwiftIO library to control input and output and the MadBoard to use the id of the pins.
import SwiftIO
import MadBoard
@main
public struct C01S02TimerLEDToggle {
public static func main() {
// Initialize a digital pin for LED module.
let led = DigitalOut(Id.D19)
// Initialize the onboard blue LED.
let blueLed = DigitalOut(Id.BLUE)
// Initialize a timer for 1500ms.
let timer = Timer(period: 1500)
// Define a new function used to toggle the LED.
func toggleLed() {
led.toggle()
}
// Set an interrupt to reverse the LED state every time the interrupt occurs.
timer.setInterrupt(toggleLed)
// Blink onboard blue LED.
while true {
blueLed.high()
sleep(ms: 500)
blueLed.low()
sleep(ms: 500)
}
}
}
Code analysis
let timer = Timer(period: 1500)
Initialize a timer.
- The timer's mode is
period
by default, which means the interrupt occurs every time the set time elapsed. If it it set tooneShot
, the timer interrupt happens once. - The parameter
period
sets the time for interrupt in milliseconds.
timer.setInterrupt(toggleLed)
Set the timer interrupt. Similar to the interrupt in the previous tutorial, it calls toggleLed
as ISR. So every 1500ms the state of led
changes.
while true {
blueLed.high()
sleep(ms: 500)
blueLed.low()
sleep(ms: 500)
}
In the loop, the microcontroller could do other work. Here you will make the onboard blue LED blink at a faster speed.
As you can see, you can control two LEDs without worrying their timing.