资讯详情

Arduino 学习笔记_2 输入输出&逻辑控制

输入输出

LED light emitting diode

长脚端应流向短脚端,产生2v左右压降,LED工作电流20mA左右。

在这里插入图片描述

加压降电阻后,LED不会短路,可以工作。

连接时,LED长脚接引脚,短脚接GND接地。

在 pinMode() 为 OUTPUT 模式时,digitalWrite() 数字引脚可以写 HIGH 或 LOW,代表5电平和0电平。

面包板

没有面包板的时候,人们用绕线布置电路,很乱。

后来,有人开始用切面包板焊接元件。

现在的面包板出现在迭代中。

面包板中间的同一列相连,因此图中电阻的右端与蓝色导线相连。

面包板的上下两行是电源轨道,其中同一行的孔分为左右两半,相互连接。一般直流电源,蓝线负极,红线正极。如果中间部分需要电流,则与上下部分连接。

按键开关

pinMode() 调整输入模式 INPUT,引脚处于高阻抗状态,可用于读取传感器信号或开关信号。

R1是10kΩ上拉电阻。不按开关时,引脚2为 HIGH,按下时为 LOW。

如果引脚没有设置任何电路,则引脚悬挂(FLOATING)。

代码:示例 Basic 中的 DigitalReadSerial

/* DigitalReadSerial Reads a digital input on pin 2, prints the result to the Serial Monitor This example code is in the public domain. https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial */  // digital pin 2 has a pushbutton attached to it. Give it a name: int pushButton = 2;  // the setup routine runs once when you press reset: void setup() { 
           // initialize serial communication at 9600 bits per second:   Serial.begin(9600);//串口通信初始化,9600位/s。Serial 是串口工具库,begin()是函数   // make the pushbutton's pin an input:   pinMode(pushButton, INPUT); }  // the loop routine runs over and over again forever: void loop() { 
           // read the input pin:   int buttonState = digitalRead(pushButton);///读取输入引脚的值,返回 HIGH 或 LOW,赋给 buttonState   // print out the state of the button:   Serial.println(buttonState);   delay(1);        // delay in between reads for stability,确保程序短暂停止 } 

不按按按钮时一直输出1,按下后一直输出0,:HIGH=1,LOW=0。

逻辑控制

pinMode() 除了INPUT OUTPUT 输入上拉模式有两种模式 INPUT_PULLUP。在上拉模式下输入 Arduino 内部有上拉电阻,不需要连接上拉电阻。

本次使用的示例名为: DigitalInputPullUp.

实验目的:按钮控制Arduino 开发板13号引脚旁边 LED 灯的亮灭。

/* Input Pull-up Serial This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital input on pin 2 and prints the results to the Serial Monitor. The circuit: - momentary switch attached from pin 2 to ground - built-in LED on pin 13 Unlike pinMode(INPUT), there is no pull-down resistor necessary. An iternal 20K-ohm resistor is pulled to 5V. This configuration causes the input to read HIGH when the switch is open, and LOW when it is closed. created 14 Mar 2012 by Scott Fitzgerald This example code is in the public domain. https://www.arduino.cc/en/Tutorial/BuiltInExamples/InputPullupSerial */

void setup() { 
        
  //start serial connection
  Serial.begin(9600);
  //configure pin 2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);

}

void loop() { 
        
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  //print out the value of the pushbutton
  Serial.println(sensorVal);

  // Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
  // HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
  // button's pressed, and off when it's not:
  if (sensorVal == HIGH) { 
        
    digitalWrite(13, LOW);
  } else { 
        
    digitalWrite(13, HIGH);
  }
}

就是说:如果7输入为高电平,13输出低电平;如果7输入低电平,13输出高电平。

Arduino 板上的L灯当13输出高电平的时候会亮,否则会灭,可以作为判断标准。

(我真服了有根线坏了导致我做if判断的时候试了3个例子都不行,最后客服教我排查方法QAQ)

如果按钮和引脚、GND相连,按下时引脚输入为0,否则输入为1.因此也可以据此制作 && || ! 电路。

标签: 接地电阻cc2520

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台