Skip to main content

Lesson 7.3: Mapping Digital and Analog Sensors into Java Objects


Technical Context

Sensors are mapped to different hardware buses: Digital, Analog, or I2C. Mapping a DigitalChannel (like a touch sensor) using the AnalogInput class, or vice versa, will result in incorrect voltage readings or hardware errors. The SDK uses distinct classes for each sensor type to enforce this separation and prevent silent logic failures.


How Sensor Mapping Stays Organized

The SDK differentiates between two common sensor categories:

Digital sensors (e.g., REV Touch Sensor, magnetic limit switches) operate on a binary signal: the pin is either HIGH or LOW. They are instantiated with the DigitalChannel class and return a boolean via getState(). One critical step: you must explicitly call setMode(DigitalChannel.Mode.INPUT) to configure the pin to receive data rather than send it.

Analog sensors (e.g., potentiometers, analog distance sensors) return a continuous voltage between 0V and the maximum port voltage. They are instantiated with the AnalogInput class and return a double via getVoltage(). Analog input ports do not require a mode to be set.


Annotated Code

package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DigitalChannel;
import com.qualcomm.robotcore.hardware.AnalogInput;

@TeleOp(name="Sensor_Mapping_Demo")
public class SensorMappingDemo extends OpMode {

private DigitalChannel touch;
private AnalogInput pot;

@Override
public void init() {
// Mapping to a Digital port: requires INPUT mode to be set
touch = hardwareMap.get(DigitalChannel.class, "limit_switch");
touch.setMode(DigitalChannel.Mode.INPUT);

// Mapping to an Analog port: functional immediately upon registration
pot = hardwareMap.get(AnalogInput.class, "arm_pot");

telemetry.addData("Status", "Sensors mapped");
}

@Override
public void loop() {
boolean isPressed = !touch.getState(); // Active-low: false = pressed
double armVoltage = pot.getVoltage();

telemetry.addData("Limit Switch Pressed", isPressed);
telemetry.addData("Arm Pot Voltage", armVoltage);
}
}

Fill-in-the-Blank Practice

  1. A touch sensor is mapped using the __________ class in the SDK.
  2. To configure a digital sensor port to receive data, you must set its mode to DigitalChannel.Mode.__________.
  3. A potentiometer is instantiated using the __________ class, and its value is read with getVoltage().
Show answers
  1. DigitalChannel
  2. INPUT
  3. AnalogInput

Simulator Challenge

Use the simulator below to practice mapping digital and analog sensors with the correct SDK classes and setup steps.

Telemark Unit 7 Simulator
Supports multiple Java files: hover over the tabs and choose + to create a separate mechanism or configuration class, or to import existing code.
Shows mapping errors, type mismatches, direction settings, and missing initialization hints.
Best for practicing `hardwareMap.get()`, device classes, naming consistency, and mapping structure.

Ready to move on?

Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.

Stuck on this lesson?

Ask about anything on this page. It can see which lesson you have open and which part you are reading.