I tried to blink an LED using only hardware PWM. For this, I set the frequency to 2Hz and the duty cycle to 50% for 1s blinks, but it doesn’t work.
Here is the code bellow:
int pwmChannel = 0; // Selects channel 0
int frequence = 2; // PWM frequency of 2 Hz
int resolution = 8; // 8-bit resolution, 256 possible values
int pwmPin = 10;
void setup(){
// Configuration of channel 0 with the chosen frequency and resolution
ledcSetup(pwmChannel, frequence, resolution);
// Assigns the PWM channel to pin 10
ledcAttachPin(pwmPin, pwmChannel);
// Create the selected output voltage
ledcWrite(pwmChannel, 127); // 1.65 V
}
void loop(){
}
Hi,R_007:
I have written pwm control LED lights related code and I ran a burn test, and there were no problems, you can refer to it and rewrite it:
int ledPin = D10; // LED connected to digital pin 10
void setup() {
// declaring LED pin as output
pinMode(ledPin, OUTPUT);
}
void loop() {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
I have already run a similar code just to test if the PWM is working. The problem is that I need to select a specific frequency. Oddly, my code works fine for higher frequencies but not for lower ones. I have already tested changing the PWM channel, pin, and resolution.