0

I have a potentiometer attached to one analog port of arduino (assume A0)
I'm reading the value of that using input_voltage = analogRead (A0); which is a value between 0-1023.
So I want to map this value to frequency from 1Hz to 20Hz. Then I want to fade my LED using this frequency.
Tried to doing this map using int frequency = map (input_voltage, 0, 1023, 1, 20);
But how can i apply this frequency to LED?
I mean PWM has a constant frequency (about 500Hz). How should I apply frequencies from 1 to 20 to this LED in order to fade it?

  • 2
    Do you want to blink or flicker instead of dimming the led ? Or does 1Hz mean you want to fade for 1 sec between ON and OFF (20 Hz = 50 ms between On and Off will be no visible difference to immediate switching) – DataFiddler May 24 at 10:11
  • What is your goal? Do you want to change the rate at which you flash an LED? PWM (Pulse width modulation) doesn't to vary the frequency of the output - it varies the duty cycle (ratio of on-time to off-time) of a fixed frequency output. If you want to dim the output of your LED based on the input pot, it seems to me you should map 0-1023 to 0-255 , an use the 0-255 output to control a PWM output pin. If you want to control the flashing rate of the LED, you need to use something other than PWM. – Duncan C May 24 at 20:31
  • @DuncanC Thanks for your help. What is that "something other than PWM" ? For example timers? – Ramtin May 25 at 8:22
0

There is a library in which you can change the frequency

Here is my example code I did long time ago I had the same problem

Get the library from here

https://github.com/atmelino/Arduino/tree/master/libraries/PWM

Go ahead and try example code there and try to understand too.

Include the library first to code

my code

void setup() {
  pinMode(13, OUTPUT);
  pinMode(8, OUTPUT);
  digitalWrite(8,LOW);
  Serial.begin(115200);
  InitTimersSafe();
}
void loop() {
  //mapping
  long x = map(analogRead(A0),0,1023,1,20);
  bool b = SetPinFrequencySafe(9,x);
  digitalWrite(13,b);
  pwmWrite(9,127);
  Serial.print(x);
  Serial.print(" Hz ");
  Serial.println(b);
  delay(100);
}

Built in led will turn on if the frequency is stetted.

By the way led will turn on in 50% on and 50% off in one cycle in pin 9.

Use only the PWM pins in Arduino 3,9,6 etc.

Now you can change the PWM frequency...

I just put a led in the female socket in for easiness + terminal in 9 and - terminal in 8 to go to ground that's why I did put pin 8 to low.

| improve this answer | |
  • Thank you for this complete and helpful answer! – Ramtin May 24 at 10:40
  • Wouldn't it be easier to modulate the duty cycle of the PWM for fading a LED? By adapting the frequency between 1Hz and 20Hz, you'll just get changing flashing rates, unless you integrate the PWM output with a (rather large) capacitor. – imalipusram May 24 at 15:04
  • @imalipusram I understand but the question asked me to do this using frequency... – Ramtin May 25 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.