Summary of Control an LED With Button Using Swift Language
This project demonstrates manual LED control using a pushbutton with the SwiftIO board. By connecting a button module to digital pin D10, users can toggle the onboard red LED on and off in real-time. The code reads the button's state within an infinite loop; pressing the button triggers a low voltage signal to illuminate the LED, while releasing it turns the light off. Hardware debouncing is utilized to prevent false readings caused by mechanical bouncing.
Parts used in Control an LED With Button Using Swift Language:
- SwiftIO board
- Shield
- Button module
- 4-pin cable

In the two previous projects, the LED turns on and off automatically. Now, you will control the LED manually using a pushbutton.
Step 1: What You Will Need

The parts you will need are all included in this Maker kit.
- SwiftIO board
- Shield
- Button module
- 4-pin cable
Step 2: Button

The button, or pushbutton, is always used to control other devices, like the light switch, or the remote control. This button is momentary, so its state will only change as you press it, once you release it, it will go back to its original state.
This kind of button usually has four legs. The two legs on the same side are shorted.
So when you connect a single button, it’s better to connect the two legs on a diagonal line.
While the button module in your kit uses the grove connector, and you could directly build the circuit without worrying about the wrong connection.
Also, there is a known issue with the button: the bounce. Due to mechanical and physical issues, when you slowly press or release the button, there might be several contacts inside the button. And the microcontroller might regard it as several presses. The button module uses the hardware debounce method, so you will not meet with this issue.
Step 3: The Circuit

The shield is a modular circuit board that makes it easier to connect the circuit. The pins on the two sides are the same as those on the SwiftIO board. Besides, it has many grove connectors, so you could use a 4-pin cable to connect the pin instead of four jumper wires.
Place the shield on the top of your SwiftIO board. Make sure you connect them in the right direction.
Connect the button module to pin D10 using a 4-pin cable. You could notice each cable has four colors of wires: the black is usually for ground, the red one is for power.
Ok, the circuit is finished. It is really convenient, isn’t it?
Step 4: 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 red onboard led.
let led = DigitalOut(Id.RED)
// Initialize the red onboard led.
let button = DigitalIn(Id.D10)
while true {
// Read the button value. If it is pressed, turn on the led.
if button.read() {
led.write(false)
} else {
led.write(true)
}
sleep(ms: 10)
}
Step 5: Code Analysis
import SwiftIO import SwiftIOBoard
First, import the two libraries: SwiftIO and SwiftIOBoard. SwiftIO is used to control the input and output of the SwiftIO board. SwiftIOBoard defines the pin name of the board.
let led = DigitalOut(Id.RED) let button = DigitalIn(Id.D10)
Initialize the red onboard LED and the digital pin (D10) the button connects to.
In the loop, you will use the if-else statement to check the button states. An if-else statement has the following form:
if condition {
statement1
} else {
statement2
}
The condition has always two results, either true or false. If it is true, statement1 will be executed; if it’s false, statement2 will be executed.
Then let’s look back to the code.
if button.read() {
led.write(false)
} else {
led.write(true)
}
The method read() allows you to get the input value. The return value is either true or false. So you could know the states of the button according to the value. If the button is pressed, the value will be true, so the pin output a low voltage to turn on the onboard LED. Once you release the button, the input value is false, the LED will turn off.
Step 6: Run the Project
As you download the code to your board, the red LED is off. If you press the button, the LED turns on. Once you release the button, the LED turns off.
Source: Control an LED With Button Using Swift Language
- How do I connect the button module?
Connect the button module to pin D10 using a 4-pin cable. - What happens when I press the button?
The red onboard LED turns on because the code writes a false value to the LED pin. - What happens when I release the button?
The red onboard LED turns off because the code writes a true value to the LED pin. - Why does the button module use hardware debouncing?
To prevent the microcontroller from registering multiple presses due to mechanical bouncing. - Which libraries are required for this project?
You must import the SwiftIO library and the SwiftIOBoard library. - What is the purpose of the shield in this circuit?
The shield makes it easier to connect the circuit and provides grove connectors for modules. - How does the code determine if the button is pressed?
The read() method returns true if the button is pressed and false if it is released. - What is the default state of the LED before running the code?
The red LED is off when the code is first downloaded to the board.
