简体   繁体   English

带有 Raspberry Pi Pico 的 SPDT 开关

[英]SPDT Switches with a Raspberry Pi Pico

Basically I'm just trying to figure out how an spdt switch would work with a raspberry pi pico.基本上我只是想弄清楚 spdt 开关如何与 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.当我搜索有关如何将开关与 pico 接口的信息时,我得到的只是有关按钮开关的信息。

Does the spdt switch act like a closed circuit, constantly providing power to the pin I connect it to? spdt 开关是否像闭合电路一样,不断为我连接的引脚供电?

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?假设以上问题的答案是“是”,我 go 如何告诉 micropython 根据接收电源的引脚做不同的事情?

Sorry for the simple questions, I have no experience in the domain of microcontrollers and can't find this info anywhere.对于简单的问题,我很抱歉,我没有微控制器领域的经验,也无法在任何地方找到此信息。

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.如果开关的中间引脚连接到 GPIO 2,两个外部引脚连接到地和 Vcc,则可以使用开关在逻辑 0 和逻辑 1 之间切换 GPIO 的值。

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.我建议阅读一些基本的电子教程(或观看一些视频)——即使它们是针对 Arduino 或其他东西而不是 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.您将遇到“浮动”输入的问题——未连接到任何东西的 GPIO 输入往往会在逻辑 0 和 1 之间摆动。您可以通过使用连接输入的上拉或下拉电阻来解决这个问题到一个特定的逻辑电平,当输入断开时,该逻辑电平将提供一个稳定的值。

Pull-up/down resistors can be wired in manually, but many microcontrollers (the Pico included) provide built-in pull-up or pull-down resistors.可以手动连接上拉/下拉电阻,但许多微控制器(包括 Pico)提供内置上拉或下拉电阻。 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. Pico 似乎同时提供了这两种功能,您可以在创建新Pin object 时通过提供Pin.PULL_UPPin.PULL_DOWN标志来激活它们。

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.我们在这里使用Pin.PULL_DOWN是因为当引脚未连接时我们想要一个稳定的 0 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM