Introduction to Sensors
Understand the basics of hardware sensors in FTC, focusing on Digital vs Analog, and how to initialize them in your code.
By the end you can
- Understand the difference between Digital and Analog sensors.
- Learn how to use DigitalChannel in the HardwareMap.
- Understand Normally Open vs Normally Closed states.
Robots are blind until you give them sensors. While we've used Odometry and Encoders to tell the robot where it is, we need different types of sensors to tell the robot what is happening inside or around it.
For example, how does the robot know if it has successfully collected a game piece? How does an elevator know when it has safely reached the bottom?
Digital vs Analog
There are two primary categories of sensors in FTC:
- Analog Sensors: These sensors return a continuous range of values. For example, a Distance Sensor might return
12.5 cmor4.2 cm. A Color Sensor might return varying levels of Red, Green, and Blue light. - Digital Sensors: These sensors are binary. They only ever return
trueorfalse(ONorOFF,HIGHorLOW).
Our team relies heavily on Digital Sensors for our internal mechanisms because they are incredibly fast, reliable, and easy to program.
The two most common digital sensors are:
- Limit Switches: A physical button that gets clicked when a moving part hits it.
- Beam Breaks: A laser that shines across an empty space into a receiver. If a game piece blocks the laser, the sensor triggers.
Initializing a Digital Sensor
In FTC, both Limit Switches and Beam Breaks plug into the digital ports on your Control Hub. In your Java code, they are both represented by the exact same class: DigitalChannel.
Here is how you initialize a digital sensor in a Subsystem:
import com.qualcomm.robotcore.hardware.DigitalChannel;
import com.qualcomm.robotcore.hardware.HardwareMap;
public class MySubsystem {
// 1. Declare the sensor
private DigitalChannel limitSwitch;
public MySubsystem(HardwareMap hardwareMap) {
// 2. Map it to the name configured on the Driver Station phone
limitSwitch = hardwareMap.get(DigitalChannel.class, "liftResetSensor");
// 3. Set the mode! (Crucial for digital sensors)
limitSwitch.setMode(DigitalChannel.Mode.INPUT);
}
public boolean isSwitchPressed() {
// 4. Read the state (returns true or false)
return limitSwitch.getState();
}
}[!IMPORTANT] You must always call
.setMode(DigitalChannel.Mode.INPUT)on a digital sensor before using it. This tells the Control Hub to read electricity from the port, rather than sending electricity out (which would beOUTPUT).
Normally Open vs Normally Closed
When reading the .getState() of a digital sensor, you might assume that true means "pressed/blocked" and false means "empty/open". This is often incorrect!
It depends on how the sensor is physically wired:
- Normally Open (NO): The circuit is naturally broken. Electricity only flows when the switch is pressed. Therefore,
.getState()returnsfalsenormally, andtruewhen pressed. - Normally Closed (NC): The circuit is naturally connected. Electricity flows constantly. When the switch is pressed, it breaks the circuit. Therefore,
.getState()returnstruenormally, andfalsewhen pressed!
Most modern FTC Beam Break sensors (and many limit switches) are wired Normally Closed as a safety feature. If the wire accidentally gets unplugged during a match, the robot will read false and think the switch is pressed, causing the mechanism to stop safely instead of destroying itself!
This means we often have to invert the logic in our code:
// If the sensor is Normally Closed, a state of 'false' means the beam is broken!
public boolean isBeamBroken() {
return !beamBreakSensor.getState();
}Check Your Understanding
Check yourself