ESP32๋Š” GPIO34-39๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ํ•€์„ PWM์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. PWM์„ ํ™œ์šฉํ•ด์„œ LED์˜ ์„ธ๊ธฐ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์ถœ๋ ฅํ•  ์ˆ˜ ๋„ ์žˆ๊ณ , ์„œ๋ณด๋ชจํ„ฐ, ์Šคํ…๋ชจํ„ฐ, ํ”ผ์—์กฐ ์Šคํ”ผ์ปค ๋“ฑ์œผ๋กœ ํ™œ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ์•„๋‘์ด๋…ธ์˜ AnalogWrite์™€ ๊ฐ™์ด ์ด์šฉํ•˜๋ฉด ๋˜๋Š”๋ฐ ์•„๋‘์ด๋…ธ์™€ ๋‹ฌ๋ฆฌ ๋”ฐ๋กœ ์„ค์ •ํ•ด์•ผํ•˜๋Š” ๊ฐ’๋“ค์ด ์กด์žฌํ•œ๋‹ค.

  • Signal Frequency: 1์ดˆ์— ๋ช‡๋ฒˆ ON/OFF๋ฅผ ํ•  ๊ฒƒ์ธ๊ฐ€
  • Resolution: PWM ํ•ด์ƒ๋„, 8bit์ธ ๊ฒฝ์šฐ 0-256(2^8), 16bit์ธ ๊ฒฝ์šฐ 0-65535(2^16)๋กœ ์„ธ๊ธฐ ์ง€์ • ๊ฐ€๋Šฅ
  • Duty Cycle: ์„ธ๊ธฐ
  • PWM Channel: 0-15๋ฒˆ์˜ ์•„๋‚ ๋กœ๊ทธ ์ฑ„๋„
  • ์‹ ํ˜ธ๋ฅผ ์ถœ๋ ฅํ•  GPIOํ•€: ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•œ ํ•€ ๋ชจ๋‘ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

์œ„์™€ ๊ฐ™์€ ๊ฐ’๋“ค์„ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.


PWM Channel

  • LEDC Chan to Group/Channel/Timer Mapping
  • ledc: 0 => Group: 0, Channel: 0, Timer: 0
  • ledc: 1 => Group: 0, Channel: 1, Timer: 0
  • ledc: 2 => Group: 0, Channel: 2, Timer: 1
  • ledc: 3 => Group: 0, Channel: 3, Timer: 1
  • ledc: 4 => Group: 0, Channel: 4, Timer: 2
  • ledc: 5 => Group: 0, Channel: 5, Timer: 2
  • ledc: 6 => Group: 0, Channel: 6, Timer: 3
  • ledc: 7 => Group: 0, Channel: 7, Timer: 3
  • ledc: 8 => Group: 1, Channel: 0, Timer: 0
  • ledc: 9 => Group: 1, Channel: 1, Timer: 0
  • ledc: 10 => Group: 1, Channel: 2, Timer: 1
  • ledc: 11 => Group: 1, Channel: 3, Timer: 1
  • ledc: 12 => Group: 1, Channel: 4, Timer: 2
  • ledc: 13 => Group: 1, Channel: 5, Timer: 2
  • ledc: 14 => Group: 1, Channel: 6, Timer: 3
  • ledc: 15 => Group: 1, Channel: 7, Timer: 3

PWM ์ฑ„๋„์€ ์œ„์™€ ๊ฐ™์ด 16๊ฐœ์˜ ์•„๋‚ ๋กœ๊ทธ ์ฑ„๋„์ด ์กด์žฌํ•˜๋Š”๋ฐ ์ฑ„๋„๋งˆ๋‹ค ํ• ๋‹น๋œ ํƒ€์ด๋จธ๊ฐ€ ๋‹ค๋ฅด๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์•„๋‚ ๋กœ๊ทธ ์„ผ์„œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ฑ„๋„์„ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•ด์•ผํ•œ๋‹ค.


Duty Cycle

