You are on page 1of 27

2022 Fall, Embedded System (D.

Kim)

디지털입출력(2)

김동욱

Department of Automotive Engineering


Yeungnam University, Republic of Korea

Department of Automotive Engineering 1


2022 Fall, Embedded System (D. Kim)
디지털 입력

디지털 값

1 Bit 값 기호상수 전압 ON/OFF 제어

1 1 HIGH 5V ON

0 0 LOW GND OFF

5V 5V

0V 0V

Department of Automotive Engineering 2


2022 Fall, Embedded System (D. Kim)
디지털 입력

디지털 핀
0~13 : 디지털 입출력 핀
A0~A5 : 디지털 입출력 핀
기호상수 :
A0 14
A1 15
A2 16
0~13 A3 17
A4 18
A5 19
A0~A5 겸용 핀
0, 1 : 시리얼 UART 통신 RX/TX 핀 겸용
2, 3 : 외부 인터럽트 0/1핀 겸용
3, 5, 6, 9, 10, 11 : PWM 핀 겸용
A0, A1, A2, A3, A4, A5 : 아날로그 입력 겸용

Department of Automotive Engineering 3


2022 Fall, Embedded System (D. Kim)
디지털 입력

void setup() {

pinMode(핀 번호, INPUT); // 입력 모드 설정

}

void loop() {

변수 = digitalRead(핀 번호); // 디지털 신호 입력

}

Department of Automotive Engineering 4


2022 Fall, Embedded System (D. Kim)
2.1 디지털 입력 : Tact 스위치

 누름(Push) 스위치
• 누르면 : 단자 연결
• 떼면 : 단자 분리

 스위치 구성
• 4 단자 중 좁은 간격의 두 단자 사이
• 아래 그림의 A (또는 D)와 B (또는 C)

Department of Automotive Engineering 5


2022 Fall, Embedded System (D. Kim)
2.2 Switch 종류 : (0) 단순 연결

 5V와 디지털 입력단자 사이에 스위치 단순 연결

 스위치 동작
• PRESS → 5V 입력
• RELEASE → Floating 신호 입력 (:단락)

Department of Automotive Engineering 6


2022 Fall, Embedded System (D. Kim)
직류 회로 직병렬 회로

R1 R2

R1

R2

Department of Automotive Engineering 7


2022 Fall, Embedded System (D. Kim)
Input port  High-Impedance port

R R

