🎨
MadMachine-Examples
  • Introduction
  • About our project
  • Getting Started
  • Tutorial
    • What is SwiftIO
    • How to use IDE
    • Libraries
    • MM SDK
  • Built-In Examples
    • GettingStarted
      • Blink
      • ReadDigitalInput
      • ReadAnalogInput
      • PWMBrightnessControl
    • SimpleIO
      • ButtoncontrolLED
      • BlinkAnalogIn
      • BlinkTimer
      • BrightnessAnalogIn
      • Debounce
      • LEDsBrightnessControl
      • PWMSoundOutput
      • PWMMelody
    • AdvancedIO
      • ILI9341
      • LCD1602
      • MidiPlayer
      • PCA9685
      • SHT3x
      • SSD1315
    • Challenge
      • LCDFamily
      • RobotArm
      • Tetris
    • MakerKit
      • Mission 01 Blink
      • Mission 02 RGB LED
      • Mission 03 Push Button
      • Mission 04 Potentiometer RGB
      • Mission 05 Buzzer
      • Mission 06 Seven Segment Display
      • Mission 07 DC Motor
      • Mission 08 Servo Motor
      • Mission 09 LCD
      • Mission 10 Humiture Sensor
  • Library Reference
  • FAQ
Powered by GitBook
On this page
  • What you need
  • Circuit
  • Code
  • Instruction
  • See Also
  • Tips

Was this helpful?

  1. Built-In Examples
  2. GettingStarted

ReadDigitalInput

PreviousBlinkNextReadAnalogInput

Last updated 4 years ago

Was this helpful?

In this example, let's try to read digital signal using a pushbutton. The input value is true or false. Here you will use serial monitor to show the it.

What you need

  • SwiftIO board

  • Button

  • Jumper wires

Circuit

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

Connect one leg on the left side to 3.3 pin. And 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

// Read the input voltage on a specified digital pin. 
// The value you get will be either true or false.

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

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

// 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 to slow the reading frequency.
    sleep(ms: 1000)
}

Instruction

The parameter passed by the object DigitalIn must be the ports (D0-D45) that can be used for digital input in the enumeration Id. The return value is true or false, representing high level and low level respectively.

The print() function is to print the result directly to the serial port. You can conveniently use a computer to connect to the serial port of the SwiftIO Board to view the results and debug.

See Also

Tips

You can find the example code at the bottom left corner of IDE: > GettingStarted > ReadDigitalInput.

Please note that the SwiftIO Board has two USB ports. The port used to load programs cannot be used as a monitor serial port, so you need to change the line. For details, please see the operations below. Both USB ports can be used as power supply ports for SwiftIO Board.

​ - Enumerations of all pins on the board.

​ - Detect the state of a digital input pin. The input value is either true (1) or false (0).

Id
DigitalIn.read()
Tips