Blink 예제 - 아두이노 시작하기

 아두이노 IDE 설정과 아두이노 Uno 보드를 연결한 후 아두이노 Uno 동작이 제대로 되는지 아두이노 Uno 보드에 있는 LED를 켜는 예제(Blink) 를 실행해 보겠습니다.


아두이노 IDE 실행

먼저 아두이노 IDE를 실행합니다.

아두이노 IDE 실행

아두이노 IDE 메뉴에서 파일 > 예제 > 01.Basics > Blink를 선택합니다.

Blink 예제 불러오기



아래는 Blink 예제를 불러온 화면입니다.


Blink 예제 살펴보기

아두이노 IDE 왼쪽 부분에 숫자들이 표시되는데 이 숫자들은 스케치 코드의 줄 번호입니다.

코드를 살펴보면, 1번 ~ 23줄은 주석입니다. 주석은 실행되는 코드가 아니라, 코드 설명을 위한 내용을 기록하는 것입니다. 한 두달이 지난 후에 코드를 보면 왜 이렇게 코드를 만들었는지 생각나지 않을 때가 있습니다. 그런 경우를 위해서 주석을 이용해서 코드를 설명할 때 이용합니다.

26번 줄에 setup 함수가 있습니다. 코드 실행은 위에서 아래로 실행이 되는데 setup 함수는 한번만 실행이 됩니다. 포트 설정(Input, Output)이나 시리얼 설정 등 한번만 실행해야 하는 함수를 setup에 넣으면 됩니다.

32번 줄에 loop 함수는 반복할 코드를 넣으면 됩니다. Blink 예제에서는 LED가 연결된 포트를 1초 주기로 high와 low를 써서 LED를 on/off하는 코드입니다.

Blink 예제 코드


/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

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

Blink 예제 업로드 하기

아두이노 IDE 아이콘 바에서 2번째 업로드 아이콘을 누르면 코드를 컴파일하고 업로드합니다.

업로드 완료

아두이노 보드에 있는 LED가 1초 주기로 on/off 되는 것을 볼 수 있습니다.

Blink 예제를 업로드해서 아두이노 IDE 설정, 보드 선택, PC와 아두이노 연결을 확인할 수 있습니다.