Analog Inputs - Software

Note

This section covers analog inputs in software. For a hardware guide to analog inputs, see Analog Inputs - Hardware.

The Systemcore’s Smart I/O supports up to 6 analog input channels that can be used to read the value of an analog voltage from a sensor. Analog inputs may be used for any sensor that outputs a simple voltage.

Analog inputs return a 12-bit integer proportional to the voltage, from 0 to 3.3 volts.

Note

Systemcore Analog Inputs are 3.3 volts, whereas the roboRIO was 5 volts.

The AnalogInput class

Note

It is often more convenient to use the Analog Potentiometers wrapper class than to use AnalogInput directly, as it supports scaling to meaningful units.

Support for reading the voltages on the Smart I/O ports is provided through the AnalogInput class (Java, C++).

Initializing an AnalogInput

An AnalogInput may be initialized as follows:

  // Initializes an AnalogInput on port 0
  AnalogInput analog = new AnalogInput(0);
  // Initializes an AnalogInput on port 0
  wpi::AnalogInput analog{0};

Reading values from an AnalogInput

Values can be read from an AnalogInput with one of two different methods:

getValue

The getValue method returns the raw instantaneous measured value from the analog input, without applying any calibration. The returned value is an integer.

    // Gets the raw instantaneous measured value from the analog input, without
    // applying any calibration and ignoring oversampling and averaging
    // settings.
    analog.getValue();
    // Gets the raw instantaneous measured value from the analog input, without
    // applying any calibration and ignoring oversampling and averaging
    // settings.
    analog.GetValue();

getVoltage

The getVoltage method returns the instantaneous measured voltage from the analog input. The value is rescaled to represent a voltage. The returned value is a double.

    // Gets the instantaneous measured voltage from the analog input.
    // Oversampling and averaging settings are ignored
    analog.getVoltage();
    // Gets the instantaneous measured voltage from the analog input.
    // Oversampling and averaging settings are ignored
    analog.GetVoltage();

Differences between roboRIO and Systemcore

Systemcore does not support oversampling or averaging, nor does it have an accumulator on the Smart I/O ports (used for Analog Gyros).

Using analog inputs in code

The AnalogInput class can be used to write code for a wide variety of sensors (including potentiometers, accelerometers, gyroscopes, ultrasonics, and more) that return their data as an analog voltage. However, if possible it is almost always more convenient to use one of the other existing WPILib classes that handles the lower-level code (reading the analog voltages and converting them to meaningful units) for you. Users should only directly use AnalogInput as a “last resort.”

Accordingly, for examples of how to effectively use analog sensors in code, users should refer to the other pages of this chapter that deal with more-specific classes.