Introduction: Microcontrollers Using Swift Like Arduino
Let's first come to an easy beginner project - blink the onboard LED.
This example shows the simplest thing you can do with just a SwiftIO board to see physical output: it blinks the onboard RGB LED.
Step 1:
What you need
SwiftIO board
Circuit
For this project, you only need the SwiftIO board.
There is a built-in RGB LED on the board as shown in the image above. You can control it using the methods in DigitalOut class. Note: the onboard LED will be turned on when you apply a low voltage. Just plug the board into your computer with a USB cable to download your code. Schematic
Step 2:
Schematic
As the schematic shows, the LEDs are connected in parallel to 3.3V power. Each LED is connected to a resistor in series, so you don't need to worry about the current limit.
The current will always flow from high voltage to low voltage. So if you apply a low voltage to one LED, the current will flow through the LED, so that LED will be on; if it is high voltage instead, there is no voltage difference in the circuit and thus no current. Code
It's time for the code. Let's see how it works. You can find the example code at the bottom left corner of IDE: > GettingStarted > Blink.
// Import the library to enable everything in it, like relevant classes and methods.
// This is the first step for your coding process. import SwiftIO
// Declare a constant. You may choose any descriptive name you like. // Initialize the onboard green LED. // The Id of onboard LED should be capitalized. let green = DigitalOut(Id.GREEN)
// In the dead loop, the code will run over and over again. while true { // Output 3.3V to turn off the green LED. green.write(true) // Pause for a second. Or, you won't notice LED state change. // During this period, the board will do nothing but just wait. sleep(ms: 1000) // Output 0V to turn on the green LED. green.write(false) sleep(ms: 1000) }
Step 3:
Instruction
What is the digital signal?
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 case, you will control the output voltage to turn on or off the LED.
About code
import SwiftIO refers to this named library SwiftIO. This library includes the basic commands to control input and output. All case programs must first reference it so that you can use everything in it, like classes and functions. Later when you create your own project, don't forget to import this library first. let is a keyword for Swift language to declare constants. You will often use it to assign a name to each port for easy reference in the future. This statement is to create an instance for DigitalOut class and initialize the specified pin, so the pin would be able to output high or low voltage. Id is an enumeration. All types of enumerated Id can be viewed here. It includes all IO ports. Note: You may be confused why RED, GREEN, and BLUE are not marked on pinMap. Because the SwiftIO board is equipped with a built-in RGB three-color LED by default. When you need to initialize one of them, just use its id RED, GREEN, or BLUE. Setting the while loop true means that the loop check will always run. It will not stop after entering, unless the hardware power off or restart. What will be executed over and over again is written in the loop, enclosed by a pair of curly braces {}. The .write() method belongs to the DigitalOut instance and its incoming values are true and false. They represent high-level output (3.3V) and low level (0V or GND) respectively. And for these three LED, you need false to turn on them. The sleep(ms:) function is a built-in function, which means the delay time, calculated in milliseconds. The parameter name ms must be added to pass in the parameter.

