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
Simulator Challenge
Use the simulator below to practice mapping digital and analog sensors with the correct SDK classes and setup steps.
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.