Blink RGB LEDs One by One Using Swift Language

by madmachineio in Circuits > Arduino

232 Views, 0 Favorites, 0 Comments

Blink RGB LEDs One by One Using Swift Language

Mission2_RGB_LED.jpg
Maker kit Mission2 RGB LED

After successfully get an LED to work, why not try more LEDs? In this project, let's build the circuit and make the LEDs blink one after another.

What You Will Need

ledsparts.png

The parts you will need are all included in this Maker kit.

  • SwiftIO board
  • Breadboard
  • Red, green and blue LEDs
  • Resistor
  • Jumper wires

About LED

LEDcircuit.png
LED-01.png

The light-emitting diode, LED for short, is one kind of diode that could be illuminated. It is has a positive leg (anode) and a negative leg (cathode). Then how to identify the two legs? Well, the long leg is positive and the short leg is negative. The current could only flow in one direction, from positive to negative. So to light it, you need to connect the positive leg to the current source.

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. This is the way you're going to use in this project.

The Circuit

ledschematics.png
LEDcircuitdiagram.png
PINOUT DIAGRAM OF SWIFTIO.png
breadboard.png

Let's know something about the breadboard first. You could find many holes in it. Actually, each upper or lower five sockets besides the gap in the middle are shorted vertically as shown above. So you could easily insert components in to these holes.

  1. Place three LEDs on the different columns.
  2. The long leg of each LED is connected to a digital pin: red LED connects to D16, green LED connects to D17, blue LED connects to D18.
  3. The short leg is connected to a 1k ohm resistor and goes to the pin GND.

Note: the resistance of the resistor are not determined, as long as it is bigger than the minimum requirement to resist the voltage. And the brightness of the LED is decided by the resistor: its resistance is higher, the LED will be dimmer.

BTW, you could usually find that the red jumper wires are used for power, the black wires are for ground.

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 three LEDs.
let red = DigitalOut(Id.D16)
let green = DigitalOut(Id.D17)
let blue = DigitalOut(Id.D18)


// The code here will run all the time.
while true {

    // Turn on red LED for 1 second, then off.
    red.write(true)
    sleep(ms: 1000)
    red.write(false)

    // Turn on green LED for 1 second, then off.
    green.write(true)
    sleep(ms: 1000)
    green.write(false)

    // Turn on blue LED for 1 second, then off.
    blue.high()
    sleep(ms: 1000)
    blue.low()
}

Code Analysis

import SwiftIO
import SwiftIOBoard

First, import the two libraries: SwiftIO and SwiftIOBoard to use everything it. SwiftIO is used to control the input and output of SwiftIO board. SwiftIOBoard defines the pin name of the board.

let red = DigitalOut(Id.D16)
let green = DigitalOut(Id.D17)
let blue = DigitalOut(Id.D18)

The DigitalOut class allows you to set the pin to output high or low voltage. You need to initialize the three output pins: D16, D17, and D18 that the LEDs connect. Only after initialization, you could change their state.

while true {
}

To make the LED blinks repeatedly, you need to write the code in the dead loop while true. The code inside it could run all the time unless you power off the board.

red.write(true)
sleep(ms: 1000)
red.write(false)<br>

In the loop, you will set three LEDs separately. The operations are similar. Let's look at the red LED.

At first, the pin outputs a high voltage to light the LED. Since each of the three LEDs connects to the digital pin and ground, they will be lighted as you apply a high voltage. After 1s, turn off the LED by applying low voltage. So the LED will be on for 1s and then be turned off.

The next LED turns on immediately and repeats the process above. Thus three LEDs blink in turns.

Run the Project

As the code is downloaded to your board, the red, green, and blue LED blink one after another. Each LED will be on for 1s.