> For the complete documentation index, see [llms.txt](https://royals6234.gitbook.io/madmachine-examples/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://royals6234.gitbook.io/madmachine-examples/examples/simpleio/buttoncontrolled.md).

# ButtoncontrolLED

![](https://gblobscdn.gitbook.com/assets%2F-MGOJWkptBbZ3bq0TpEw%2Fsync%2F7a31caa67b4783d87958a9eccb7d36f763b7b287.gif?alt=media)

In this example, we will use a push-button to control the LED.

The input signal will change as you press the button. Thus, you can set LED status according to different input states.

## What you need

* SwiftIO board
* Button
* Jumper wires

### Kits that meet the experimental conditions: <a href="#kits-that-meet-the-experimental-conditions" id="kits-that-meet-the-experimental-conditions"></a>

* ​[Maker Kit for SwiftIO](https://www.madmachine.io/product-page/maker-kit-for-swiftio)​

## Circuit

![](https://845437858-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MGOJWkptBbZ3bq0TpEw%2Fsync%2F2640ed87af38d440872298304e252f9c1806defe.png?generation=1600762625062391\&alt=media)

There is an onboard RGB LED. Please apply **low** voltage to light it.

The button has four legs. The two legs on same side are interconnected.

* Connect the leg on left side to 3.3V pin.&#x20;
* Connect the leg on right side to digital pin D10.

In default mode, the digital pin reads `false`. When you press the button, the two points on the button will be connected. And the value of pin will be `true`.

So please be sure you connected the button in a right way.

## Code

Here comes the code. You can find the example code at the bottom left corner of IDE: ![](https://845437858-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MGOJWkptBbZ3bq0TpEw%2Fsync%2F70ce0b12c2dc39b9c876ff921cab3624779013e0.jpg?generation=1599226613029534\&alt=media) > SimpleIO > ButtoncontrolLED.

```swift
// Read the input signal controlled by a button to turn on and off the LED.

// Import the library to enable everything in it, like relevant classes and methods. 
// This is first step for your coding process.
import SwiftIO

// Declare a constant. You may choose any descriptive name you like. 
// Initialize the onboard red LED. 
// The Id of onboard LED should be capitalized.
let red = DigitalOut(Id.RED)

// Initialize a digital input pin D10 the button is connected to.
let button = DigitalIn(Id.D10)

// Allow the button to control the LED all the time.
while true {
    // Check the state of button. 
    // If it is pressed, the value will be true and then turn off the LED.
    // Modify the code according to your button if necessary.
    if button.read() {
        red.write(false)
    } else {
        red.write(true)
    }
}
```

## Instruction

`DigitalIn` class is intended to detect the state of a digital input pin. The input value is either `true`(1) or `false`(0). The `.read()` function reads the value from a digital input pin.

If you have the experience with Arduino, you may notice there's no pull-down resistor on the button. That's because the SwiftIO Board already provides a pull-down function. Reference the `DigitalIn` class for more information.

## See Also

* [Id](https://swiftioapi.madmachine.io/Enums/Id.html) - Enumerations of all the pins on the board.
* [DigitalIn](https://swiftioapi.madmachine.io/Classes/DigitalIn.html) - Detect the state of a digital input pin. The input value is either true (1) or false (0).
