在里面previous tutorial, we discussed digital output from Arduino to drive an LED. A controller can interface and interact with other devices in five ways: digital output, digital input, analog output, analog input, and serial communication.
在本教程中,我们将使用Arduino的数字输入来读取触觉开关的状态 - 使用它打开/关闭LED。
开关
开关are electrical devices designed to interrupt the flow of electricity in a circuit. A switch can make or break an electrical connection. In a circuit, it’s connected between two points at different potentials. At one point there’s a load that is ready to consume the power (current) and at the other point is a source that’s ready to supply the power (current).
开关可以完全打开或完全关闭。在完全条件下,它会在源和负载之间创建一个封闭的路径,从而使负载可以从源消耗功率(电流)。在完全关闭的条件下,它可以在负载和源之间的路径上开路,因此负载不消耗电源。
开关主要用于将ON/OFF电源打开到电路或部分电路的一部分。从本质上讲,任何电气或电子设备都有开关以打开/关闭设备。这样的开关可能是机械的或电子的。用于开关目的的开关是一种锁定类型,一旦切换,它们就会保持在全面或关闭状态。
Additionally, switches can be used as input devices and to supply digital logic for a controller or processor channel. Such switches are considered a momentary type. They remain in a fully ON state unless they are pressed or activated, and then they switch back to fully OFF state as they are released or deactivated.
开关类型
如前所述,开关可以是机械的或电子的。通过按下,移动或触摸其触点来激活机械开关。可以通过开关切换的电路(负载)的数量称为“极点”。负载可以切换到使用开关的状态数称为“投掷”。
Mechanical switches are classified in several ways, including:
- The number of contacts (single-contact or multi-contact)
- The number of poles and throws (SPST, SPDT, DPST, DPDT)
- 驱动,建筑和状态的方法(锁定或暂时)。
机械开关包括:
1.单极单掷(SPST)
2.单极双掷(SPDT)
3.Double Pole Single Throw (DPDT)
4.Double Pole Double Throw (DPDT)
5。按钮
6。拨动开关
7。浮球开关
8。Limit Switch
9。旋转开关
10。压力开关
11.Flow Switch
12.操纵杆开关
13.Temperature Switch
电子开关是活跃的组件,可以通过控制其端子之间的电流流来作为开关操作。这些组件通常用作单向开关。
但是,也可以通过在适当电路中使用多个开关组件来设计双向开关。
These active components can operate as a switch:
1.功率二极管/信号二极管
2.双极晶体管
3.MOSFET
4.IGBT
5。可控硅
6。TRIAC
7。diac
8。闸门关闭晶闸管
数据输入
对于数字/逻辑输入,使用瞬时类型开关,例如按钮。控制器或处理器在适当时使用它评估开关的状态(按下和释放时)。
根据该程序,按下(或发布)的事件可以解释为“数据输入”。
Pull-up and pull-down
当将Arduino的通道配置为“输入”时,它仍然处于高阻抗状态。这意味着它对外部源的需求极低,如果没有连接(称为“浮动”),则开关状态无法预测。
If the switch is directly interfaced to a current source or to the ground, it will remain in a floating state until the switch is pressed. So, the pin must be hard-wired to the opposite logic by default. This hard-wiring must be done via a resistor to protect the channels (pins) of the controller.
If a pin is hard-wired to ground, so that it can read a HIGH logic (in a positive logic system) via the switch, the resistor used is called a pull-down resistor. If a pin is hard-wired to the DC voltage, so that it can read a LOW logic (in a positive logic system) via the switch, the resistor used is called a pull-up resistor.
当引脚配置为Input_pullup时,它已连接到内部上拉电阻器,这意味着它必须通过开关连接到地面以读取逻辑低为输入。
当引脚配置为输入或Input_pullup时,如果施加的电压为负或大于电源导轨电压(5或3V),则PIN可能会损坏或破坏。
如果引脚分别通过带有外部上拉和下拉电阻器的开关连接到最高40 mA,则可以源或下沉。因此,无论哪种情况,都必须确保通过通道(引脚)的电流不得超过30或40 mA。
当配置为输入或INPUT_PULLUP销,a voltage greater than 3V and a voltage greater than 2V is read as logical HIGH in 5V and 3.3V boards respectively. A voltage less than 1.5V and a voltage less than 1V is read as logical LOW in 5V and 3.3V boards, respectively.
阅读数字输入
The AVR ports have true, read-modify-write functionality. Each pin consists of three registers (internal registers of AVR controller) bits: DDxn, PORTxn, and PINxn. The DDxn bit controls the direction of the pin.
当引脚配置为输出时,该位设置为逻辑上,当PIN配置为输入时,该位设置为逻辑零。当使用引脚用作输出时,如果将逻辑上的一个写在门户网站位上,则将引脚设置为高,如果将逻辑零写在门户网站位上,则引脚设置为低。
When a pin is used as input — and if logical one is written on the PORTxn bit — the internal pull-up is activated and if logical ZERO is written on the PORTxn bit, the internal pull-up resistor is switched off.
当PIN配置为输入时,输入逻辑将在PINXN位读取。PinxN寄存器位具有前面的闩锁,可作为同步器。当系统时钟较低时,该闩锁关闭,并且当系统时钟高时透明。
当系统时钟较低时,在PINXN寄存器位锁定了信号值。即使输入信号到达系统时钟的下降边缘,它也会在系统时钟的下一个下一个跌落边缘的PINXN寄存器位中读取。
This diagram is a timing diagram for reading an externally applied pin value:
它需要单个系统时钟脉冲来读取Arduino的AVR控制器的数字输入。考虑到Arduino是由16 MHz晶体计时的,因此读取输入需要62.5纳秒。结果,如果有条件地避免数据输入中的不良冗余,则始终评估开关的状态。
编程Arduino用于数字输入
For reading digital input from Arduino, the pin must be configured as INPUT or INPUT_PULLUP using the pinMode() function. For example, this statement configures pin 3 as the INPUT:
pinmode(3,输入);
如果将引脚配置为输入,则必须使用外部上拉或下拉电阻。当引脚使用内部上拉电阻时,也可以将其配置为Input_pullup。此语句将引脚3配置为Input_pullup:
pinMode(3, INPUT_PULLUP);
如果将引脚配置为Input_pullup,则必须通过开关将其连接到地面,以便从外部应用信号读取逻辑低。
可以使用DigitalWrite()函数读取PIN值。必须将读取值与低/0/false或高/1/true进行比较,以执行所需的后续操作。例如:
if(DigitalRead(3)==低)
{
digitalWrite(2,LOW);
}
按钮控制的LED食谱
In this recipe, we will switch ON and OFF an LED with a switch. The LED will remain ON by default. When a push button is pressed, the LED will switch OFF momentarily.
需要组件:
1.Arduino Uno X1
2.LED X1
3.触觉开关X1
4.330欧姆电阻x1
5。面包板X1
6。Male-to-Male Jumper Wires
电路连接
First, connect the digital I/O pin 2 of Arduino UNO with the anode of the LED. Then, connect the cathode of the LED with a series resistor of 330 Ohms and ground the other terminal of the resistor.
接下来,在Arduino Uno的引脚3处连接一个按钮,然后将按钮的另一个端子接地。直流电源电压和接地可以分别从5V功率引脚和Arduino Uno的地面引脚之一给予电路。
电路图
编程指南
To control the LED, pin 2 (where the LED is interfaced) must be configured as the digital output. The pin 3 (where the tactile switch is interfaced) must be configured as the digital INPUT_PULLUP.
这些引脚配置在setup()函数中设置如下:
void setup(){
Pinmode(2,输出);
pinMode(3, INPUT_PULLUP);
}
每当按下按钮时,LED都必须打开。当按钮配置为Input_pullup时,不使用外部上拉或下拉电阻,并且开关只能读取为逻辑低。
使用DigitalRead()函数读取开关的状态,并将其与逻辑低。如果较低,则意味着关闭LED - 通过使用DigitalWrite()函数将引脚2的输出设置为Low来完成。
但是,由于LED默认情况下应保持在上面,因此引脚2的输出设置为高度。该代码在loop()函数中写入如下:
void loop(){
if(DigitalRead(3)==低)
{
digitalWrite(2,LOW);
}
别的
{
DigitalWrite(2,高);
}
}
该项目
开关通过引脚3的内部上拉与Arduino连接,因此引脚可以从外部电路中读取为逻辑低。LED在电路中的Arduino处连接在PIN 2处,因此当电流被设置为高度时,它可以打开它。
还对Arduino进行编程,以便每当按下开关并在引脚3处读取逻辑低时,通过将引脚2设置为低点来关闭LED。引脚2设置为逻辑高的子句,因此默认情况下LED仍将打开。

OLYMPUS DIGITAL CAMERA
自己做
Rewrite the code of this recipe to toggle the state of the LED for pressing the push button. When the button is pressed once, the LED must switch OFF permanently. When the button is pressed again, the LED must switch ON again.
在下一个教程中,我们将讨论从Arduino生成模拟输出(PWM信号)的步骤。
演示视频:
Arduinosketch:
提交以下:Arduino,354manbetx
与本文有关的问题?
询问并讨论edaboard.com和Electro-Tech-online.com论坛。
告诉我们你的想法!!
You must be登录to post a comment.