Faster digital-potentiometer controlled PWM circuit ?

I have a very simple PWM control circuit on an Uno R3 with a 0-5V potentiometer wiper on A0 and outputting PWM on D11. With following code, the circuit functions well but I am getting a repetition rate of approximately 490Hz, and I need it to me much faster - preferably over 10kHz if possible. I have optimised the code as best I can, but have not managed to make any increase in the speed. I have also looked at other posts here about speeding up analog writes and reads using registers, but cannot work out how to translate this into code. Please forgive my ignorance. Can someone show me how to change my code to achieve this ?

int pin=0;
void setup() {
}
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(pin);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 * 0.0009775);
  // Write the output to pin11
  analogWrite(11, voltage*51);
}

ReadAnalogVoltageWritePCM.ino (314 Bytes)

read this page analogWrite() - Arduino Reference

"The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. Pins 3 and 11 on the Leonardo also run at 980 Hz."

plus if you look at the example code it shows the easiest way to convert 0 to 1023 (input) to 0 to 255 (output)is just to divide by 4

I have seen other Threads in which the standard PWM frequency was changed. Google should find them for you.

...R

Robin2:
I have seen other Threads in which the standard PWM frequency was changed. Google should find them for you.

...R

Indeed yes, I have searched for and read more than a few posts and articles so far, both on the analogue Read and analogueWrite functions, but have not found any example code yet - at least none that I can understand how to implement. I know that it involves not using these functions and doing it by working with registers, but have not managed to comprehend how to put that into code.

ElectronHT:
I know that it involves not using these functions

I think it is is possible to modify the code for analogWrite() to get it to work at a different frequency. I imagine it is just a case of changing values related to the relevant Timer.

All the source code is included with the Arduino IDE.

analogRead() has nothing at all to do with analogWrite(). analogWrite() should probably have been called pwmWrite().

...R

You change the speed of the hardware timers to change the PWM frequency. On the Uno pins
5 and 6 are connected to timer0 which is used for micros()/millis()/delay(), so you normally
wouldn't want to change that. The hardware timers are documented in the datasheet - directly
programming them is not portable of course.

There is a TimerOne library to help change the settings of timer1 in a relatively painless way

  • this timer controls PWM on pins 9 and 10 on the Uno

Robin2:
I think it is is possible to modify the code for analogWrite() to get it to work at a different frequency. I imagine it is just a case of changing values related to the relevant Timer.

All the source code is included with the Arduino IDE.

analogRead() has nothing at all to do with analogWrite(). analogWrite() should probably have been called pwmWrite().

...R

And THANK YOU Mark! - It not only should have been, but IS called just that in the PWM library. Problem solved.