pwm

Duty์˜ ๊ฐ’์— ๋”ฐ๋ผ ์ถœ๋ ฅ์˜ ์„ธ๊ธฐ๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค. ์œ„์˜ ์‚ฌ์ง„์—์„  8bit์˜ resolution์„ ๊ฐ€์งˆ ๋•Œ dury cycle์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋‹ค. 100% duty cycle์ธ ๊ฒฝ์šฐ 8bit resolution์˜ ์ตœ๋Œ“๊ฐ’์ธ analoyWrite(255)์™€ ๊ฐ™์€ ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


PWM ์‚ฌ์šฉ๋ฒ•

* ledcSetup(PWM_Channel_Number, Frequency, resolution)
* ledcAttachPin(GPIO_PIN , CHANNEL)
* ledcWrite(CHANNEL, DUTY_CYCLE)

์œ„์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ PWM์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ledcSetup์„ ์‚ฌ์šฉํ•ด ์ฑ„๋„๊ณผ ์ฃผํŒŒ์ˆ˜, ํ•ด์ƒ๋„๋ฅผ ์„ค์ •ํ•˜๊ณ  ledcAttachPin์œผ๋กœ ์„ค์ •ํ•œ ์ฑ„๋„์˜ ์ถœ๋ ฅ์„ ์–ด๋Š GPIO ํ•€์œผ๋กœ ๋ณด๋‚ผ ์ง€ ์ง€์ •ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ledcWrite์„ ์‚ฌ์šฉํ•ด ์›ํ•˜๋Š” ์ฑ„๋„์— analog๊ฐ’์„ ์ „์†กํ•œ๋‹ค.


PWM์œผ๋กœ LED ์ถœ๋ ฅํ•˜๊ธฐ

const int ledPin = 16;
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;

void setup() {

  ledcSetup(ledChannel, freq, resolution);
  ledcAttachPin(ledPin, ledChannel);
}

void loop() {

  for (int i = 0; i < 256; i++) {
    ledcWrite(ledChannel, i);
    delay(10);
  }
  for (int i = 256; i > 0; i--) {
    ledcWrite(ledChannel, i);
    delay(10);
  }
}

LED๋ฅผ D16์— ์—ฐ๊ฒฐํ•œ ํ›„ ESP32๋ณด๋“œ๋ฅผ ์„ ํƒํ•ด ์—…๋กœ๋“œํ•˜๋ฉด ์–ด๋‘์›Œ์กŒ๋‹ค๊ฐ€ ๋ฐ์•„์ง€๋Š” ๊ฒƒ์„ ๋ฐ˜๋ณตํ•˜๋Š” LED๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


PWM์œผ๋กœ ์„œ๋ณด๋ชจํ„ฐ ์ถœ๋ ฅํ•˜๊ธฐ

const int servoPin = 23; //D23์— ์—ฐ๊ฒฐ
const int servoChannel = 1;
const int freq = 50;
const int resolution = 16;

void setup() {

  ledcSetup(servoChannel, freq, resolution);
  ledcAttachPin(servoPin, servoChannel);
}

void loop() {

  servoWrite(servoChannel, 90); //90๋„ ํšŒ์ „
  delay(1000);
  servoWrite(servoChannel, 0); //์›๋ž˜ ์ž๋ฆฌ๋กœ
  delay(1000);
}


void servoWrite(int ch, int deg)
{
  int duty = deg * 18.2 + 3277;

  ledcWrite(ch, duty);
}

