0

Basically I'm just trying to figure out how an spdt switch would work with a raspberry pi pico. When I search for information on how to interface a switch with a pico, all I get is info on button switches.

Does the spdt switch act like a closed circuit, constantly providing power to the pin I connect it to?

Assuming the answer to the question above is "yes", how would I go about telling micropython to do different things based on which pin is receiving power?

Sorry for the simple questions, I have no experience in the domain of microcontrollers and can't find this info anywhere.

tylerst
  • 45
  • 6

1 Answers1

2

A switch -- by itself -- doesn't provide power to anything. All it does is close and open a circuit. You can replace the switch with a wire and connect/disconnect the wire and accomplish the same thing.

If you had the middle pin of the switch connected to GPIO 2, and the two outer pins wire to Ground and Vcc, you could use the switch to switch the value of the GPIO between logic 0 and logic 1.

Reading the value would look something like this:

>>> from machine import Pin
>>> pin = Pin(2, Pin.IN)
>>> pin()
0

You can of course use the pin values in condition statements:

>>> if pin():
...    print('Switch is in position 1')
... else:
...    print('Switch is in position 2')
...
Switch is in position 1
>>>

I would suggest reading through some basic electronics tutorials (or watching some videos!) -- even if they're for Arduino or something rather than Micropython, many of the concepts are transferable.


For the configuration you've described in your comment...


GPIO2 ---o      <--- the switch is connecting this input to Vcc
          \
           \
            o--- Vcc


GPIO3 ---o       <--- this input may have an unstable value

You're going to have the problem of "floating" inputs -- GPIO inputs that aren't connected to anything will tend to wobble between logic 0 and 1. You solve this by using pull up or pull down resistors, which connect your input to a specific logic level that will provide a stable value when the input is otherwise disconnected.

Pull-up/down resistors can be wired in manually, but many microcontrollers (the Pico included) provide built-in pull-up or pull-down resistors. The Pico appears to provide both, and you can activate them by providing the Pin.PULL_UP or Pin.PULL_DOWN flags when creating a new Pin object.

That means you need to write something like:

>>> pinA = Pin(2, Pin.PULL_DOWN)
>>> pinB = Pin(3, Pin.PULL_DOWN)
>>> if PinA():
...     print("Pin A is connected")
... elif PinB():
...     print("Pin B is connected")
... else:
...     print("Neither pin is connected")
...

We're using Pin.PULL_DOWN here because we want a stable value of 0 when the pin is not connected.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Thanks! Like I said, I looked around and couldn't find much, but I'll keep looking (I can't imagine it isn't out there somewhere). – tylerst Feb 23 '22 at 04:58
  • I'm trying to use a switch to activate one of two pins on the pico. Basically I'm trying to make a simple circuit where I can use switches to choose which of 4 LED's are activated. I currently have it set up so that the center pin on the switch is receiving power, and the outer two pins on the switch are going to two different input pins on the pico, and I'm trying to figure out how to make micropython look for which pin is receiving power through the switch. Is this an incorrect setup for switches, or can I make this work? – tylerst Feb 23 '22 at 05:02
  • I've updated the answer to respond to y our comment. – larsks Feb 23 '22 at 12:37
  • This is a comprehensive answer to the question but it should be pointed out that if you want to know whether a switch is in one of two states, you only need one GPIO input: effectively, you can ignore one pin of the switch and just treat it the same as a pushbutton - if the switch is in one position it is on and in the other position it is off. If you had a 3-position switch where the middle position connects neither of the two outputs to the common, then the configuration in this answer would be the correct one. – nekomatic Mar 06 '23 at 11:18