[ESP32] Output multiple independent PWM signals

I will leave a memorandum about how to output multiple independent PWM signals, such as when you want to control multiple servo motors using ESP32.

The sketch is below

// 5 PIN と 13 PIN にPWM信号を出力させる例
#include<ESP32Servo.h>   //esp32でサーボを使うためのライブラリをインクルード

Servo myServo1;
  #define SERVO_OUT 5 //5PINをサーボモーター制御用と想定
  #define ENA 13  //13PINをモータドライバ(L298N)のENAと想定

  const int freq_servo = 50;

  int vref; 

  const int freq_dc = 50;
  const int pwm_channel = 13;
  const int resolution = 8;

void setup() {
  //myServo1.attach(5);    //サーボ制御用のGPIOを5pinに設定
  myServo1.attach(SERVO_OUT, 500, 2400); 
  myServo1.setPeriodHertz(freq_servo);
  vref = 0;
  ledcWrite(pwm_channel, vref);
  ledcSetup(pwm_channel, freq_dc, resolution); 
  ledcAttachPin(ENA, pwm_channel); 

}

void loop() {
  myServo1.write(120);    //90°回転 = 起点

  vref = 128 ;
  ledcWrite(pwm_channel, vref);

}

I was able to output PWM signals to 5 pin and 13 pin.

コメント

タイトルとURLをコピーしました