AnalogIn

public final class AnalogIn

The AnalogIn class is used to read the external voltage applied to an analog input pin.

Example: Read and print the analog input value on a analog pin

import SwiftIO

//Initialize an AnalogIn to analog pin A0.
let pin = AnalogIn(Id.A0)

//Read and print the analog input value every 1 second.
while true {
    var value = pin.readVoltage()
    print("The analog input volatge is \(value)")
    sleep(ms: 1000)
}
  • The max raw value of the ADC. Different ADC has different resolutions. The max raw value of an 8-bit ADC is 255 and that one of a 10-bit ADC is 4095.

    Declaration

    Swift

    public var maxRawValue: Int { get }
  • The reference voltage of the board.

    Declaration

    Swift

    public var refVoltage: Float { get }
  • Initialize an AnalogIn to a specified pin.

    Usage Example

    // Initialize an analog pin A0.
    let pin = AnalogIn(Id.A0)
    

    Declaration

    Swift

    public init(_ id: IdName)

    Parameters

    id

    REQUIRED The AnalogIn Id on the board. See Id for reference.

  • Read the current raw value from the specified analog pin.

    Declaration

    Swift

    @inline(__always)
    public func readRawValue() -> Int

    Return Value

    An integer in the range of 0 to max resolution.

  • Read the input voltage in percentage from a specified analog pin.

    Declaration

    Swift

    @inline(__always)
    public func readPercent() -> Float

    Return Value

    A percentage of the reference voltage in the range of 0.0 to 1.0.

  • Read the input voltage from a specified analog pin.

    Declaration

    Swift

    @inline(__always)
    public func readVoltage() -> Float

    Return Value

    A float value in the range of 0.0 to the reference voltage.