아두이노 나노 ESP32 - 코드 구조 | Arduino Nano ESP32 - Code Structure

ESP32를 프로그래밍하는 방법을 배우기 위해서는 아두이노 나노 ESP32 코드의 구조를 배워야 합니다. 이 튜토리얼은 아두이노 나노 ESP32 코드의 구조를 제공하고 설명합니다.

준비물

1×Arduino Nano ESP32 Amazon
1×USB Cable Type-C 쿠팡 | Amazon
1×(Optional) DC Power Jack 쿠팡 | Amazon
1×Breadboard 쿠팡 | Amazon
1×Jumper Wires Amazon
1×(Recommended) Screw Terminal Adapter for Arduino Nano 쿠팡 | Amazon
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

기본 코드 구조

아두이노 나노 ESP32 코드(또한 아두이노 나노 ESP32 스케치라고도 함)는 아두이노 코드와 동일한 구조를 가지고 있습니다. 이것은 주로 두 부분으로 구성되어 있습니다: 설정 코드와 루프 코드입니다.

코드 설정

  • setup() 함수 안에 있는 코드를 세팅 코드라고 합니다.
  • 세팅 코드는 전원이 켜지거나 리셋 된 직후에 실행됩니다.
  • 세팅 코드는 오직 한 번만 실행됩니다.
  • 세팅 코드는 변수 초기화, 핀 모드 설정, 라이브러리 사용 시작 등에 사용됩니다.

루프 코드

  • Loop 코드는 loop() 함수 안에 있는 코드입니다.
  • Loop 코드는 설정 코드 바로 다음에 실행됩니다.
  • Loop 코드는 반복적으로(무한히) 실행됩니다.
  • Loop 코드는 응용 프로그램의 주요 작업을 수행하는 데 사용됩니다.

예시

void setup() { // 여기에 설정 코드를 넣으세요. 한 번만 실행됩니다: Serial.begin(9600); Serial.println("This is Arduino Nano ESP32 setup code"); } void loop() { // 여기에 메인 코드를 넣으세요. 반복적으로 실행됩니다: Serial.println("This is Arduino Nano ESP32 loop code"); delay(1000); }

사용 방법

  • 이것이 당신이 아두이노 나노 ESP32를 처음 사용하는 경우라면, 아두이노 나노 ESP32를 위한 환경 설정 방법을 참조하세요.
  • 위의 코드를 복사하여 아두이노 IDE에 붙여넣으세요.
  • 아두이노 IDE에서 Upload 버튼을 클릭하여 아두이노 나노 ESP32 보드에 코드를 컴파일하고 업로드하세요.
  • 아두이노 IDE에서 시리얼 모니터를 엽니다.
How to open serial monitor on Arduino IDE

시리얼 모니터에서 출력을 확인하세요.

COM6
Send
This is Arduino Nano ESP32 setup code This is Arduino Nano ESP32 loop code This is Arduino Nano ESP32 loop code This is Arduino Nano ESP32 loop code This is Arduino Nano ESP32 loop code This is Arduino Nano ESP32 loop code This is Arduino Nano ESP32 loop code This is Arduino Nano ESP32 loop code
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

시리얼 모니터에서 볼 수 있듯이, “This is Arduino Nano ESP32 the setup code”는 한 번 출력되지만, “This is Arduino Nano ESP32 loop code”는 여러 번 출력됩니다. 이는 아두이노 나노 ESP32 설정 코드가 한 번 실행되고, 아두이노 나노 ESP32 루프 코드가 반복적으로 실행된다는 것을 의미합니다. 설정 코드가 먼저 실행됩니다.

※ NOTE THAT:

setup() 함수와 loop() 함수는 Arduino Nano ESP32 코드에서 반드시 사용되어야 합니다. 그렇지 않으면 오류가 발생합니다.

기타 부품들

Arduino Nano ESP32 스케치에는 설정 및 루프 코드 외에 다음과 같은 부분들이 포함될 수 있습니다:

  • Block comment: usually used to write some information about the author, the wiring instruction, the license ... Arduino Nano ESP32 will ignore this part.
  • Libraries inclusion: is used to include libraries into the sketch.
  • Constant definition: used to define constant
  • Global variables declaration

예를 들어:

/* * 이 Arduino Nano ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino Nano ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-nano-esp32/arduino-nano-esp32-code-structure */ #include <Servo.h> #include <LiquidCrystal.h> #define MAX_COUNT 180 Servo servo; LiquidCrystal lcd(3, 4, 5, 6, 7, 8); int loop_count = 0; void setup() { Serial.begin(9600); lcd.begin(16, 2); servo.attach(9); Serial.println("Arduino Nano ESP32 설정 코드입니다"); } void loop() { loop_count++; Serial.print("Arduino Nano ESP32 루프 코드, 카운트: "); Serial.println(loop_count); lcd.print("Hello World!"); servo.write(loop_count); if(loop_count >= MAX_COUNT) loop_count = 0; delay(1000); }

사용 방법

  • 아두이노 나노 ESP32를 처음 사용하는 경우, 아두이노 IDE에서 아두이노 나노 ESP32 환경 설정하는 방법을 확인하세요.
  • 위의 코드를 복사하여 아두이노 IDE에 붙여넣으세요.
  • 아두이노 IDE에서 Upload 버튼을 클릭하여 아두이노 나노 ESP32 보드에 코드를 컴파일하고 업로드하세요.
  • 아두이노 IDE에서 시리얼 모니터를 열어보세요.
How to open serial monitor on Arduino IDE

시리얼 모니터에서 출력을 확인하세요.

COM6
Send
This is Arduino Nano ESP32 setup code This is Arduino Nano ESP32 loop code, count: 1 This is Arduino Nano ESP32 loop code, count: 2 This is Arduino Nano ESP32 loop code, count: 3 This is Arduino Nano ESP32 loop code, count: 4 This is Arduino Nano ESP32 loop code, count: 5 This is Arduino Nano ESP32 loop code, count: 6 This is Arduino Nano ESP32 loop code, count: 7
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

지금 우리는 코드를 줄마다 이해할 필요가 없습니다. 우리는 코드 구조에 대해 알아야 합니다. 줄마다 있는 코드는 다음 튜토리얼에서 설명될 것입니다.

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!