Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 3개 있습니다.)

New NodeMCU v3 아두이노 호환 보드의 내장 LED 및 입력 핀 사용법

지난번에 살펴 본 NodeMCU 보드가,

New NodeMCU v3 아두이노 호환 보드의 기본 개발 환경 구성
; https://www.sysnet.pe.kr/2/0/11595

아두이노 호환이라면서 꽤 특이한 구성을 가지고 있는데, 우선 보드의 PIN 구성이 다음과 같습니다.

좌측        우측

A0          D0 (GPIO 16)
G           D1 (GPIO 5)
VU          D2 (GPIO 4) - 미신호 3.3V
S3          D3 (GPIO 0) - 미신호 3.3V
S2          D4 (builtin led, GPIO 2) - 미신호 3.3V
S1          3V3
SC          G 
S0          D5 (GPIO 14)
SK          D6 (GPIO 12)
G           D7 (GPIO 13)
3V          D8 (GPIO 15)
EN          RX 
RST         TX
G           G 
VIN         3V3

ESP8266은 총 17개의 GPIO를 제공한다고 하는데, 다음의 자료를 보면,

The ESP8266 as a microcontroller - Hardware
; https://tttapa.github.io/ESP8266/Chap04%20-%20Microcontroller.html

비어 있는 GPIO 값들이 아래와 같이 이미 할당되어 있어 보드의 핀으로는 제공되지 않는 것입니다.

GPIO 1: TX of the hardware Serial port (UART)
GPIO 3: RX of the hardware Serial port (UART)
GPIO 6 ~ 11: To connect the flash memory chip

GPIO 2번의 내장 LED는 특이하게 HIGH인 경우 점멸하고, LOW인 경우 점등합니다. HIGH인 경우 D4 핀에서 3.3V가, LOW인 경우 0V가 찍히는 걸로 봐서는 기기 고장은 아닌듯 하고, "The ESP8266 as a microcontroller - Hardware" 문서에 따라,

Note: you don’t have to add an external pull-up resistor to GPIO2, the internal one is enabled at boot.

풀업 방식으로 활성화되어 있기 때문에, 신호를 주지 않은 상태가 HIGH이고 신호를 준 상태가 LOW로 바뀌므로 그렇게 동작하는 것 같습니다.

문서를 보면, 아두이노와 동일하게 GPIO 0~15번의 핀들은 모두 내장 풀업 레지스터가 있다고 합니다. 플로팅 상태가 예측 불가능한 면이 있기 때문에 외부 풀업 레지스터를 설정하지 않을 거라면 프로그램에서 미리 INPUT_PULLUP으로 초기화하는 것이 좋습니다.

INPUT_PULLUP으로 초기화된 핀들은 3.3V 전압이 걸려 있습니다. 따라서, 외부에서 스위치 등을 연결해 신호를 ON 시킨다는 것은 전압이 0V로 내려가는 것을 의미합니다. 일례로, D2 데이터 선에 스위치를 연결하고, 이 스위치가 눌리면 내부 LED를 ON하고 누르지 않으면 LED를 끄도록 하고 싶다면 다음과 같은 식으로 배선을 하면 됩니다.

node_mcu_pins_1.png

그리고 아두이노 스케치는 다음과 같이 작성할 수 있습니다.

const int buttonPin = 4; // GPIO4 - D2
const int ledPin = 2;    // GPIO2 - D4 - builtin led

int buttonState = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void LedOn() {
    digitalWrite(ledPin, LOW); // 내장 LED는 LOW 신호일 때 점등
}

void LedOff() {
    digitalWrite(ledPin, HIGH);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) { // 스위치가 열렸으면,
    LedOff();
  } else { // 스위치가 닫혔으면,             
    LedOn();
  }

  delay(1000);
}

처음 시작하면, 풀업 상태의 D2 핀에 HIGH 3.3V 전압이 걸리므로 LedOff 함수가 실행, 내장 LED가 꺼진 상태입니다. 이후, 스위치를 눌러 D2가 GND에 연결되면 내부 풀업 상태의 전압이 GND로 빠지면서 D2는 LOW 상태로 되고 따라서 LedOn 함수가 실행됩니다.




문서에 의하면,

GPIO15 is always pulled low, so you can’t use the internal pull-up resistor. You have to keep this in mind when using GPIO15 as an input to read a switch or connect it to a device with an open-collector (or open-drain) output, like I²C.


GPIP15 번(D8) 핀은 INPUT_PULLUP 옵션에 상관없이 LOW 상태로 시작합니다. 따라서, D8 핀을 스위치로 제어하고 싶다면 눌렸을 때 3.3V 전압을 인가해야 합니다. 따라서 이번에는 다음과 같이 회로를 구성하고,

node_mcu_pins_2.png

이를 이용해 내장 LED를 On/Off하고 싶다면 이번에는 반대로 프로그램을 해야 합니다.

const int buttonPin = 15; // GPIO 15 - D8
const int ledPin = 2;    // GPIO 2 - D4 - builtin led

int buttonState = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  // pinMode(buttonPin, INPUT_PULLUP); // pullup 지정을 해도 pulldown으로 동작
  pinMode(buttonPin, INPUT);
}

void LedOn() {
    digitalWrite(ledPin, LOW);
}

void LedOff() {
    digitalWrite(ledPin, HIGH);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) { // 스위치가 닫히면,
    LedOn();
  } else { // 스위치가 열리면,             
    LedOff();
  }

  delay(1000);
}

GPIO 15 번(D8) 핀의 경우 기본적으로 PullDown 기능이 활성화되었다고 볼 수 있는데, 문서에는 GPIO 16 번(D0) 핀에 대해서도 다음과 같이 설명하고 있습니다.

