Skip to main content

Mission1_Blink

As you get a new board, if you don't have some previous knowledge, you might not be able to get it to work out of the box. It is so discouraging. So this first project would like to get everyone started with electronic stuff and the Swift language. You will start with the hello world project - blink the LED. You will make the LED on and off alternatively to get it to blink. Let's break all stuff down to see how it works.

What you need

  • SwiftIO board
SwiftIO board

You can notice there is an onboard LED (marked with a red box above). You will only deal with it in this project, and there is no need for other components.

Circuit

Just connect the SwiftIO board to your computer through the download port using a USB cable. There are two ports on the board. The one beside the golden ring is the download port.

Connect the SwiftIO board to your computer

Example code

// Import the SwiftIO library to use everything in it.
import SwiftIO

// Import the board library to use the Id of the specific board.
import MadBoard

// Initialize the blue LED
let led = DigitalOut(Id.BLUE)

// The code here will run all the time.
while true {
// Set Blue LED off.
led.write(true)
// Interval of LED blink (milliseconds).
sleep(ms: 1000)

// Set Blue LED on.
led.write(false)
sleep(ms: 1000)
}

Background

Digital signal

The digital signal usually has two states. Its value is either 1 or 0. For the SwiftIO board, 1 represents 3.3V, and 0 represents 0V. There are also other ways to express the same meaning: high or low, true or false. In this project, you will control the output voltage to turn on or off the LED.

Digital signal

LED

The LED, or light-emitting diode, is one kind of diode. It has a positive leg (anode) and a negative leg (cathode). The long leg is positive, and the short leg is negative. The current can only flow in one direction, from positive to negative. You need to connect the positive leg to the current source. Only when you connect it in the right direction, the current can flow.

LED

There are two ways to connect the LED:

Connect LED
  1. Connect the LED to the power and a digital pin. Since the current always flows from high to low voltage, if the pin outputs a high voltage, there is no voltage difference between the two ends of the LED, so the LED is off. When the pin outputs a low voltage, the current can flow from the power to that pin, and the LED will be on. This is how the onboard LED works.
  2. Connect the LED to the digital pin and ground. If the pin outputs a high voltage, the current flows from that pin to the ground, and the LED will be on. If it outputs a low voltage, the LED is off.

You can find an RGB LED on your board. It is a different type from the images above for easier soldering. It has three colors: red, green and blue. As you download the code, it serves as a status indicator. Besides, you can also control its color and state by setting the output voltage.

You can light any, so it will appear red, green, or blue. You can also turn on two of them. If you turn on red and blue, you can notice it appears magenta. If all three are on, the LED seems to be white.

RGB colors

The onboard LED is connected to 3.3V internally. If you set it to high voltage, there will be no current. So it will be lighted when you apply low voltage.

Code analysis

Let's look into the code in detail:

import SwiftIO
import MadBoard

SwiftIO consists of all the functionalities to control your board. All programs must first reference it so you can use everything in it, like classes and functions.

SwiftIOBoard defines the corresponding pin id of the SwiftIO board. The pins of different boards are different. So this library tells the IDE you are dealing with the SwiftIO board, not any others. Then you can use the id in it.

let led = DigitalOut(Id.BLUE)

Before you set a specific pin, you need to initialize it.

  1. First, declare a constant: use the keyword let followed by a constant name led.
  2. Then make it an instance of DigitalOut class and initialize that pin.
  3. To initialize the pin, you need to indicate its id. All ids are in an enumeration, and the built-in RGB LEDs use the id RED, GREEN, or BLUE. Thus the id of blue LED here is written as Id.BLUE using dot syntax.
while true { 
led.write(true)
sleep(ms: 1000)
led.write(false)
sleep(ms: 1000)
}

In the dead loop while true, all code in the brackets will run over and over again unless you power off the board.

The method write(_:) is used to set the pin to output high or low voltage. Its parameter is a boolean type: true corresponds to a high level, and false corresponds to a low level. And as mentioned above, you need to set a low voltage to turn on the LED.

The function sleep(ms:) will stop the microcontroller's work for a specified period. It needs a period in milliseconds as its parameter. It is a global function in the library, so you can directly use it in your code.

So the code above makes LED alternate between off and on every second.

Reference

DigitalOut - set whether the pin output a high or low voltage.

sleep(ms:) - suspend the microcontroller's work and thus make the current state last for a specified time, measured in milliseconds.

SwiftIOBoard - find the corresponding pin id of SwiftIO board.