pwm (20 hz) con un pot

I need a programming to control a led with a pwm signal, with a pot and with a frequency of 20 hz. Please

and with a frequency of 20 hz

Why? The normal PWM frequency is much higher than that.

I like this solution, found through a quick Google search on this very forum:

loop() {
 digitalWrite( 7 , (millis()%50 < 25) ? (HIGH) : (LOW)  )  ; // 20 Hz 50% duty cycle Pin 7

Or for higher resolution for your duty cycle (is that where the pot comes in?):

loop() {
 digitalWrite( 7 , (micros()%50000 < 25000) ? (HIGH) : (LOW)  )  ; // 20 Hz 50% duty cycle Pin 7

If you can explain how this works you may use it for your assignment :slight_smile:

Alternative (better if you have a lot of other things going on) is of course by using a timer interrupt.

I had a friend who used to do PWM the same way but with random numbers and it worked pretty well. ISTR that it keeps the switching noise down.

And with what granularity do you want to change the duty cycle of the 20 Hz
Pwm? You’ll probably get a number range of 0 to 1023 from the potentiometer by analogRead() and you’ll have to base the duty cycle formula on that.