GPIO16 has a built-in pull-down resistor.

To enable the pull-down resistor for GPIO16, you have to use INPUT_PULLDOWN_16.

즉, GPIO 16 번(D0) 핀은 Pull-down 레지스터를 내장하긴 하지만 기본 활성화는 안 되어 있는 것입니다. 따라서 이것을 활성화하려면 명시적으로 다음과 같이 INPUT_PULLDOWN_16을 지정해야 합니다.

pinMode(16, INPUT_PULLDOWN_16);

이후, 사용 방법은 GPIO 15번(D8) 핀과 같습니다.

(첨부 파일은 이 글의 다이어그램 파일 2개와 "A-Beginner's-Guide-to-the-ESP8266.pdf" 매뉴얼을 포함합니다.)




이 글에 사용된 아두이노 회로도 다이어그램은 다음의 프로그램을 이용한 것입니다.

[Arduino] 간편한 회로도 그리기 툴, Fritzing
; http://kaizen8501.tistory.com/2

기본 부품 다이어그램에 NodeMCU는 포함하고 있지 않는데 검색해 보면 다음과 같이 공개되어 있습니다.

NodeMCU V1.0  
; https://github.com/squix78/esp8266-fritzing-parts/tree/master/nodemcu-v1.0

nodemcu-v3-fritzing
; https://github.com/roman-minyaylov/fritzing-parts/tree/master/esp8266-nodemcu-v3




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 5/5/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2021-05-31 09시56분
[기초 지식] 아두이노와 스위치 사용시 주의할 점(디바운싱이란?)
; https://blog.naver.com/jamduino/221096134736
정성태

... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
12206정성태4/2/202011232오류 유형: 613. 파일 잠금이 바로 안 풀린다면? - The process cannot access the file '...' because it is being used by another process.
12205정성태4/2/20208634스크립트: 18. Powershell에서는 cmd.exe의 명령어를 지원하진 않습니다.
12204정성태4/1/20208438스크립트: 17. Powershell 명령어에 ';' (semi-colon) 문자가 포함된 경우
12203정성태3/18/202010471오류 유형: 612. warning: 'C:\ProgramData/Git/config' has a dubious owner: '...'.
12202정성태3/18/202013076개발 환경 구성: 486. .NET Framework 프로젝트를 위한 GitLab CI/CD Runner 구성
12201정성태3/18/202010885오류 유형: 611. git-credential-manager.exe: Using credentials for username "Personal Access Token". [1]
12200정성태3/18/202011327VS.NET IDE: 145. NuGet + Github 라이브러리 디버깅 관련 옵션 3가지 - "Enable Just My Code" / "Enable Source Link support" / "Suppress JIT optimization on module load (Managed only)"
12199정성태3/17/20209176오류 유형: 610. C# - CodeDomProvider 사용 시 Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path '...\f2_6uod0.tmp'.
12198정성태3/17/202011917오류 유형: 609. SQL 서버 접속 시 "Cannot open user default database. Login failed."
12197정성태3/17/202011057VS.NET IDE: 144. .NET Core 콘솔 응용 프로그램을 배포(publish) 시 docker image 자동 생성 - 두 번째 이야기 [1]
12196정성태3/17/20208993오류 유형: 608. The ServicedComponent being invoked is not correctly configured (Use regsvcs to re-register).
12195정성태3/16/202010703.NET Framework: 902. C# - 프로세스의 모든 핸들을 열람 - 세 번째 이야기
12194정성태3/16/202013024오류 유형: 607. PostgreSQL - Npgsql.NpgsqlException: sorry, too many clients already
12193정성태3/16/20209682개발 환경 구성: 485. docker - SAP Adaptive Server Enterprise 컨테이너 실행 [1]
12192정성태3/14/202012138개발 환경 구성: 484. docker - Sybase Anywhere 16 컨테이너 실행
12191정성태3/14/202012522개발 환경 구성: 483. docker - OracleXE 컨테이너 실행 [1]
12190정성태3/14/20208645오류 유형: 606. Docker Desktop 업그레이드 시 "The process cannot access the file 'C:\Program Files\Docker\Docker\resources\dockerd.exe' because it is being used by another process."
12189정성태3/13/202013472개발 환경 구성: 482. Facebook OAuth 처리 시 상태 정보 전달 방법과 "유효한 OAuth 리디렉션 URI" 설정 규칙
12188정성태3/13/202015785Windows: 169. 부팅 시점에 실행되는 chkdsk 결과를 확인하는 방법
12187정성태3/12/20208462오류 유형: 605. NtpClient was unable to set a manual peer to use as a time source because of duplicate error on '...'.
12186정성태3/12/20209536오류 유형: 604. The SysVol Permissions for one or more GPOs on this domain controller and not in sync with the permissions for the GPOs on the Baseline domain controller.
12185정성태3/11/202010229오류 유형: 603. The browser service was unable to retrieve a list of servers from the browser master...
12184정성태3/11/202011638오류 유형: 602. Automatic certificate enrollment for local system failed (0x800706ba) The RPC server is unavailable. [3]
12183정성태3/11/202010005오류 유형: 601. Warning: DsGetDcName returned information for \\[...], when we were trying to reach [...].
12182정성태3/11/202011211.NET Framework: 901. C# Windows Forms - Vista/7 이후의 Progress Bar 업데이트가 느린 문제파일 다운로드1
12181정성태3/11/202011995기타: 76. 재현 가능한 최소한의 예제 프로젝트란? - 두 번째 예제파일 다운로드1
... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...