Skip to content
Mechanical, Build & Pneumatics·Lesson 28 of 47

Programming Solenoids and the Compressor in WPILib

Write the WPILib code that runs the compressor and switches single and double solenoids, with real Java examples.

Sign in to track progress, earn XP, and save lessons.

Telling WPILib Which Module You Have

Every pneumatics class takes a PneumaticsModuleTypeREVPH for the REV Pneumatic Hub or CTREPCM for the CTRE PCM. WPILib's Solenoid, DoubleSolenoid, and Compressor classes do the rest; the controller hardware handles closed-loop compressor control automatically.

Single Solenoid

A Solenoid controls one channel. Construct it with the module type and channel, then turn it on or off:

Solenoid exampleSolenoid = new Solenoid(PneumaticsModuleType.REVPH, 0);
exampleSolenoid.set(true);   // energize
exampleSolenoid.set(false);  // de-energize (springs back)
exampleSolenoid.toggle();    // flip current state

Double Solenoid

A DoubleSolenoid takes a forward channel and a reverse channel, and is set to one of three states: kForward, kReverse, or kOff:

DoubleSolenoid exampleDouble =
    new DoubleSolenoid(PneumaticsModuleType.CTREPCM, 1, 2);
exampleDouble.set(DoubleSolenoid.Value.kForward);
exampleDouble.set(DoubleSolenoid.Value.kReverse);
exampleDouble.set(DoubleSolenoid.Value.kOff);  // both off, holds last air state

A DoubleSolenoid defaults to kOff, so you must set() it before toggle() will work. You can also pass an explicit CAN ID, e.g. new DoubleSolenoid(9, PneumaticsModuleType.REVPH, 4, 5).

Running the Compressor

The Compressor class manages automatic compressor control. There are three modes:

Compressor compressor = new Compressor(PneumaticsModuleType.REVPH);

// 1) Digital: run until the pressure switch reports full. Works on PH and PCM.
compressor.enableDigital();

// 2) Analog: PH only - run while pressure is between min and max (psi)
compressor.enableAnalog(70, 120);

// 3) Hybrid: PH only - analog range AND the digital switch agree it is not full
compressor.enableHybrid(70, 120);

compressor.disable();                  // stop automatic control
double psi = compressor.getPressure(); // live pressure (PH analog only)

Closed-loop (digital) control is enabled by default when a Compressor is constructed, so simply creating the object usually starts the compressor running under pressure-switch control once the robot is enabled.

Best Practices

  • Put solenoid objects in a subsystem and expose methods like extend() / retract() rather than calling set() from all over your code.
  • For double solenoids, decide a sensible disabled/init state (kForward, kReverse, or kOff).
  • getPressure() returns valid data only with the REV analog sensor on a PH; on a PCM use the digital switch state instead.
  • Test on the bench with the REV Hardware Client before relying on robot code.

Key takeaways

  • Every pneumatics class takes PneumaticsModuleType.REVPH or CTREPCM; the hardware handles compressor closed-loop control.
  • Single solenoid: Solenoid(module, channel) with set(true/false) and toggle().
  • Double solenoid: DoubleSolenoid(module, fwd, rev) set to kForward, kReverse, or kOff; it defaults to kOff.
  • Compressor modes: enableDigital() (any module), enableAnalog(min,max) and enableHybrid(min,max) (PH only); getPressure() needs the PH analog sensor.

Lesson quiz

Required

Answer all 3 questions correctly to complete this lesson.

1.In WPILib, how many output channels does a DoubleSolenoid use and what states can it hold?

2.Which value must you pass for the PneumaticsModuleType when constructing a Solenoid on a REV Pneumatic Hub?

3.What is true about closed-loop compressor control in WPILib by default?

Answer every question to submit.