Arduino LED 灯闪烁

Arduino 最简单的示例就是 LED 灯闪烁了,相当于编程界的 Hello world。这里需要一个面包板,一个 1K 欧的电阻,一个 LED 发光二极管和两根杜邦线。

TIM图片20191026124731

根据图片所示连线,需要注意的是 LED 二极管长引脚接正极,短引脚接负极。连接好之后在 Arduino IDE 中写入如下代码:

/*
  Blink
*/
const int Pin = 6;
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin as an output.
  pinMode(Pin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(Pin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(Pin, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

在工具菜单栏选择相应的开发板和端口,点击上传,一切正常的话 LED 灯就会开始闪烁。