week_3 (GPIO) - JINEEH/SmartDevice_JinHee GitHub Wiki

GPIO ๋ฐ ์™ธ๋ถ€ LED ๊ตฌํ˜„

1. ํ•™์Šต ๋ชฉํ‘œ

1-1. ESP32์—์„œ GPIO ์‚ฌ์šฉ

2. GPIO ์ •์˜

2-1. ๋‹ค์šฉ๋„ ์ž…์ถœ๋ ฅ(General Purpose Input/Output)
2-2. ์ž…/์ถœ๋ ฅ ๋™์ž‘์ด ์‚ฌ์šฉ์ž์— ์˜ํ•ด ์ œ์–ด๋˜๋Š” ์ง‘์  ํšŒ๋กœ or ์ „๊ธฐ ํšŒ๋กœ ๊ธฐํŒ์˜ ๋””์ง€ํ„ธ ์‹ ํ˜ธํ•€
2-3. ์–ด์…ˆ๋ธ”๋ฆฌ ๋ ˆ๋ฒจ์˜ ํšŒ๋กœ๋ง ์„ค๊ณ„์ž์— ์˜ํ•ด ๊ตฌํ˜„๋จ

3. ์™ธ๋ถ€ LED ์‹ค์Šต 1

  • ์™ธ๋ถ€ LED ์ผœ๊ณ  ๋„๊ธฐ(๋””์ง€ํ„ธ ์ถœ๋ ฅ)
  • ์ค€๋น„๋ฌผ: ESP32, ESP32 ํ™•์žฅ ์‹ค๋“œ, LED ๋ชจ๋“ˆ, ์ ํผ ์ผ€์ด๋ธ” 3๊ฐœ
  • ์‹คํ–‰ ์ฝ”๋“œ
const int ledPin = 4; //led ํ•€ ๋ฒˆํ˜ธ ์„ค์ •
//setupํ•จ์ˆ˜๋Š” ๋ฆฌ์…‹์„ ๋ˆ„๋ฅด๊ฑฐ๋‚˜ ๋ณด๋“œ์— ์ „์›์„ ๊ณต๊ธ‰ํ•  ๋•Œ ํ•œ ๋ฒˆ ์‹คํ–‰

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

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(1000);

  digitalWrite(ledPin, LOW);  
  delay(1000);
}

4. ์™ธ๋ถ€ LED ์‹ค์Šต 2

  • ์™ธ๋ถ€ LED ์ผœ๊ณ  ๋„๊ธฐ(์•„๋‚ ๋กœ๊ทธ ์ถœ๋ ฅ)
  • ์ค€๋น„๋ฌผ: ESP32, ESP32 ํ™•์žฅ ์‹ค๋“œ, LED ๋ชจ๋“ˆ, ์ ํผ ์ผ€์ด๋ธ” 3๊ฐœ
  • ์‹คํ–‰ ์ฝ”๋“œ
const int ledPin = 4;
void setup() {
  // put your setup code here, to run once:
    pinMode(ledPin, OUTPUT);
}
void loop() {
  // put your main code here, to run repeatedly:
  for(int i = 0; i <= 255; i++) {
    analogWrite(ledPin, i);
    delay(15);
  }
  for(int i = 255; i >= 0; i--){
     analogWrite(ledPin, i);
     delay(15);
  }
}


5. ์™ธ๋ถ€ LED ์‹ค์Šต 3

  • ์™ธ๋ถ€ LED ์ผœ๊ณ  ๋„๊ธฐ(๋””์ง€ํ„ธ ์ž…๋ ฅ, ๋ฒ„ํŠผ ์‚ฌ์šฉ)
  • ์ค€๋น„๋ฌผ: ESP32, ESP32 ํ™•์žฅ ์‹ค๋“œ, LED ๋ชจ๋“ˆ, ๋ฒ„ํŠผ ๋ชจ๋“ˆ, ์ ํผ ์ผ€์ด๋ธ” 6๊ฐœ
  • ์‹คํ–‰ ์ฝ”๋“œ
const int buttonPin = 33;
const int ledPin = 4;
int buttonState = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}
void loop() {
  // put your main code here, to run repeatedly:
  buttonState = digitalRead(buttonPin);
  if(buttonState == LOW) {
    digitalWrite(ledPin, HIGH);
  }
  else{
    digitalWrite(ledPin, LOW);
  }
 }

