Limit Switches 2020?

My team is currently trying to connect limit switches to the Robo rio. We have two wires connecting from the limit switches to the DIO port on the Rio, but so far they are not working. We have determined it is most likely a coding problem but we have a hard time finding an updated firmware version for the limit switch. Any suggestions will be greatly appreciated. Thank you

1 Like

Can you post your code to a platform like GitHub? I highly doubt it’s a firmware version issue. My team got limit switches working just fine.

Sure! how did your team connect the limit switches to Rio and can you upload your code to Github?

So when connecting to the DIO ports, they pull up by default so you should connect the limit switch to the ground one and signal one and leave the one that puts out 5V (I think) disconnected.

When the circuit is closed, it will read 0 and when the circuit is open it will read 1. It’s counter intuitive if you are using normally open.

1 Like

When you leave the 5v disconnected are your sensors getting power from elsewhere?

So limit switches don’t really need power to work. The roboRio just has to detect if a circuit is closed or not. The Rio uses a pull up resistor. I don’t know exactly how pull up resistors work, but basically unless you pull the signal back down to ground, it will read a high value. When you ground it, it will read a low value.

Thank you! This clears up some confusion about the hardware aspects, if you have any suggestions about the code for the limit switches we would be appreciative.

While coding limit switches, I recommend using abstraction by creating methods like isLimitPressed() and isLimitClosed(). Your isLimitClosed() method would return value == 0. Now, the only place you would call this method is in your isLimitPressed(). If you are using normally open, it would return isLimitClosed() == true. If it’s normally closed, it would be isLimitClosed() == false.

I think that by doing this, it will be easier to understand your code. Also, unless you know what you’re doing, enable motor safety so if you stop commanding a motor, it will stop.

This is great we are trying it now!

Ok that was already in the code, we had and abstraction method that would return a Boolean and we tested that Boolean by using a print line however it always returned false thus we thought is was a wiring issue

You may want to look at the WPILIB pages for Digital Inputs. There are some examples there.
Hardware
Software

1 Like

This may not be OP’s issue, but a good place to describe how pull up resistors work for DIO (Digital Input/Output).

  • The pull-up resistor is connected between the positive voltage and the signal on the RIO, Arduinos, and many other systems. (I’m not aware of any that do it differently.) This resistor is specified so that it is small enough that it if nothing is plugged in (or a limit switch is open which is really the same thing), it will allow enough current to flow to “pull” the signal input to positive (1/true).
  • The resistor is also specified to be large enough that if the signal pin is shorted to the ground pin (e.g. a limit switch between these two pins is closed), the resistor will not draw more current than the system can safely supply on the positive line, nor sink on the ground line, nor enough to burn out the resistor*. As the signal pin is directly connected to ground in the closed switch case, the return value is ground (0/false).

* Common pull up resistor values are at least 5kΩ , up to at least 20kΩ. For a 5V difference between the high and low voltage, this means that when the switch is closed, the current draw will be no more than 1 mA, and power dissipation no more than 5mW.

2 Likes