Skip to main content

Read digital input

In this example, let's try to read digital signals using a pushbutton. When you press or release the button, the input value will change accordingly, either true or false. You can see the result in the serial monitor.

What you need

  • SwiftIO Feather (or SwiftIO board)
  • Breadboard
  • Button
  • Jumper wires

Circuit

  1. Plug the button into the breadboard. Make sure the button is not in the wrong direction.
  2. Connect one leg on the left side to the pin 3V3.
  3. Connect one leg on the right side to pin D1.
circuit diagram
note

In a breadboard, the holes beside the blue horizontal line are all connected. They are for ground. The holes beside the red line are the same and connect to the power pin. In the middle, there are 30 rows. The holes A, B, C, D, E in the same row are connected internally. The holes F, G, H, I, J are the same. So make sure the button is placed in the right direction.

info

You can notice the wires are in different colors. Usually, red is for the wire connected to power, and black is for ground.

Example code

You can find the example code by clicking the bottom left corner of IDE: example / GettingStarted / ReadDigitalInput.

// Read the input voltage on a specified digital pin. The value you get will be either true or false.
// Import the library to enable the relevant classes and functions.
import SwiftIO

// Import the board library to use the Id of the specific board.
import MadBoard

// Initialize the pin D1 as a digital input pin.
let pin = DigitalIn(Id.D1)

// read the input every second.
while true {
// Declare a constant to store the value you read from the digital pin.
let value = pin.read()
// Print the value and you can see it in the serial monitor.
print(value)
// Wait a second and then continue to read.
sleep(ms: 1000)
}

Background

Button

The button, or pushbutton, is always used to control other devices. You can see it in a light switch, remote control, etc. 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.

button

This kind of button usually has four legs. Every two legs are shorted, like 1 and 3, 2 and 4.

If you connect both 1 and 3 to your circuit, the current can flow through it all the time. So the button cannot control other components.

So it's a good idea to connect the two legs on a diagonal line in the circuit, like 1 and 4.

Code analysis

import SwiftIO
import MadBoard

SwiftIO consists of all the functionalities to control your board. All programs must first reference it to use everything in it, like classes and functions.

MadBoard defines the corresponding pin id of the different boards. You will not tell a specific board here since you have chosen a specified board for your project when creating it.

let pin = DigitalIn(Id.D1)

The button connects to pin D1. The digital pins can be used as both input and output. You will set it when initializing the pin. Here the pin serves as input, so you need the class DigitalIn.

let value = pin.read()

The method read() allows you to get the input value. The return value is either true or false, representing high and low level respectively. So you can know the states of the button according to the values. If you press the button, the pin connects to 3V3, so the value will be true. Once you release the button, the input value will be false.

print(value)

The function print() can print the values out. You can view them on the serial monitor by connecting the serial port to your computer. Here is a guide to tell you how to open the serial monitor.

Reference

DigitalIn - read the input value from a digital pin.

  • init(_:mode:) - initialize the digital input pin. The first parameter needs the id. You can refer to the corresponding Id enumeration. The parameter mode already has a default value.

  • read() - read values from a input pin. It will return a boolean value. true corresponds to a high level, and false corresponds to a low level.

print() - print the values out.

MadBoard - find the corresponding pin id of your board.