6. ์™ธ๋ถ€ LED ์‹ค์Šต 4

  • ์™ธ๋ถ€ LED ์ผœ๊ณ  ๋„๊ธฐ(์•„๋‚ ๋กœ๊ทธ ์ž…๋ ฅ, ๊ฐ€๋ณ€ ์ €ํ•ญ)
  • ์ค€๋น„๋ฌผ: ESP32, ESP32 ํ™•์žฅ ์‹ค๋“œ, LED ๋ชจ๋“ˆ, ๊ฐ€๋ณ€ ์ € ๋ชจ๋“ˆ, ์ ํผ ์ผ€์ด๋ธ” 6๊ฐœ
  • ์‹คํ–‰ ์ฝ”๋“œ
const int potPin = 34;
const int ledPin = 4;
int potValue = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(potPin, INPUT);
  pinMode(ledPin, OUTPUT);
}
void loop() {
  // put your main code here, to run repeatedly:
  potValue = analogRead(potPin);
  analogWrite(ledPin, map(potValue, 0, 4095, 0, 255));
  delay(500);
}

7. ๋ถ€๊ฐ€์„ค๋ช…


โ— ESP32 ์นฉ์€ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ 48ํ•€๊ณผ ํ•จ๊ป˜ ์ œ๊ณต๋˜๋ฉฐ ์•„๋‘์ด๋…ธ์™€ ์‚ฌ์šฉ๋ฒ•์ด ๊ฐ™์Œ
โ— ESP Devkit: EN, VIN, 3V3, GND ๋ฐ ์ž…๋ ฅ ์ „์šฉํ•€(34,35,36,39)๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ํ•€์„ ๋””์ง€ํ„ธ ์ถœ๋ ฅ์‹œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ
โ— ๊ทธ๋ผ์šด๋“œ(GND)๊ฐ€ ๋งŽ์œผ๋ฉด ์•ˆ์ •์ ์œผ๋กœ ๋Œ์•„๊ฐ
โ— UART(Universal Asynchronous Receiver/Transmitter): ๋‘ ์žฅ์น˜ ๊ฐ„์— ์ง๋ ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตํ™˜ํ•˜๋ฉฐ ๋‘๊ฐœ์˜ ์ „์„ ์œผ๋กœ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ์–ด ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ํ†ต์‹  ํ”„๋กœํ† ์ฝœ ์ค‘ ํ•˜๋‚˜์ž„
โ— DAC: ๋””์ง€ํ„ธ ์•„๋‚ ๋กœ๊ทธ ๋ณ€ํ™˜ ํšŒ๋กœ(์‹œ๊ฐ ์ž๋ฃŒ ํ™œ์šฉ์„ ์œ„ํ•ด ํŒจ๋„ ์ž์ฒด ํ™”์†Œ์˜ ๋ฐ๊ธฐ๋ฅผ ์ œ์–ดํ•จ)
โ— ADC: ์•„๋‚ ๋กœ๊ทธ ๋””์ง€ํ„ธ ๋ณ€ํ™˜ ํšŒ๋กœ์ด๋ฉฐ DAC์˜ ์—ญ๋ฐฉํ–ฅ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰, ์•„๋‚ ๋กœ๊ทธ ์‹ ํ˜ธ๋ฅผ ๋””์ง€ํ„ธ ์‹ ํ˜ธ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉฐ ๋Œ€๋ถ€๋ถ„์˜ ์„ผ์„œ ๊ธฐ๊ธฐ์— ์‚ฌ์šฉ๋˜๋Š” ํšŒ๋กœ
โ— TWN: ๋””์ง€ํ„ธ ์ถœ๋ ฅ(DAC๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  LED ๋ฐ๊ธฐ ์กฐ์ ˆ ๊ฐ€๋Šฅ)
โ— PWM: ํŽ„์Šค ๋ณ€์กฐ์˜ ์ผ์ข…์ด๋ฉฐ ์‹ ํ˜ธ ํฌ๊ธฐ์— ๋”ฐ๋ผ ํŽ„์Šค์˜ ํญ์„ ๋ณ€์กฐํ•จ(DAC์™€ ๋ฐ˜๋Œ€๋˜๋Š” ๊ฐœ๋…์˜ ๋””์ง€ํ„ธ ์ถœ๋ ฅ)

8. ์‹œ๊ฐ ์ž๋ฃŒ

  • ESP32 Devkit
  • DAC์™€ PWM ๋น„๊ต

9. ์ฐธ๊ณ  ์˜์ƒ