s s
MCU MCU
( (

R R

s s
MCU MCU
( (

Department of Automotive Engineering 8


2022 Fall, Embedded System (D. Kim)
2.2 Switch 종류 : (1) Pulldown

 디지털 입력단자와 스위치 사이에 저항을 통한 GND 연결

 스위치의 반대편 단자는 5V 연결

 스위치 동작
 PRESS → 5V 입력
 RELEASE → 0V 입력

Department of Automotive Engineering 9


2022 Fall, Embedded System (D. Kim)
2.2 Switch 종류 : (2) Pullup

 디지털 입력단자와 스위치 사이에 저항을 통한 5V 연결

 스위치의 반대편 단자는 GND 연결

 스위치 동작
• PRESS → 0V 입력
• RELEASE → 5V 입력

Department of Automotive Engineering 10


2022 Fall, Embedded System (D. Kim)
2.2.1 디지털 입력 : Pulldown Switch

목표: 1. 디지털 입력 이해
2. Pulldown Switch 이해
3. pinMode() 이해
4. digitalRead() 이해

준비물:
1. Arduino Uno R3 board
2. Tact Switch, Resistor 10kΩ
3. LED, Resistor 220 Ω
3. Breadboard

+ 저항 220Ω 13
LED
- GND
아두이노
1 5V
Tact UNO
2 저항 10kΩ GND
Switch
2

Department of Automotive Engineering 11


2022 Fall, Embedded System (D. Kim)
2.2.1 Pulldown Switch 프로그램

const byte pinBtn = 2;


const byte pinLED = 13;

bool stateBtn = false;

void setup() {
pinMode(pinBtn, INPUT); 2번 핀 디지털 입력 지정
pinMode(pinLED, OUTPUT); 13번 핀 디지털 출력 지정
Serial.begin(9600);
}

void loop() {
stateBtn = (bool) digitalRead(pinBtn); 2번핀 스위치 상태를 읽어, 변수 stateBtn에 저장
Serial.println(stateBtn); stateBtn 값 시리얼 출력

if (stateBtn == HIGH) stateBtn이 HIGH라면,


digitalWrite(pinLED, HIGH); 13번핀의 LED를 점등
else stateBtn이 LOW라면,
digitalWrite(pinLED, LOW); 13번핀의 LED를 소등

delay(100); 0.1초간 시간 지연
}

Department of Automotive Engineering 12


2022 Fall, Embedded System (D. Kim)
2.2.2 디지털 입력 : Pullup Switch
목표: 1. 디지털 입력 이해
2. Pullup Switch 이해
3. pinMode() 이해
4. digitalRead() 이해

준비물:
1. Arduino Uno R3 board
2. Tact Switch, Resistor 10kΩ
3. LED, Resistor 220 Ω
3. Breadboard

+ 저항 220Ω 13
LED
- GND
아두이노
1 GND
Tact UNO
2 저항 10kΩ 5V
Switch
2

Department of Automotive Engineering 13


2022 Fall, Embedded System (D. Kim)
2.2.2 Pullup Switch 프로그램

const byte pinBtn = 2;


const byte pinLED = 13;

bool stateBtn = true;

void setup() {
pinMode(pinBtn, INPUT); 2번 핀 디지털 입력 지정
pinMode(pinLED, OUTPUT); 13번 핀 디지털 출력 지정
Serial.begin(9600);
}

void loop() {
stateBtn = (bool) digitalRead(pinBtn); 2번핀 스위치 상태를 읽어, 변수 stateBtn에 저장
Serial.println(stateBtn); stateBtn 값 시리얼 출력

if (stateBtn == HIGH) stateBtn이 HIGH라면,


digitalWrite(pinLED, HIGH); 13번핀의 LED를 점등
else stateBtn이 LOW라면,
digitalWrite(pinLED, LOW); 13번핀의 LED를 소등

delay(100); 0.1초간 시간 지연
}

※ Pulldown과 Pullup 프로그램은 stateBtn 초기화를 제외하고 동일하다.


단, 스위치를 누르거나 뗄때, ON/OFF의 결과가 서로 반대로 나타난다.
Department of Automotive Engineering 14
2022 Fall, Embedded System (D. Kim)
2.2.3 디지털 입력 : 내장 Pullup Switch
목표: 1. 디지털 입력 이해
2. 내장 Pullup Switch 이해
3. pinMode(핀 번호, INPUT_PULLUP) 이해
4. digitalRead() 이해

준비물:
1. Arduino Uno R3 board
2. Tact Switch
3. LED, Resistor 220 Ω
3. Breadboard

+ 저항 220Ω 13
LED
- GND 아두이노
Tact 1 GND UNO
Switch 2 2

Department of Automotive Engineering 15


2022 Fall, Embedded System (D. Kim)
2.2.3 내장 Pullup Switch 프로그램

const byte pinBtn = 2;


const byte pinLED = 13;

bool stateBtn = true;

void setup() {
pinMode(pinBtn, INPUT_PULLUP); 2번 핀 디지털 입력 지정 & 내장 PULLUP 사용
pinMode(pinLED, OUTPUT); 13번 핀 디지털 출력 지정
Serial.begin(9600);
}

void loop() {
stateBtn = (bool) digitalRead(pinBtn); 2번핀 스위치 상태를 읽어, 변수 stateBtn에 저장
Serial.println(stateBtn); stateBtn 값 시리얼 출력

if (stateBtn == HIGH) stateBtn이 HIGH라면,


digitalWrite(pinLED, HIGH); 13번핀의 LED를 점등
else stateBtn이 LOW라면,
digitalWrite(pinLED, LOW); 13번핀의 LED를 소등

delay(100); 0.1초간 시간 지연
}

※ Pullup과 내장 Pullup 의 프로그램 차이는 pinMode의 INPUT과 INPUT_PULLUP이다.


단, Pullup은 직접 회로 구성을 하여야 하고, 내장 Pullup은 별도의 회로 구성이 필요없다.
Department of Automotive Engineering 16
2022 Fall, Embedded System (D. Kim)
2.2.3 내장 Pullup Switch 프로그램

Pullup 스위치로 Pulldown 스위치처럼 눌렀을 때 켜지도록 하려면 : .

const byte pinBtn = 2;


const byte pinLED = 13;

bool stateBtn = false;

void setup() {
pinMode(pinBtn, INPUT_PULLUP); 2번 핀 디지털 입력 지정 & 내장 PULLUP 사용
pinMode(pinLED, OUTPUT); 13번 핀 디지털 출력 지정
Serial.begin(9600);
}

void loop() {
stateBtn = ! (bool) digitalRead(pinBtn); 2번핀 스위치 상태를 읽어, 반전시켜, 변수 stateBtn에 저장
Serial.println(stateBtn); stateBtn 값 시리얼 출력

if (stateBtn == HIGH) stateBtn이 HIGH라면,


digitalWrite(pinLED, HIGH); 13번핀의 LED를 점등
else stateBtn이 LOW라면,
digitalWrite(pinLED, LOW); 13번핀의 LED를 소등

delay(100); 0.1초간 시간 지연
}

Department of Automotive Engineering 17


2022 Fall, Embedded System (D. Kim)
예제) 디지털 입력 : Toggle 스위치

[예제2.3] 스위치 : 2번 핀, Pulldown


LED : 13번 핀
Toggle 스위치 구성 : LED 처음 OFF, 스위치 누르면 ON, 다시 누르면 OFF, ......

const byte pinBtn = 2;


const byte pinLED = 13;

bool stateBtn = false;


bool prevState = false;
bool toggleBtn = false;

void setup() {
pinMode(pinBtn, INPUT);
pinMode(pinLED, OUTPUT);
Serial.begin(9600);
}

void loop() {
stateBtn = (bool) digitalRead(pinBtn);
if (stateBtn) {
if (stateBtn != prevState) {
toggleBtn = !toggleBtn;
Serial.println(toggleBtn);
}
}
digitalWrite(pinLED, toggleBtn);
prevState = stateBtn;
delay(100);
}

Department of Automotive Engineering 18


2022 Fall, Embedded System (D. Kim)
예제) 디지털 입력 : 3 Btns & 3 LEDs

[예제2.4] 스위치 A : 빨강 LED, 스위치 B : 녹색 LED, 스위치 C : 노랑 LED


내장 Pullup 이용.
3개 버튼으로 3개 LED를 각각 제어, Toggle 스위치 이용.

const byte pinLEDs[3] = {13, 8, 7}; for(int i=0; i<3; i++) {


const byte pinBtns[3] = {2, 3, 4}; stateBtns[i] = (bool) !digitalRead(pinBtns[i]);
if (stateBtns[i]) {
bool stateBtns[3] = {false, false, false}; if (stateBtns[i] != prevStates[i]) {
bool prevStates[3] = {false, false, false}; toggleBtns[i] = !toggleBtns[i];
bool toggleBtns[3] = {false, false, false}; Serial.println(toggleBtns[i]);
}
for (int i=0; i<3; i++) { }
pinMode(pinLEDs[i], OUTPUT); digitalWrite(pinLEDs[i], toggleBtns[i]);
pinMode(pinBtns[i], INPUT_PULLUP); prevStates[i] = stateBtns[i];
} }

Department of Automotive Engineering 19


2022 Fall, Embedded System (D. Kim)
예제) 디지털 입력 : 3 Btns & 3 LEDs (계속)

[예제2.4] 스위치 A : 빨강 LED, 스위치 B : 녹색 LED, 스위치 C : 노랑 LED


내장 Pullup 이용.
3개 버튼으로 3개 LED를 각각 제어, Toggle 스위치 이용.

const byte pinLEDs[3] = {13, 8, 7}; void loop() {


const byte pinBtns[3] = {2, 3, 4}; for(int i=0; i<3; i++) {
stateBtns[i] = (bool) !digitalRead(pinBtns[i]);
bool stateBtns[3] = {false, false, false}; if (stateBtns[i]) {
bool prevStates[3] = {false, false, false}; if (stateBtns[i] != prevStates[i]) {
bool toggleBtns[3] = {false, false, false}; toggleBtns[i] = !toggleBtns[i];
Serial.println(toggleBtns[i]);
void setup() { }
for (int i=0; i<3; i++) { }
pinMode(pinLEDs[i], OUTPUT);
pinMode(pinBtns[i], INPUT_PULLUP); digitalWrite(pinLEDs[i], toggleBtns[i]);
} prevStates[i] = stateBtns[i];
Serial.begin(9600); }
} }

Department of Automotive Engineering 20


2022 Fall, Embedded System (D. Kim)
2.3 Debouncing

 Bounce : 기계적 스위치 작동에 따른 전압의 진동 현상

스위치 위치 스위치 위치
release release
UP UP

press press
DOWN DOWN

디지털 신호 디지털 신호
BOUNCE HIGH HIGH BOUNCE
HIGH HIGH

LOW LOW
LOW LOW

Department of Automotive Engineering 21


2022 Fall, Embedded System (D. Kim)
2.3 Bouncing?
카운트 프로그램 :
Button Count : 누를 때마다 카운트 1씩 증가
Button Reset : 카운트 0으로 리셋

const byte pinBtnCount = 8; Button Count를 누를 때 :


const byte pinBtnReset = 9; 한 번에 2 이상 증가
delay(10)으로 변경 :
한 번에 10 이상 증가
int count=0; delay() 제거 :
한번에 20 이상 증가
void setup() {
pinMode(pinBtnCount, INPUT_PULLUP);
pinMode(pinBtnReset, INPUT_PULLUP);
Serial.begin(9600);
}
No Bouncing Effect

void loop() { Wrong Programming


if (!digitalRead(pinBtnCount)) count++;
if (!digitalRead(pinBtnReset)) count = 0; Multiple Counts Due to Multiple Executions of loop()
Serial.println(count);
delay(100);
}

Department of Automotive Engineering 22


2022 Fall, Embedded System (D. Kim)
2.3 Bouncing

카운트 프로그램 :
Button Count : 누를 때마다 카운트 1씩 증가
Button Reset : 카운트 0으로 리셋

const byte pinBtnCount = 8; void loop() {


const byte pinBtnReset = 9; int stateBtnCount, stateBtnReset;

int count=0, lastBtnCount=0, lastBtnReset=0; stateBtnCount = !digitalRead(pinBtnCount);

void setup() { if (stateBtnCount != lastBtnCount) {


pinMode(pinBtnCount, INPUT_PULLUP); if(stateBtnCount){
pinMode(pinBtnReset, INPUT_PULLUP); count++;
Serial.begin(9600); Serial.println(count);
Serial.println(count); }
} lastBtnCount = stateBtnCount;
}

Button Count를 누를 때 : stateBtnReset = !digitalRead(pinBtnReset);


대부분 1씩 증가 if (stateBtnReset != lastBtnReset) {
간혹 : if(stateBtnReset) {
누를 때 2회 수행 count = 0;
뗄 때 1회 추가 수행 Serial.println(count);
}
lastBtnReset = stateBtnReset;
}
Bouncing }

Department of Automotive Engineering 23


2022 Fall, Embedded System (D. Kim)
2.3 Debouncing : Software

Bounce2 라이브러리 :

#include <Bounce2.h> // Bounce2 라이브러리 가져오기


Bounce 바운스객체 = Bounce();

void setup() {

바운스객체.attach(핀 번호); // Debounce 핀 지정 (PULLUP등 핀 설정 후)
바운스객체.interval(시간간격_ms); // Debounce time을 ms 단위로 설정

}

void loop() {

바운스객체.update(); // 바운스객체 업데이트 (버튼 상태가 변하면 true 반환)
변수 = 바운스객체.read(); // 업데이트된 핀 값을 읽음
바운스객체.fell(); // 핀이 HIGH에서 LOW로 변하면 true 반환
바운스객체.rose(); // 핀이 LOW에서 HIGH로 변하면 true 반환

}

Department of Automotive Engineering 24


2022 Fall, Embedded System (D. Kim)
2.3 Debouncing : Bounce2 라이브러리

#include <Bounce2.h> void loop() {


Bounce btnCount = Bounce(); int stateBtnCount, stateBtnReset;

const byte pinBtnCount = 8; btnCount.update();


const byte pinBtnReset = 9;
if (btnCount.fell()) {
int count=0, lastBtnCount=0, lastBtnReset=0; stateBtnCount = !btnCount.read();
if(stateBtnCount){
void setup() { count++;
pinMode(pinBtnCount, INPUT_PULLUP); Serial.println(count);
pinMode(pinBtnReset, INPUT_PULLUP); }
Serial.begin(9600); lastBtnCount = stateBtnCount;
Serial.println(count); }

btnCount.attach(pinBtnCount); stateBtnReset = !digitalRead(pinBtnReset);


btnCount.interval(50); // ms if (stateBtnReset != lastBtnReset) {
} if(stateBtnReset) {
count = 0;
Button Count를 누를 때 : Serial.println(count);
1씩 증가 }
lastBtnReset = stateBtnReset;
}
}
Debouncing

Department of Automotive Engineering 25


2022 Fall, Embedded System (D. Kim)
2.3 Debouncing : Hardware

Debouncing 회로 :

5V Capacitor를 Tact Switch에 병렬 연결


C 용량이 너무 크면, 스위치 반응 속도 저하
C 용량이 너무 작으면, Debouncing 효과 저하
R
Bouncing 주기에 따라 적절한 용량의 C 선택
: 𝑅𝐶 > 𝐵𝑜𝑢𝑛𝑐𝑖𝑛𝑔 𝑃𝑒𝑟𝑖𝑜𝑑

디지털 입력

Tact 스위치 C
(10μF, 1μF, 0.1μF)

Department of Automotive Engineering 26


2022 Fall, Embedded System (D. Kim)
Q&A

Thank you!
dwkim@yu.ac.kr

Department of Automotive Engineering 27

You might also like