Go Down

Topic: pwm (20 hz) con un pot (Read 196 times) previous topic - next topic


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.
The art of getting good answers lies in asking good questions.


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

Code: [Select]

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?):

Code: [Select]

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 :-)

Alternative (better if you have a lot of other things going on) is of course by using a timer interrupt.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.


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.
Electronics and firmware/software design and assistance. No project too small


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.

Go Up