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
- A touch sensor is mapped using the
__________class in the SDK. - To configure a digital sensor port to receive data, you must set its mode to
DigitalChannel.Mode.__________. - A potentiometer is instantiated using the
__________class, and its value is read withgetVoltage().
Show answers
DigitalChannelINPUTAnalogInput
Template Challenge
Robot Scenario: You are adding a "claw_sensor" (Digital) and a "height_pot" (Analog) to your robot. Map these components and set the required mode for the digital sensor, then display both readings on the Driver Station.
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_Map_Challenge")
public class SensorMap extends OpMode {
private DigitalChannel clawSensor;
private AnalogInput heightPot;
@Override
public void init() {
// INSERT CODE HERE: Map the DigitalChannel named "claw_sensor"
// INSERT CODE HERE: Set its mode to INPUT
// INSERT CODE HERE: Map the AnalogInput named "height_pot"
}
@Override
public void loop() {
telemetry.addData("Claw Sensor State", clawSensor.getState());
telemetry.addData("Height Voltage", heightPot.getVoltage());
}
}
Show answer
@Override
public void init() {
clawSensor = hardwareMap.get(DigitalChannel.class, "claw_sensor");
clawSensor.setMode(DigitalChannel.Mode.INPUT);
heightPot = hardwareMap.get(AnalogInput.class, "height_pot");
telemetry.addData("Status", "Sensors mapped");
}
Ready to move on?
Sign in with Google to save your progress with Telemark, or continue without saving.