Sunday, October 5, 2014

Arduino PWM

Arduino uno board uses timers to generate pwm signals on pins 3,5,6,9,10 and 11 using analogwrite() command. These pwm signals have fixed frequency of 490Hz unless pins 5 and 6 have frequency equals to 980Hz. if we need different frequency we will use the pwm library.
Using pwm library for arduino we can adjust our pwm frequency in easy way, this library works with some pins in the arduino uno board, and the command used is:
 Here is an example which generate a pwm signal on pin 9 with a frequency of 20KHz:
#include <PWM.h>

int32_t frequency = 20000;

void setup(){
  bool success = SetPinFrequencySafe(9, frequency);
  if(success) {
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);    
void loop(){
  int s = analogRead(A0);