Blink an LED Using Swift Language

by madmachineio in Circuits > Microcontrollers

273 Views, 0 Favorites, 0 Comments

Blink an LED Using Swift Language

blink.jpg
Maker kit 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. This is quite discouraging. So this instructable would like to get everyone started with electronic stuff and the Swift language. Believe me, it's not so difficult as you have thought. Let's look into it in more detail.

In the very beginning, we will start certainly with the hello world project - blink the LED. You will make the LED on and off alternatively to get it to blink.

What You Will Need

swiftio2.jpg
SwiftIO.png
  • SwiftIO board

You could notice there is an onboard LED. So you will only deal with it in this project, no other components are needed.

What Is Digital Output

digitalsignal.png

The digital signal normally 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.

About the LED

LEDcircuit.png
RGB.png
LED.png

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 could only flow in one direction, from positive to negative. You should connect the positive leg to the current source.Only when you connect it in the right direction, the current could flow.

There are two ways to connect the 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. Only when the pin outputs a low voltage, the current could flow from the power to the 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 the pin to the ground, the LED will be on. If it outputs a low voltage, the LED is off.

You could find a RGB LED on your board. It has three colors: red, green and blue. As you download the code, it serves as a status indicator. Besides, you could also control its color and state by setting the output voltage.

Since there are three colors, you could light any of them: if you turn on red and blue, you could notice it appears magenta. If all three are on, the LED seems to be white.

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

The Circuit

Just connect the SwiftIO board to your computer through download port using a USB cable.

The 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 SwiftIOBoard

// 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)
}

Code Analysis

code.jpg

To program the SwiftIO board, you will need to download the MadMachine IDE to edit and build the code.

After you install the IDE, open it and create a new project. In the file main.swift, you could paste the code above.

Let's look into the code in detail:

import SwiftIO
import SwiftIOBoard

SwiftIO consists of all the functionalities to control your board. All programs must first reference it so that 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 other boards. Then you could use the id in it.

let led = DigitalOut(Id.BLUE)

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

First, declare a constant: use the keyword let, followed by a constant name led.

Then make it an instance of DigitalOut class and initialize that pin.

To initialize the pin, you need to indicate its id. All ids are listed in an enum, 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 code. Here, the pin outputs high voltage and then sleeps for 1 second. So in the first 1s, there is always a high voltage. Similarly, in the next 1s, the pin outputs low voltage.

The method write(_:) is used to set the pin to output high or low voltage. Its parameter is a boolean type: true or false: 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 sleep(ms:) function means the delay time, calculated in milliseconds. The parameter name ms must be added to pass in the parameter.

Run the Project

guide.png
download.png

As you finish the code, time to download it to your board.

Make sure the SD card is inserted into the slot. Press the download button.

Wait a few seconds. You would notice the onboard LED turns to steady green, and the message in the status bar on the IDE indicates the board is ready, then you could click the download button on the IDE.

After the code is downloaded, the onboard LED begins to blink.