Skip to main content

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.

Timer

🔸Circuit - LED module

LED module on the kitLED circuit diagram
note

The circuits above are simplified for your reference.

🔸Preparation

Class

Timer - this class is used to set interrupt at a specific time interval.

MethodExplanation
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.

LED blink using timer

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

// 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 to oneShot, 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.