2

I would like to read the output of an on-off-on switch with my Leonardo board, but I have very little knowledge of electronics and don't know how to wire it up.

This is the switch in question: E-Switch 200MSP4T1B1M1QE

The switch has three pins. Generic three-pin SPDT wiring diagrams I've found suggest that the two outer pins should be connected to separate lights and the middle pin should be connected to the power source. Those lights also have a ground connection.

I don't want to power anything in a literal sense, I only want to have the Leonardo read the output of the switch. The end goal is to have the two ON positions read as separate button presses (eventually using a joystick library, in case that changes how the switch needs to be wired).

My questions are:

  1. Is the above wiring example correct for my use case?
  2. Is the output of the switch considered digital or analog?
  3. Is a wire from the switch to GND not required?

One final note - I'm not using a breadboard, but connecting the wires directly from the switch to the board.

1

You talk about wiring a switch to control lights directly. Instead, you want to use your switch to send information to your Arduino.

You link to an SPDT (single pole, double throw) switch. When flipped one way, it connects the center pin to one of the outside pins, and when flipped the other way, it connects the center pin to the other outside pin.

If you want the Leonardo to read the state of the switch as 2 separate switches/button presses, wire the 2 outer pins of the switch to 2 separate digital inputs on the Arduino.

You need to force each input to either ground or +5V when it's not connected to anything, or it will "float" and you'll get semi-random high and low readings.

I suggest setting the Arduino input pins to INPUT_PULLUP mode. That connects the inputs internally through a resistor to +5V. If you don't connect anything to the pin, it reads as HIGH. When the button is pressed/switch is closed, it reads LOW. That might seem a little confusing, but it means you don't need external resistors.

You'd then attach those to pins to the two outer pins of your switch, and attach the center pin to ground (GND).

If you flip the switch one way, it would connect one outer pin to the center pin. That would connect that Arduino pin to ground, and the reading on the pin would drop from HIGH to LOW. Flip the switch the other way and it would connect the other pin to ground, at that other pin would drop from HIGH to LOW.

It is also possible to wire your switches using INPUT mode, not INPUT_PULLUP, and have them read LOW when the switch is open, and HIGH when closed, but to do that you'll need external pull-down resistors. (And wiring it will be a little painful without a breadboard or PC board.)

To answer your questions:

  1. Is the above wiring example correct for my use case?

You don't show a wiring example. You talk about how to wire a switch to switch power to a pair of lights directly, without using a microcontroller. That's not relevant here.

  1. Is the output of the switch considered digital or analog?

It would make sense to use it as a digital input, since you need it to give you 2 states, open and closed.

  1. Is a wire from the switch to GND not required?

You need to connect the center pin to either ground +5V, as discussed above.

| improve this answer | |
  • See the diagram in Edgar's answer. A picture is worth a thousand words. – Duncan C Sep 18 at 19:31
  • This is exactly what I was looking for, thank you! I would say this is the better answer. Not that the diagrams were bad, necessarily, but for someone with little wiring/circuitry experience the diagrams only confused me more. – Boonswaddle Sep 18 at 23:36
  • If you feel this (or Edgar's) answer answers your question you should accept it. (Accepting the first/best answer that answers your question is expected on Stack Exchange sites.) – Duncan C Sep 19 at 14:52
3

I suggest this simple circuit:

schematic

simulate this circuit – Schematic created using CircuitLab

Set both inputs to INPUT_PULLUP. If the input reads LOW, that means it has been grounded by the switch.


Edit: Per Duncan’s request, here is the alternative circuit with the polarities inverted. With the switch’s common connected to Vcc, the inputs should be set to INPUT_PULLDOWN. However, most Arduinos do not support this mode, for lack of internal pull-downs. Then one has to use external pull-downs instead, and set the pins to INPUT.

schematic

simulate this circuit

With this circuit, a pin reads HIGH when it is connected to +5V by the switch.

| improve this answer | |
  • I didn't notice Duncan C posted an answer just while I was preparing mine. Both answers are the same: one with words, the other with an image. – Edgar Bonet Sep 18 at 19:29
  • I haven't used CircuitLab very much. A diagram is soooo much clearer. (voted.) Could you also show the alternate circuit with the switch connected to +5V, with pull-down resistors? Some newbies find LOW == switch on confusing. – Duncan C Sep 18 at 19:34
  • @DuncanC: OK, edited as per your request. – Edgar Bonet Sep 18 at 20:56
  • Why not save an input and use the != and == to determine which way the switch is positioned. less code less hardware. – Gil Sep 18 at 21:58
  • 1
    @Gil: I don't understand. Did you notice it's a 3-positions switch? Would you post an answer expanding on your idea? – Edgar Bonet Sep 18 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.