// the number of the LED pin
const int ledPin = 9; // PIN16
// setting PWM properties
const int freq = 40000;//頻率40khz,目前測試到50HZ都正常
const int ledChannel = 0;//通道0,共有0~16
const int resolution = 8;//分辨率8bit
int PWM;
int VRpin = 17;
int VR;
void setup() {
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);//設置 通道,頻率,分辨率
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin, ledChannel);//使用pwm的腳位及通道
pinMode(VRpin, INPUT);
}
void loop() {
// increase the LED brightness
VR = analogRead(VRpin);
Serial.print("VR:");
Serial.println(VR);
PWM = map(VR, 0, 1024, 0, 255);
Serial.print("PWM:");
Serial.println(PWM);
ledcWrite(ledChannel, PWM);//通道及工作週期50%
}
Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1