Solenoids in Java

I can’t really find anything on how to properly code solenoids in java all ive got for solenoids is


if (m_driverController.getRawButton(kDoubleSolenoidForward)) {


} else if (m_driverController.getRawButton(kDoubleSolenoidReverse)) {



Seems like a reasonable start.

If you are looking for some documentation or small examples…
Have you looked through the WPIlib docs on Double solenoid’s?

would something like this possibly work

if (m_driverController.getRawButton(kDoubleSolenoid)) {



if (m_driverController.getRawButton(kSolenoidButton)) {


1 Like

Depends on what you want the solenoid to do, and whether this code is getting run periodically or not.

Keep in mind every time toggle() is called, the solenoid will change state. If this code is being called in teleopPeriodic, and a button is pushed, this will switch the cylinder position every 20ms. I suspect this is not the behavior you are looking for

However, general advice: always work methodically. Start by defining what you want the behavior to be, find functions which help you get closer to that behavior, and check whether the code you write matches your requirements. Guess and check has its place, but is often inneficent to quickly write functional code.


ok how would i go about fixing it i just need the solenoids to fire on and off with a button like press the button and they stay either open or closed like a pinch and the single to lift

1 Like

So the next steps I would take:

Start by reading the docs and examples to figure out what “tools” wpilib provides you to solve the problem.

For example, calling the .set() method will change the state of the solenoid. Not calling any .set() method will leave the solenoid in its previous state.

With that in mind, how might you use if statements and button states to only call .set() when appropriate?

so this is what i currently have for the solenoids

  // Solenoids
  // Defaults Double Solenoid Pinch Closed
  // Defaults Single Solenoid Lift Up
  // Opens Double Solenoid Pinch
if (m_driverController.getRawButton(5)) {
  // Closes Double Solenoid Pinch
 if (m_driverController.getRawButton(3)) {
  // Raises Single Solenoid Lift
if (m_driverController.getRawButton(1)) {
  // Lowers Single Solenoid Life
if (m_driverController.getRawButton(2)) {

will this work properly???

Presumably this code will end up inside teleopPeriodic?

If that’s the case, this code will be getting run ~50x per second.

So it’s good to keep in mind that the “default” positions you have identified will be re-asserted many times a second.

I would say if you move the code that asserts default positions off into the robotinit method, and leave the remaining code in teleopperiodic, your code would make sense to allow toggling the solenoid positions with the different buttons referenced.

The question of will it work is harder to answer because we don’t have the complete robot code or info on how the robot is wired/plumbed, nor a notion of what behaviors/controls you would deem acceptable to solve your problem.

One way you can answer the question for yourself is to execute your code in simulation mode right on your computer. This will allow you to check if what you expect should happen actually does once the code is running.

There’s info here on how to setup and execute your code in sim:

You won’t see a robot do stuff, but you can verify there are no compilation/runtime errors, and then see what happens to your solenoid output states whe. You press the buttons on your keyboard or a real joystick plugged into your computer. It a great stepping stone to verifying your expectations when you don’t have a robot sitting in front of you to deploy code to

so the double solenoid appears to have them set so one opens and one is closed when the button is pressed how would i fix this so either both open or both close

I think what you’re saying is that when you press button 5 and 3 you see the two outputs for the double solenoid toggling, but only one output is energized at a time?

The above is how kForward and kReverse states of a double solenoid are intended to work. There are two other states the dounle solenoid can be in, kOff (where both solenoid channels are powered off) and kOn (where both solenoid channels are powered on).
Aside: It looks like kOn isn’t a valid value for a doublesilenoid, it is for a relay device, I may be misremembering it as a possible output option or it was a modification to the library to discourage people using it (see below).

This video may be helpful for visualizing how a pneumatic 5/3 double solenoid valve physically works to port the air to the cylinder:

Note that if you energized both of the coils in the double solenoid, you wouldnt actually cause motion of the valve cartridge. Air wouldn’t be routed (ported) differently, so no resulting motion of the cylinder would occur.

If this is the kind of pneumatic valve you’re using on your robot, you probably don’t actually want both outputs to be energized on simultaneously.

tomorrow morning I’ll be able to test the code on the robot so I’ll do that then get back to you because i dont really know exactly what kinda valves we got going on.

This video might be a better one to follow.

I’d forgottent that center blocked valves are not typically used (against rules iirc) in FRC, the valve on your robot likely only has two states it can be in (vs the 3 positions illustrated in the video above)

If you want to take pictures of the mechanism, valve(s), plumbing, I’m sure myself/others can help explain what’s going on further.

Here are the solenoids on the robot the pinch are both attached to the same solenoid

They look like they are both double solenoids to me. The way you can tell is a double solenoid would have coils (where the wires plug in/connect to the valve assembly) on both ends. A single sole oid just has one coil on one end, which makes it look asymmetrical.

In that style of SMC valve,
A single solenoid looks something like this:

A double solenoid looks like this:

Can you explain why you were using single vs double in your code?

It was just an error I realized after looking they were both double and fixed the code accordingly rn I have to rewire the pneumatic controller

Pneumatics are working flawlessly now

1 Like