์„œ๋ณด๋ชจํ„ฐ๋Š” Servo_ESP32๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋”ฐ๋กœ ์กด์žฌํ•ด ์‰ฝ๊ฒŒ ์„œ๋ณด๋ชจํ„ฐ๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ledc๋งŒ์„ ์ด์šฉํ•ด์„œ ์„œ๋ณด๋ชจํ„ฐ๋ฅผ ์–ด๋ ต์ง€ ์•Š๊ฒŒ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค. 50Hz๋กœ ์ฃผํŒŒ์ˆ˜๋ฅผ ์„ค์ •ํ•˜๊ณ  duty๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉฐ ์„œ๋ณด๋ชจํ„ฐ์˜ ์ถœ๋ ฅ์„ ์ œ์–ดํ•˜๋ฉด duty๊ฐ’์— ๋”ฐ๋ผ ์ œ์–ดํ•˜๋Š” ์„œ๋ณด๋ชจํ„ฐ์˜ ๊ฐ๋„๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 50Hz์— 16bit์˜ ํ•ด์ƒ๋„๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด duty๊ฐ€ 3277์ธ ๊ฒฝ์šฐ 1ms์ถœ๋ ฅ๋ ๋•Œ 0๋„, 4915์ธ ๊ฒฝ์šฐ 1.5ms ์ถœ๋ ฅ๋˜๊ณ  90๋„, 6554์ธ ๊ฒฝ์šฐ 2ms์ถœ๋ ฅ๋˜๊ณ  180๋„๋กœ ํšŒ์ „ํ•œ๋‹ค.


์•„๋‚ ๋กœ๊ทธ ์ถœ๋ ฅ ์—ฌ๋Ÿฌ ๊ฐœ ์‚ฌ์šฉํ•˜๊ธฐ

์•„๋‚ ๋กœ๊ทธ ์ถœ๋ ฅ์„ ์—ฌ๋Ÿฌ ๊ฐœ ์‚ฌ์šฉํ•  ๋• ์ฑ„๋„์ด ๊ฒน์น˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค. ๋˜ํ•œ LED์™€ ์„œ๋ณด๋ชจํ„ฐ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์ฃผํŒŒ์ˆ˜์™€ ํ•ด์ƒ๋„ ๋ชจ๋‘ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ํƒ€์ด๋จธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์œ„์˜ pwm channel์„ ๋ณด๋ฉด ์ฑ„๋„ 0๊ณผ 1์€ ๊ฐ™์€ ํƒ€์ด๋จธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์„œ๋ณด๋ชจํ„ฐ์™€ led์˜ ์ฑ„๋„ 0๊ณผ 1์„ ๋ถ€์—ฌํ•˜๋ฉด ๋‘ ๊ฐœ ์ค‘ ํ•˜๋‚˜์˜ ์„ผ์„œ๋Š” ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ฑ„๋„ 0๊ณผ 2์™€ ๊ฐ™์ด ๋‹ค๋ฅธ ํƒ€์ด๋จธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค. ์•„๋ž˜๋Š” LED์™€ ์„œ๋ณด๋ชจํ„ฐ๋ฅผ ํ•œ ๋ฒˆ์— ์ œ์–ดํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค. ์„œ๋ณด๋ชจํ„ฐ๊ฐ€ 90๋„๋กœ ํšŒ์ „ํ–ˆ๋‹ค๊ฐ€ ์ œ์ž๋ฆฌ๋กœ ๊ฐ„ ํ›„ LED๊ฐ€ ์ผœ์กŒ๋‹ค ๊บผ์ง€๋„๋ก ํ•˜์˜€๋‹ค.

const int servoPin = 23;
const int servoChannel=0;
const int ledPin = 16;
const int ledChannel = 2;

void setup(){
    ledcSetup(servoChannel, 50, 16);
  ledcAttachPin(servoPin, servoChannel);

    ledcSetup(ledChannel, 5000, 8);
  ledcAttachPin(ledPin, ledChannel);

}
 void loop(){

  servoWrite(servoChannel, 90);
  delay(1000);
  servoWrite(servoChannel, 0);
  delay(1000);
  ledcWrite(ledChannel, 100);
  delay(1000);
  ledcWrite(ledChannel, 0);
  delay(1000);

}

void servoWrite(int ch, int deg)
{
  int duty = deg*18.2 + 3277;

  ledcWrite(ch, duty);
}

Reference