// ESP32-ESP32S2-AnalogWrite
// https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite
// for https://forum.arduino.cc/t/how-to-make-a-controlled-duty-cycle-and-frequency-output/1108413/5


#include <pwmWrite.h>

const byte pwm100Hz20DutyPin = 19;
const byte pwm100Hz30DutyPin = 18;
const byte pwm100Hz50DutyPin = 5;
const byte pwm25Hz50DutyPin = 21;
Pwm pwm = Pwm();

// pwm.write(pwmPin, duty, frequency, resolution);

void setup() {
  Serial.begin(115200);
  pwm.write(pwm100Hz20DutyPin, 195, 100, 10);
  pwm.write(pwm100Hz30DutyPin, 307, 100, 10);
  pwm.write(pwm100Hz50DutyPin, 512, 100, 10);

  pwm.attach(pwm25Hz50DutyPin, 4, true);
  pwm.write(pwm25Hz50DutyPin, 512, 25, 10);
}

void loop() {
  delay(10); // speeds up the simulation
  int pot = analogRead(12);
  static int lastpot = 0;
  if(lastpot/1 != pot/1){
    pwm.write(pwm100Hz20DutyPin, map(pot,0,4096,0,1023), 100, 10);
    pwm.write(pwm100Hz30DutyPin, map(pot,0,4096,1023,0), 100, 10);
    pwm.write(pwm100Hz50DutyPin, map(pot,0,4096,50,900), 200, 10);
    Serial.println(pot);
    lastpot = pot; 
  }

}
Loading chip...chip-scope